Skip to content
This repository
Browse code

[HttpFoundation] SPL IteratorAggregate+Countable on *Bags

Added the IteratorAggregate and Countable SPL Interfaces on all the *Bag classes in HttpFoundation
  • Loading branch information...
commit bd0255428960a92877dabf766a6b3c5a7c5b37d9 1 parent 665fdeb
Chris Boden authored March 15, 2012
22  src/Symfony/Component/HttpFoundation/HeaderBag.php
@@ -18,7 +18,7 @@
18 18
  *
19 19
  * @api
20 20
  */
21  
-class HeaderBag
  21
+class HeaderBag implements \IteratorAggregate, \Countable
22 22
 {
23 23
     protected $headers;
24 24
     protected $cacheControl;
@@ -267,6 +267,26 @@ public function removeCacheControlDirective($key)
267 267
         $this->set('Cache-Control', $this->getCacheControlHeader());
268 268
     }
269 269
 
  270
+    /**
  271
+     * IteratorAggregate method for looping the instance
  272
+     *
  273
+     * @return array An array of parameters
  274
+     */
  275
+    public function getIterator()
  276
+    {
  277
+        return new \ArrayIterator($this->headers);
  278
+    }
  279
+
  280
+    /**
  281
+     * Countable method returning the number of headers
  282
+     *
  283
+     * @return int Number of parameters held in the bag
  284
+     */
  285
+    public function count()
  286
+    {
  287
+        return count($this->headers);
  288
+    }
  289
+
270 290
     protected function getCacheControlHeader()
271 291
     {
272 292
         $parts = array();
2  src/Symfony/Component/HttpFoundation/ParameterBag.php
@@ -282,7 +282,7 @@ public function filter($key, $default = null, $deep = false, $filter=FILTER_DEFA
282 282
     /**
283 283
      * IteratorAggregate method for looping the instance
284 284
      *
285  
-     * @return array An array of parameters
  285
+     * @return ArrayIterator An array of parameters wrapped in an ArrayIterator
286 286
      */
287 287
     public function getIterator()
288 288
     {
22  src/Symfony/Component/HttpFoundation/Session/Attribute/AttributeBag.php
@@ -14,7 +14,7 @@
14 14
 /**
15 15
  * This class relates to session attribute storage
16 16
  */
17  
-class AttributeBag implements AttributeBagInterface
  17
+class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Countable
18 18
 {
19 19
     private $name = 'attributes';
20 20
 
@@ -134,4 +134,24 @@ public function clear()
134 134
 
135 135
         return $return;
136 136
     }
  137
+
  138
+    /**
  139
+     *
  140
+     *
  141
+     * @return ArrayIterator
  142
+     */
  143
+    public function getIterator()
  144
+    {
  145
+        return new \ArrayIterator($this->attributes);
  146
+    }
  147
+
  148
+    /**
  149
+     *
  150
+     *
  151
+     * @return int
  152
+     */
  153
+    public function count()
  154
+    {
  155
+        return count($this->attributes);
  156
+    }
137 157
 }
22  src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php
@@ -16,7 +16,7 @@
16 16
  *
17 17
  * @author Drak <drak@zikula.org>
18 18
  */
19  
-class FlashBag implements FlashBagInterface
  19
+class FlashBag implements FlashBagInterface, \IteratorAggregate, \Countable
20 20
 {
21 21
     private $name = 'flashes';
22 22
 
@@ -155,4 +155,24 @@ public function clear()
155 155
     {
156 156
         return $this->all();
157 157
     }
  158
+
  159
+    /**
  160
+     * IteratorAggregate 
  161
+     *
  162
+     * @return ArrayIterator
  163
+     */
  164
+    public function getIterator()
  165
+    {
  166
+        return new \ArrayIterator($this->all());
  167
+    }
  168
+
  169
+    /**
  170
+     * 
  171
+     *
  172
+     * @return int
  173
+     */
  174
+    public function count()
  175
+    {
  176
+        return count($this->flashes);
  177
+    }
158 178
 }
22  src/Symfony/Component/HttpFoundation/Session/Session.php
@@ -26,7 +26,7 @@
26 26
  *
27 27
  * @api
28 28
  */
29  
-class Session implements SessionInterface
  29
+class Session implements SessionInterface, \IteratorAggregate, \Countable
30 30
 {
31 31
     /**
32 32
      * Storage driver.
@@ -260,4 +260,24 @@ public function clearFlashes()
260 260
     {
261 261
        return $this->getBag('flashes')->clear();
262 262
     }
  263
+
  264
+    /**
  265
+     * IteratorAggregate method for looping through the session attributes
  266
+     *
  267
+     * @return ArrayIterator
  268
+     */
  269
+    public function getIterator()
  270
+    {
  271
+        return new \ArrayIterator($this->storage->getBag('attributes')->all());
  272
+    }
  273
+
  274
+    /**
  275
+     * Get the number of attributes in the session
  276
+     *
  277
+     * @return int Number of attributes
  278
+     */
  279
+    public function count()
  280
+    {
  281
+        return count($this->storage->getBag('attributes')->all());
  282
+    }
263 283
 }
28  tests/Symfony/Tests/Component/HttpFoundation/HeaderBagTest.php
@@ -127,4 +127,32 @@ public function testCacheControlDirectiveOverrideWithReplace()
127 127
         $this->assertTrue($bag->hasCacheControlDirective('max-age'));
128 128
         $this->assertEquals(10, $bag->getCacheControlDirective('max-age'));
129 129
     }
  130
+
  131
+    /**
  132
+     * @covers Symfony\Component\HttpFoundation\HeaderBag::getIterator
  133
+     */
  134
+    public function testGetIterator()
  135
+    {
  136
+        $headers   = array('foo' => 'bar', 'hello' => 'world', 'third' => 'charm');
  137
+        $headerBag = new HeaderBag($headers);
  138
+
  139
+        $i = 0;
  140
+        foreach ($headerBag as $key => $val) {
  141
+            $i++;
  142
+            $this->assertEquals(array($headers[$key]), $val);
  143
+        }
  144
+
  145
+        $this->assertEquals(count($headers), $i);
  146
+    }
  147
+
  148
+    /**
  149
+     * @covers Symfony\Component\HttpFoundation\HeaderBag::count
  150
+     */
  151
+    public function testCount()
  152
+    {
  153
+        $headers   = array('foo' => 'bar', 'HELLO' => 'WORLD');
  154
+        $headerBag = new HeaderBag($headers);
  155
+
  156
+        $this->assertEquals(count($headers), count($headerBag));
  157
+    }
130 158
 }
