Last active
December 12, 2015 05:48
-
-
Save wouterj/4724557 to your computer and use it in GitHub Desktop.
A Simple ParameterBag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ParameterBag implements \IteratorAggregate, \Countable | |
{ | |
/** | |
* @var array | |
*/ | |
private $parameters; | |
private $froozen = false; | |
/** | |
* @param array $parameters | |
*/ | |
public function __construct($parameters = array()) | |
{ | |
$this->setParameters($parameters); | |
} | |
public function set($id, $value) | |
{ | |
if ($this->isFroozen()) { | |
throw new \OverflowException('You cannot change a froozen parameterbag'); | |
} | |
$this->parameters[$id] = $value; | |
} | |
public function get($id) | |
{ | |
if (!$this->has($id)) { | |
throw new \OutOfBoundsException(sprintf('The parameter "%s" is not found', $id)); | |
} | |
return $this->parameters[$id]; | |
} | |
public function has($id) | |
{ | |
return array_key_exists($id, $this->getParameters()); | |
} | |
/** | |
* @return array | |
*/ | |
public function getParameters() | |
{ | |
return $this->parameters; | |
} | |
public function freeze() | |
{ | |
if ($this->isFroozen()) { | |
throw new \BadMethodCallException('The parameterbag is already froozen'); | |
} | |
$this->froozen = true; | |
} | |
public function isFroozen() | |
{ | |
return $this->froozen; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function getIterator() | |
{ | |
return new \ArrayIterator($this->getParameters()); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function count() | |
{ | |
return count($this->getParameters()); | |
} | |
private function setParameters(array $parameters) | |
{ | |
$this->parameters = $parameters; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'ParameterBag.php'; | |
class ParameterBagTest extends \PHPUnit_Framework_TestCase | |
{ | |
public function testNormalBehaviour() | |
{ | |
$pb = new ParameterBag(); | |
$pb->set('foo', 'lorem'); | |
$pb->set('bar', 'ipsum'); | |
$this->assertEquals('lorem', $pb->get('foo')); | |
$pb->set('baz', 'dolor'); | |
$this->assertEquals('ipsum', $pb->get('bar')); | |
$this->assertEquals('dolor', $pb->get('baz')); | |
} | |
public function testFreezing() | |
{ | |
$pb = new ParameterBag(); | |
$pb->set('foo', 'lorem'); | |
$pb->freeze(); | |
$this->assertTrue($pb->isFroozen()); | |
$this->assertEquals('lorem', $pb->get('foo')); | |
} | |
/** | |
* @expectedException OverflowException | |
*/ | |
public function testCannotChangeAFroozenBag() | |
{ | |
$pb = new ParameterBag(); | |
$pb->freeze(); | |
$pb->set('foo', 'lorem'); | |
} | |
public function testCountParameters() | |
{ | |
$pb = new ParameterBag(); | |
$pb->set('foo', 'lorem'); | |
$pb->set('bar', 'ipsum'); | |
$pb->set('baz', 'dolor'); | |
$this->assertEquals(3, count($pb)); | |
} | |
public function testIterator() | |
{ | |
$pb = new ParameterBag(); | |
$pb->set('foo', 'lorem'); | |
$pb->set('bar', 'ipsum'); | |
$pb->set('baz', 'dolor'); | |
$expected = array('lorem', 'ipsum', 'dolor'); | |
foreach ($pb as $p) { | |
$this->assertContains($p, $expected); | |
} | |
} | |
public function testFilter() | |
{ | |
$pb = new ParameterBag(); | |
$pb->set('foo', 'lorem'); | |
$pb->set('bar', 'ipsum'); | |
$pb->set('baz', 'dolor'); | |
$filter = new \CallbackFilterIterator($pb->getIterator(), function ($current, $key) { | |
return 'ba' === substr($key, 0, 2); | |
}); | |
$this->assertCount(2, $filter); | |
$this->assertFalse($pb->has('lorem')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment