diff --git a/src/Symfony/Component/HttpFoundation/HeaderBag.php b/src/Symfony/Component/HttpFoundation/HeaderBag.php index f614b094fd..b581dcca82 100644 --- a/src/Symfony/Component/HttpFoundation/HeaderBag.php +++ b/src/Symfony/Component/HttpFoundation/HeaderBag.php @@ -18,7 +18,7 @@ namespace Symfony\Component\HttpFoundation; * * @api */ -class HeaderBag +class HeaderBag implements \IteratorAggregate, \Countable { protected $headers; protected $cacheControl; @@ -267,6 +267,26 @@ class HeaderBag $this->set('Cache-Control', $this->getCacheControlHeader()); } + /** + * IteratorAggregate method for looping the instance + * + * @return array An array of parameters + */ + public function getIterator() + { + return new \ArrayIterator($this->headers); + } + + /** + * Countable method returning the number of headers + * + * @return int Number of parameters held in the bag + */ + public function count() + { + return count($this->headers); + } + protected function getCacheControlHeader() { $parts = array(); diff --git a/src/Symfony/Component/HttpFoundation/ParameterBag.php b/src/Symfony/Component/HttpFoundation/ParameterBag.php index a5b04da031..e6a67978fa 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 ArrayIterator An array of parameters wrapped in an ArrayIterator + */ + 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/src/Symfony/Component/HttpFoundation/Session/Attribute/AttributeBag.php b/src/Symfony/Component/HttpFoundation/Session/Attribute/AttributeBag.php index d1bcb0ffb6..2a49a5398a 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Attribute/AttributeBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Attribute/AttributeBag.php @@ -14,7 +14,7 @@ namespace Symfony\Component\HttpFoundation\Session\Attribute; /** * This class relates to session attribute storage */ -class AttributeBag implements AttributeBagInterface +class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Countable { private $name = 'attributes'; @@ -134,4 +134,24 @@ class AttributeBag implements AttributeBagInterface return $return; } + + /** + * + * + * @return ArrayIterator + */ + public function getIterator() + { + return new \ArrayIterator($this->attributes); + } + + /** + * + * + * @return int + */ + public function count() + { + return count($this->attributes); + } } diff --git a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php index c0b4ee57a0..03880c71df 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php @@ -16,7 +16,7 @@ namespace Symfony\Component\HttpFoundation\Session\Flash; * * @author Drak */ -class FlashBag implements FlashBagInterface +class FlashBag implements FlashBagInterface, \IteratorAggregate, \Countable { private $name = 'flashes'; @@ -155,4 +155,24 @@ class FlashBag implements FlashBagInterface { return $this->all(); } + + /** + * IteratorAggregate + * + * @return ArrayIterator + */ + public function getIterator() + { + return new \ArrayIterator($this->all()); + } + + /** + * + * + * @return int + */ + public function count() + { + return count($this->flashes); + } } diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index 13c6448874..08a7b0b817 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -27,7 +27,7 @@ use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; * * @api */ -class Session implements SessionInterface +class Session implements SessionInterface, \IteratorAggregate, \Countable { /** * Storage driver. @@ -297,4 +297,24 @@ class Session implements SessionInterface { return $this->getBag('flashes')->clear(); } + + /** + * IteratorAggregate method for looping through the session attributes + * + * @return ArrayIterator + */ + public function getIterator() + { + return new \ArrayIterator($this->storage->getBag('attributes')->all()); + } + + /** + * Get the number of attributes in the session + * + * @return int Number of attributes + */ + public function count() + { + return count($this->storage->getBag('attributes')->all()); + } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/HeaderBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/HeaderBagTest.php index 229c00a451..c9f4b433df 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/HeaderBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/HeaderBagTest.php @@ -127,4 +127,32 @@ class HeaderBagTest extends \PHPUnit_Framework_TestCase $this->assertTrue($bag->hasCacheControlDirective('max-age')); $this->assertEquals(10, $bag->getCacheControlDirective('max-age')); } + + /** + * @covers Symfony\Component\HttpFoundation\HeaderBag::getIterator + */ + public function testGetIterator() + { + $headers = array('foo' => 'bar', 'hello' => 'world', 'third' => 'charm'); + $headerBag = new HeaderBag($headers); + + $i = 0; + foreach ($headerBag as $key => $val) { + $i++; + $this->assertEquals(array($headers[$key]), $val); + } + + $this->assertEquals(count($headers), $i); + } + + /** + * @covers Symfony\Component\HttpFoundation\HeaderBag::count + */ + public function testCount() + { + $headers = array('foo' => 'bar', 'HELLO' => 'WORLD'); + $headerBag = new HeaderBag($headers); + + $this->assertEquals(count($headers), count($headerBag)); + } } 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)); + } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Attribute/AttributeBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Attribute/AttributeBagTest.php index 5bf62110d9..8a937db23d 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Attribute/AttributeBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Attribute/AttributeBagTest.php @@ -159,4 +159,26 @@ class AttributeBagTest extends \PHPUnit_Framework_TestCase array('bye/for/now', null, false), ); } + + /** + * @covers Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag::getIterator + */ + public function testGetIterator() + { + $i = 0; + foreach ($this->bag as $key => $val) { + $this->assertEquals($this->array[$key], $val); + $i++; + } + + $this->assertEquals(count($this->array), $i); + } + + /** + * @covers Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag::count + */ + public function testCount() + { + $this->assertEquals(count($this->array), count($this->bag)); + } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php index 1ae73f8f0a..57fd9c4c9a 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php @@ -132,4 +132,37 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase ), $this->bag->peekAll() ); } + + /** + * @covers Symfony\Component\HttpFoundation\Session\Flash\FlashBag::count + */ + public function testCount() + { + $flashes = array('hello' => 'world', 'beep' => 'boop', 'notice' => 'nope'); + foreach ($flashes as $key => $val) { + $this->bag->set($key, $val); + } + + $this->assertEquals(count($flashes), count($this->bag)); + } + + /** + * @covers Symfony\Component\HttpFoundation\Session\Flash\FlashBag::getIterator + */ + public function testGetIterator() + { + $flashes = array('hello' => 'world', 'beep' => 'boop', 'notice' => 'nope'); + foreach ($flashes as $key => $val) { + $this->bag->set($key, $val); + } + + $i = 0; + foreach ($this->bag as $key => $val) { + $this->assertEquals($flashes[$key], $val); + $i++; + } + + $this->assertEquals(count($flashes), $i); + $this->assertEquals(0, count($this->bag)); + } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php index 9141c79fb9..8da6885b26 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php @@ -215,4 +215,34 @@ class SessionTest extends \PHPUnit_Framework_TestCase $this->assertFalse($this->session->hasFlash('notice')); $this->assertFalse($this->session->hasFlash('error')); } + + /** + * @covers Symfony\Component\HttpFoundation\Session\Session::getIterator + */ + public function testGetIterator() + { + $attributes = array('hello' => 'world', 'symfony2' => 'rocks'); + foreach ($attributes as $key => $val) { + $this->session->set($key, $val); + } + + $i = 0; + foreach ($this->session as $key => $val) { + $this->assertEquals($attributes[$key], $val); + $i++; + } + + $this->assertEquals(count($attributes), $i); + } + + /** + * @covers Symfony\Component\HttpFoundation\Session\Session::count + */ + public function testGetCount() + { + $this->session->set('hello', 'world'); + $this->session->set('symfony2', 'rocks'); + + $this->assertEquals(2, count($this->session)); + } }