merged branch mtdowling/event_dispatcher_add_iterator (PR #5268)

Commits
-------

0ad00f8 [EventDispatcher] Adding IteratorAggregate to GenericEvent

Discussion
----------

[EventDispatcher] Adding IteratorAggregate to GenericEvent

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

by drak at 2012-08-16T07:43:29Z

What is the use case for this that it should be part of the Generic event?

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

by mtdowling at 2012-08-16T17:12:28Z

This allows for the GenericEvent to be even more generic. Now listeners don't need to know an exact key from the arguments, but rather can iterate over the arguments to find what they are looking for. This makes the GenericEvent more like an array.

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

by mtdowling at 2012-08-17T19:31:04Z

How would this be a nasty break? It's just giving the GenericEvent more capabilities with IteratorAggregate.

This is a completely separate PR from the one that flipped the constructor args.

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

by schmittjoh at 2012-08-17T19:34:47Z

Why are you not just doing ``foreach ($event->getArguments() as $arg) { /** ... */ }``?

If you just have ``foreach ($event)``, to me at least it would not be so clear what we are actually iterating over.

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

by mtdowling at 2012-08-17T19:39:23Z

This class already has ArrayAccess. If you're already using this class like an array, then I think you should expect to be able to iterate it like an array. I'm just finishing that concept off by implementing IteratorAggregate.

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

by schmittjoh at 2012-08-17T19:47:43Z

Indeed, if we already have ArrayAccess which we probably don't want to remove again, then that seems reasonable.
This commit is contained in:
Fabien Potencier 2012-08-18 10:28:12 +02:00
commit 7fe18d13a0
2 changed files with 20 additions and 1 deletions

View File

@ -18,7 +18,7 @@ namespace Symfony\Component\EventDispatcher;
*
* @author Drak <drak@zikula.org>
*/
class GenericEvent extends Event implements \ArrayAccess
class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
{
/**
* Observer pattern subject.
@ -177,4 +177,14 @@ class GenericEvent extends Event implements \ArrayAccess
{
return $this->hasArgument($key);
}
/**
* IteratorAggregate for iterating over the object like an array
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->arguments);
}
}

View File

@ -128,4 +128,13 @@ class GenericEventTest extends \PHPUnit_Framework_TestCase
{
$this->assertSame($this->subject, $this->event->getSubject());
}
public function testHasIterator()
{
$data = array();
foreach ($this->event as $key => $value) {
$data[$key] = $value;
}
$this->assertEquals(array('name' => 'Event'), $data);
}
}