From 665fdebc8c48e1586b379cfe07c108f0f34d3819 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Tue, 6 Mar 2012 10:07:49 -0500 Subject: [PATCH] [HttpFoundation] SPL on ParameterBag Added some SPL interface goodness to the ParameterBag class --- .../Component/HttpFoundation/ParameterBag.php | 22 ++++++++++++++- .../HttpFoundation/ParameterBagTest.php | 28 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/ParameterBag.php b/src/Symfony/Component/HttpFoundation/ParameterBag.php index a5b04da031..12f7253b77 100644 --- a/src/Symfony/Component/HttpFoundation/ParameterBag.php +++ b/src/Symfony/Component/HttpFoundation/ParameterBag.php @@ -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); + } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php index 2cf9b52c22..44a0c3ebde 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php @@ -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)); + } }