55use ArrayAccess ;
66use BadMethodCallException ;
77use Countable ;
8+ use Iterator ;
89use PPA \core \Entity ;
910use PPA \core \EntityProperty ;
1011use PPA \core \query \TypedQuery ;
1112
1213
13- class MockEntityList extends MockEntity implements ArrayAccess, Countable {
14+ class MockEntityList extends MockEntity implements ArrayAccess, Countable, Iterator {
15+
16+ private $ entities ;
1417
1518 /**
1619 * The MockEntity serves as replacement for a real Entity. On method calls
@@ -27,37 +30,38 @@ public function __construct(TypedQuery $query, Entity $owner, EntityProperty $pr
2730 }
2831
2932 /**
30- * It is necessary to find out, if this method is needed!!
33+ * This method will be called, when no other method applies to $this.
3134 *
3235 * @param string $name
3336 * @param array $arguments
34- * @return mixed The value, the entity method should return.
37+ * @return mixed The value, the function of the internal array should return.
3538 * @throws BadMethodCallException If method does not exist.
3639 */
3740 public function __call ($ name , $ arguments ) {
38- $ entities = $ this ->exchange ();
41+ $ this ->exchange ();
3942
40- if (method_exists ($ entities , $ name )) {
41- return call_user_func (array ($ entities , $ name ), $ arguments );
43+ if (method_exists ($ this -> entities , $ name )) {
44+ return call_user_func (array ($ this -> entities , $ name ), $ arguments );
4245 } else {
43- throw new BadMethodCallException ("Method ' {$ name }()' does not exist in class ' " . get_class ( $ entities ) . " ' . " );
46+ throw new BadMethodCallException ("Method ' {$ name }()' cannot be called on an Array . " );
4447 }
4548 }
4649
4750 protected function exchange () {
48- $ entities = $ this ->query -> getResultList ();
49- $ this ->property -> setValue ( $ this ->owner , $ entities );
50-
51- return $ entities ;
51+ if ( $ this ->entities == null ) {
52+ $ this ->entities = $ this ->query -> getResultList ( );
53+ $ this -> property -> setValue ( $ this -> owner , $ this -> entities );
54+ }
5255 }
5356
54-
5557 public function offsetExists ($ offset ) {
56- return isset ($ this ->exchange ()[$ offset ]);
58+ $ this ->exchange ();
59+ return isset ($ this ->entities [$ offset ]);
5760 }
5861
5962 public function offsetGet ($ offset ) {
60- return $ this ->exchange ()[$ offset ];
63+ $ this ->exchange ();
64+ return $ this ->entities [$ offset ];
6165 }
6266
6367 public function offsetSet ($ offset , $ value ) {
@@ -69,7 +73,34 @@ public function offsetUnset($offset) {
6973 }
7074
7175 public function count () {
72- return count ($ this ->exchange ());
76+ $ this ->exchange ();
77+ return count ($ this ->entities );
78+ }
79+
80+ public function current () {
81+ $ this ->exchange ();
82+ return current ($ this ->entities );
83+ }
84+
85+ public function key () {
86+ $ this ->exchange ();
87+ return key ($ this ->entities );
88+ }
89+
90+ public function next () {
91+ $ this ->exchange ();
92+ return next ($ this ->entities );
93+ }
94+
95+ public function rewind () {
96+ $ this ->exchange ();
97+ reset ($ this ->entities );
98+ }
99+
100+ public function valid () {
101+ $ key = $ this ->key ();
102+ $ var = ($ key !== null && $ key !== false );
103+ return $ var ;
73104 }
74105
75106}
0 commit comments