merged branch cboden/interfaces (PR #3520)

Commits
-------

bd02554 [HttpFoundation] SPL IteratorAggregate+Countable on *Bags
665fdeb [HttpFoundation] SPL on ParameterBag

Discussion
----------

[HttpFoundation] SPL on ParameterBag

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes

Added a couple SPL interfaces to ParameterBag, added shortcuts to working with the parameters.  For example:

```php
<?php
    $post = Request::createFromGlobal()->request;
    echo "There are {count($post)} POST variables\n";

    foreach ($post as $key => $val) {
        echo "{$key}: {$val}\n";
    }
```

Thoughts?

---------------------------------------------------------------------------

by stealth35 at 2012-03-07T13:09:11Z

You already have the `all` method

``` php
<?php
$post = Request::createFromGlobals()->request->all();

echo "There are ", count($post), " POST variables\n";

foreach ($post as $key => $val) {
    echo "{$key}: {$val}\n";
}
```

---------------------------------------------------------------------------

by cboden at 2012-03-07T13:50:22Z

Yes, but when in the context of working with the Request object (or POST ParamegerBag), it's 1 more call and loose variable to set.

ParameterBag is a container, these common SPL interfaces give standard PHP container methods to it.

---------------------------------------------------------------------------

by lsmith77 at 2012-03-07T18:42:41Z

makes sense to me ..

---------------------------------------------------------------------------

by vicb at 2012-03-09T15:45:40Z

Probably makes sense. Could you check if any other `*Bag.php` needs to be updated so that it could ba an atomic merge.

---------------------------------------------------------------------------

by cboden at 2012-03-09T15:48:40Z

Whoops, good catch @vicb.  I made a poor assumption all the *Bags extended ParameterBag, while only some do.  I will post an update shortly.
This commit is contained in:
Fabien Potencier 2012-03-23 13:44:35 +01:00
commit 6381dbb8ed
10 changed files with 246 additions and 5 deletions

View File

@ -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();

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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());
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}