File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33namespace Wikibase \DataModel \Statement ;
44
5+ use ArrayIterator ;
6+ use Countable ;
57use InvalidArgumentException ;
8+ use IteratorAggregate ;
9+ use Traversable ;
610
711/**
812 * Ordered and unique collection of Statement objects.
1418 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
1519 * @author Kai Nissen < kai.nissen@wikimedia.de >
1620 */
17- class StatementByGuidMap implements \ Countable {
21+ class StatementByGuidMap implements IteratorAggregate, Countable {
1822
1923 private $ statements = array ();
2024
@@ -86,4 +90,12 @@ public function count() {
8690 return count ( $ this ->statements );
8791 }
8892
93+ /**
94+ * @see IteratorAggregate::getIterator
95+ * @return Traversable
96+ */
97+ public function getIterator () {
98+ return new ArrayIterator ( $ this ->statements );
99+ }
100+
89101}
Original file line number Diff line number Diff line change @@ -141,4 +141,24 @@ public function testMapCanBePassedToCount() {
141141 $ this ->assertSame ( 2 , count ( $ statements ) );
142142 }
143143
144+ public function testMapCanBeIteratedOver () {
145+ $ statement1 = $ this ->newStatement ( 1 , 'some guid ' );
146+ $ statement2 = $ this ->newStatement ( 2 , 'other guid ' );
147+
148+ $ statementMap = new StatementByGuidMap ( array ( $ statement1 , $ statement2 ) );
149+
150+ $ iteratedStatements = array ();
151+
152+ foreach ( $ statementMap as $ guid => $ statement ) {
153+ $ iteratedStatements [$ guid ] = $ statement ;
154+ }
155+
156+ $ expectedStatements = array (
157+ 'some guid ' => $ statement1 ,
158+ 'other guid ' => $ statement2
159+ );
160+
161+ $ this ->assertEquals ( $ expectedStatements , $ iteratedStatements );
162+ }
163+
144164}
You can’t perform that action at this time.
0 commit comments