[HttpFoundation] SPL IteratorAggregate+Countable on *Bags
Added the IteratorAggregate and Countable SPL Interfaces on all the *Bag classes in HttpFoundation
This commit is contained in:
parent
665fdebc8c
commit
bd02554289
@ -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();
|
||||
|
@ -282,7 +282,7 @@ class ParameterBag implements \IteratorAggregate, \Countable
|
||||
/**
|
||||
* 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 @@ 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);
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ namespace Symfony\Component\HttpFoundation\Session\Flash;
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class Session implements SessionInterface
|
||||
class Session implements SessionInterface, \IteratorAggregate, \Countable
|
||||
{
|
||||
/**
|
||||
* Storage driver.
|
||||
@ -260,4 +260,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());
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user