Permalink
Browse files

[HttpFoundation] SPL IteratorAggregate+Countable on *Bags

Added the IteratorAggregate and Countable SPL Interfaces on all the *Bag classes in HttpFoundation
  • Loading branch information...
1 parent 665fdeb commit bd0255428960a92877dabf766a6b3c5a7c5b37d9 @cboden cboden committed Mar 15, 2012
@@ -18,7 +18,7 @@
*
* @api
*/
-class HeaderBag
+class HeaderBag implements \IteratorAggregate, \Countable
{
protected $headers;
protected $cacheControl;
@@ -267,6 +267,26 @@ public function removeCacheControlDirective($key)
$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();
@@ -282,7 +282,7 @@ public function filter($key, $default = null, $deep = false, $filter=FILTER_DEFA
/**
* IteratorAggregate method for looping the instance
*
- * @return array An array of parameters
+ * @return ArrayIterator An array of parameters wrapped in an ArrayIterator
*/
public function getIterator()
{
@@ -14,7 +14,7 @@
/**
* This class relates to session attribute storage
*/
-class AttributeBag implements AttributeBagInterface
+class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Countable
{
private $name = 'attributes';
@@ -134,4 +134,24 @@ public function clear()
return $return;
}
+
+ /**
+ *
+ *
+ * @return ArrayIterator
+ */
+ public function getIterator()
+ {
+ return new \ArrayIterator($this->attributes);
+ }
+
+ /**
+ *
+ *
+ * @return int
+ */
+ public function count()
+ {
+ return count($this->attributes);
+ }
}
@@ -16,7 +16,7 @@
*
* @author Drak <drak@zikula.org>
*/
-class FlashBag implements FlashBagInterface
+class FlashBag implements FlashBagInterface, \IteratorAggregate, \Countable
{
private $name = 'flashes';
@@ -155,4 +155,24 @@ public function clear()
{
return $this->all();
}
+
+ /**
+ * IteratorAggregate
+ *
+ * @return ArrayIterator
+ */
+ public function getIterator()
+ {
+ return new \ArrayIterator($this->all());
+ }
+
+ /**
+ *
+ *
+ * @return int
+ */
+ public function count()
+ {
+ return count($this->flashes);
+ }
}
@@ -26,7 +26,7 @@
*
* @api
*/
-class Session implements SessionInterface
+class Session implements SessionInterface, \IteratorAggregate, \Countable
{
/**
* Storage driver.
@@ -260,4 +260,24 @@ public function clearFlashes()
{
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());
+ }
}
@@ -127,4 +127,32 @@ public function testCacheControlDirectiveOverrideWithReplace()
$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));
+ }
}
@@ -159,4 +159,26 @@ public function attributesProvider()
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));
+ }
}
@@ -132,4 +132,37 @@ public function testPeekAll()
), $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));
+ }
}
@@ -215,4 +215,34 @@ public function testClearFlashes()
$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));
+ }
}

0 comments on commit bd02554

Please sign in to comment.