Skip to content

Commit

Permalink
Config Section now implements ArrayAccess
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Mar 3, 2014
1 parent 3113b96 commit 4e4a0b5
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
72 changes: 71 additions & 1 deletion src/Provider/ConfigSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Configuration section
*/
class ConfigSection implements ConfigSectionInterface
class ConfigSection implements ConfigSectionInterface, \ArrayAccess
{
protected $_name;
protected $_items;
Expand Down Expand Up @@ -75,4 +75,74 @@ public function addItem($key, $value)
$this->_items[$key] = $value;
return $this;
}

/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Whether a offset exists
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
*
* @param mixed $offset <p>
* An offset to check for.
* </p>
*
* @return boolean true on success or false on failure.
* </p>
* <p>
* The return value will be casted to boolean if non-boolean was returned.
*/
public function offsetExists($offset)
{
return isset($this->_items[$offset]);
}

/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Offset to retrieve
* @link http://php.net/manual/en/arrayaccess.offsetget.php
*
* @param mixed $offset <p>
* The offset to retrieve.
* </p>
*
* @return mixed Can return all value types.
*/
public function offsetGet($offset)
{
return $this->getItem($offset);
}

/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Offset to set
* @link http://php.net/manual/en/arrayaccess.offsetset.php
*
* @param mixed $offset <p>
* The offset to assign the value to.
* </p>
* @param mixed $value <p>
* The value to set.
* </p>
*
* @return void
*/
public function offsetSet($offset, $value)
{
$this->addItem($offset, $value);
}

/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Offset to unset
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
*
* @param mixed $offset <p>
* The offset to unset.
* </p>
*
* @return void
*/
public function offsetUnset($offset)
{
unset($this->_items[$offset]);
}
}
13 changes: 13 additions & 0 deletions tests/ConfigSectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,17 @@ public function getConfigSection()
{
return new \Packaged\Config\Provider\ConfigSection();
}

public function testArrayAccess()
{
$section = $this->getConfigSection();
$this->assertFalse(isset($section['random']));
$this->assertNull($section['random']);
$section['random'] = 'testing';
$this->assertTrue(isset($section['random']));
$this->assertEquals('testing', $section['random']);
$this->assertEquals('testing', $section->getItem('random'));
unset($section['random']);
$this->assertFalse(isset($section['random']));
}
}

0 comments on commit 4e4a0b5

Please sign in to comment.