22  tests/Symfony/Tests/Component/HttpFoundation/Session/Attribute/AttributeBagTest.php
@@ -159,4 +159,26 @@ public function attributesProvider()
159 159
             array('bye/for/now', null, false),
160 160
         );
161 161
     }
  162
+
  163
+    /**
  164
+     * @covers Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag::getIterator
  165
+     */
  166
+    public function testGetIterator()
  167
+    {
  168
+        $i = 0;
  169
+        foreach ($this->bag as $key => $val) {
  170
+            $this->assertEquals($this->array[$key], $val);
  171
+            $i++;
  172
+        }
  173
+
  174
+        $this->assertEquals(count($this->array), $i);
  175
+    }
  176
+
  177
+    /**
  178
+     * @covers Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag::count
  179
+     */
  180
+    public function testCount()
  181
+    {
  182
+        $this->assertEquals(count($this->array), count($this->bag));
  183
+    }
162 184
 }
33  tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php
@@ -132,4 +132,37 @@ public function testPeekAll()
132 132
             ), $this->bag->peekAll()
133 133
         );
134 134
     }
  135
+
  136
+    /**
  137
+     * @covers Symfony\Component\HttpFoundation\Session\Flash\FlashBag::count
  138
+     */
  139
+    public function testCount()
  140
+    {
  141
+        $flashes = array('hello' => 'world', 'beep' => 'boop', 'notice' => 'nope');
  142
+        foreach ($flashes as $key => $val) {
  143
+            $this->bag->set($key, $val);
  144
+        }
  145
+
  146
+        $this->assertEquals(count($flashes), count($this->bag));
  147
+    }
  148
+
  149
+    /**
  150
+     * @covers Symfony\Component\HttpFoundation\Session\Flash\FlashBag::getIterator
  151
+     */
  152
+    public function testGetIterator()
  153
+    {
  154
+        $flashes = array('hello' => 'world', 'beep' => 'boop', 'notice' => 'nope');
  155
+        foreach ($flashes as $key => $val) {
  156
+            $this->bag->set($key, $val);
  157
+        }
  158
+
  159
+        $i = 0;
  160
+        foreach ($this->bag as $key => $val) {
  161
+            $this->assertEquals($flashes[$key], $val);
  162
+            $i++;
  163
+        }
  164
+
  165
+        $this->assertEquals(count($flashes), $i);
  166
+        $this->assertEquals(0, count($this->bag));
  167
+    }
135 168
 }
30  tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php
@@ -215,4 +215,34 @@ public function testClearFlashes()
215 215
         $this->assertFalse($this->session->hasFlash('notice'));
216 216
         $this->assertFalse($this->session->hasFlash('error'));
217 217
     }
  218
+
  219
+    /**
  220
+     * @covers Symfony\Component\HttpFoundation\Session\Session::getIterator
  221
+     */
  222
+    public function testGetIterator()
  223
+    {
  224
+        $attributes = array('hello' => 'world', 'symfony2' => 'rocks');
  225
+        foreach ($attributes as $key => $val) {
  226
+            $this->session->set($key, $val);
  227
+        }
  228
+
  229
+        $i = 0;
  230
+        foreach ($this->session as $key => $val) {
  231
+            $this->assertEquals($attributes[$key], $val);
  232
+            $i++;
  233
+        }
  234
+
  235
+        $this->assertEquals(count($attributes), $i);
  236
+    }
  237
+
  238
+    /**
  239
+     * @covers Symfony\Component\HttpFoundation\Session\Session::count
  240
+     */
  241
+    public function testGetCount()
  242
+    {
  243
+        $this->session->set('hello', 'world');
  244
+        $this->session->set('symfony2', 'rocks');
  245
+
  246
+        $this->assertEquals(2, count($this->session));
  247
+    }
218 248
 }

0 notes on commit bd02554

Please sign in to comment.
Something went wrong with that request. Please try again.