[HttpFoundation] SPL on ParameterBag

Added some SPL interface goodness to the ParameterBag class
This commit is contained in:
Chris Boden 2012-03-06 10:07:49 -05:00
parent b4c1e03954
commit 665fdebc8c
2 changed files with 49 additions and 1 deletions

View File

@ -18,7 +18,7 @@ namespace Symfony\Component\HttpFoundation;
*
* @api
*/
class ParameterBag
class ParameterBag implements \IteratorAggregate, \Countable
{
/**
* Parameter storage.
@ -278,4 +278,24 @@ class ParameterBag
return filter_var($value, $filter, $options);
}
/**
* IteratorAggregate method for looping the instance
*
* @return array An array of parameters
*/
public function getIterator()
{
return new \ArrayIterator($this->parameters);
}
/**
* Countable method returning the number of key/val pairings
*
* @return int Number of parameters held in the bag
*/
public function count()
{
return count($this->parameters);
}
}

View File

@ -202,4 +202,32 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
}
/**
* @covers Symfony\Component\HttpFoundation\ParameterBag::getIterator
*/
public function testGetIterator()
{
$parameters = array('foo' => 'bar', 'hello' => 'world');
$bag = new ParameterBag($parameters);
$i = 0;
foreach ($bag as $key => $val) {
$i++;
$this->assertEquals($parameters[$key], $val);
}
$this->assertEquals(count($parameters), $i);
}
/**
* @covers Symfony\Component\HttpFoundation\ParameterBag::count
*/
public function testCount()
{
$parameters = array('foo' => 'bar', 'hello' => 'world');
$bag = new ParameterBag($parameters);
$this->assertEquals(count($parameters), count($bag));
}
}