Skip to content

Commit 7022029

Browse files
committed
MockEntityList is now iteratable
1 parent 9d3e7a3 commit 7022029

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

core/mock/MockEntityList.php

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
use ArrayAccess;
66
use BadMethodCallException;
77
use Countable;
8+
use Iterator;
89
use PPA\core\Entity;
910
use PPA\core\EntityProperty;
1011
use 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
}

examples/playground.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212
echo $result[0]->getRole()->getName();
1313
$result = $result[0]->getRole();
1414

15-
//echo count($result->getRights());
1615

17-
var_dump(isset($result->getRights()[0]));
16+
echo $result->getRights()->func();
1817

19-
\PPA\prettyDump($result->getRights()[0]);
18+
foreach ($result->getRights() as $right) {
19+
PPA\prettyDump($result->getRights());
20+
\PPA\prettyDump($right);
21+
}
22+
23+
//\PPA\prettyDump($result->getRights()[0]);
2024

2125

2226
//echo $result;
2327
//
24-
\PPA\prettyDump($result);
28+
//\PPA\prettyDump($result);
2529

2630
//$query = new PPA\core\query\TypedQuery("select * from `role` ", "\\PPA\\examples\\entity\\Role");
2731
//\PPA\prettyDump($query->getResultList());

0 commit comments

Comments
 (0)