8000 merged branch mtdowling/event_dispatcher_add_iterator (PR #5268) · symfony/symfony@7fe18d1 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 7fe18d1

Browse files
committed
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.
2 parents 1f37191 + 0ad00f8 commit 7fe18d1

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Symfony/Component/EventDispatcher/GenericEvent.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Drak <drak@zikula.org>
2020
*/
21-
class GenericEvent extends Event implements \ArrayAccess
21+
class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
2222
{
2323
/**
2424
* Observer pattern subject.
@@ -177,4 +177,14 @@ public function offsetExists($key)
177177
{
178178
return $this->hasArgument($key);
179179
}
180+
181+
/**
182+
* IteratorAggregate for iterating over the object like an array
183+
*
184+
* @return \ArrayIterator
185+
*/
186+
public function getIterator()
187+
{
188+
return new \ArrayIterator($this->arguments);
189+
}
180190
}

src/Symfony/Component/EventDispatcher/Tests/GenericEventTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,13 @@ public function testGetSubject()
128128
{
129129
$this->assertSame($this->subject, $this->event->getSubject());
130130
}
131+
132+
public function testHasIterator()
133+
{
134+
$data = array();
135+
foreach ($this->event as $key => $value) {
136+
$data[$key] = $value;
137+
}
138+
$this->assertEquals(array('name' => 'Event'), $data);
139+
}
131140
}

0 commit comments

Comments
 (0)
0