Skip to content

Commit 09b4dab

Browse files
committed
Creating an abstraction on top of InnerResultIterator
Migrates the EmptyInnerResultIterator out of the class InnerResultIterator. In the future, we will be able to create a "StaticInnerResultIterator" containing a static array.
1 parent 3b7d130 commit 09b4dab

8 files changed

+233
-38
lines changed

src/AlterableResultIterator.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class AlterableResultIterator implements Result, \ArrayAccess, \JsonSerializable
1717
{
1818
/**
19-
* @var \Iterator|null
19+
* @var \Traversable|null
2020
*/
2121
private $resultIterator;
2222

@@ -37,9 +37,9 @@ class AlterableResultIterator implements Result, \ArrayAccess, \JsonSerializable
3737
private $resultArray;
3838

3939
/**
40-
* @param \Iterator|null $resultIterator
40+
* @param \Traversable|null $resultIterator
4141
*/
42-
public function __construct(\Iterator $resultIterator = null)
42+
public function __construct(\Traversable $resultIterator = null)
4343
{
4444
$this->resultIterator = $resultIterator;
4545
$this->alterations = new \SplObjectStorage();
@@ -48,9 +48,9 @@ public function __construct(\Iterator $resultIterator = null)
4848
/**
4949
* Sets a new iterator as the base iterator to be altered.
5050
*
51-
* @param \Iterator $resultIterator
51+
* @param \Traversable $resultIterator
5252
*/
53-
public function setResultIterator(\Iterator $resultIterator): void
53+
public function setResultIterator(\Traversable $resultIterator): void
5454
{
5555
$this->resultIterator = $resultIterator;
5656
$this->resultArray = null;
@@ -59,9 +59,9 @@ public function setResultIterator(\Iterator $resultIterator): void
5959
/**
6060
* Returns the non altered result iterator (or null if none exist).
6161
*
62-
* @return \Iterator|null
62+
* @return \Traversable|null
6363
*/
64-
public function getUnderlyingResultIterator(): ?\Iterator
64+
public function getUnderlyingResultIterator(): ?\Traversable
6565
{
6666
return $this->resultIterator;
6767
}
@@ -231,7 +231,7 @@ public function count()
231231
/**
232232
* Return an iterator over all results of the paginatable.
233233
*
234-
* @return \Iterator
234+
* @return \Traversable
235235
*/
236236
public function getIterator()
237237
{

src/EmptyInnerResultIterator.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\TDBM;
5+
6+
use Iterator;
7+
8+
/**
9+
* @internal
10+
*/
11+
class EmptyInnerResultIterator implements Iterator, InnerResultIteratorInterface
12+
{
13+
14+
/**
15+
* Return the current element
16+
* @link https://php.net/manual/en/iterator.current.php
17+
* @return mixed Can return any type.
18+
* @since 5.0.0
19+
*/
20+
public function current()
21+
{
22+
return null;
23+
}
24+
25+
/**
26+
* Move forward to next element
27+
* @link https://php.net/manual/en/iterator.next.php
28+
* @return void Any returned value is ignored.
29+
* @since 5.0.0
30+
*/
31+
public function next()
32+
{
33+
}
34+
35+
/**
36+
* Return the key of the current element
37+
* @link https://php.net/manual/en/iterator.key.php
38+
* @return mixed scalar on success, or null on failure.
39+
* @since 5.0.0
40+
*/
41+
public function key()
42+
{
43+
return null;
44+
}
45+
46+
/**
47+
* Checks if current position is valid
48+
* @link https://php.net/manual/en/iterator.valid.php
49+
* @return boolean The return value will be casted to boolean and then evaluated.
50+
* Returns true on success or false on failure.
51+
* @since 5.0.0
52+
*/
53+
public function valid()
54+
{
55+
return false;
56+
}
57+
58+
/**
59+
* Rewind the Iterator to the first element
60+
* @link https://php.net/manual/en/iterator.rewind.php
61+
* @return void Any returned value is ignored.
62+
* @since 5.0.0
63+
*/
64+
public function rewind()
65+
{
66+
}
67+
68+
/**
69+
* Whether a offset exists
70+
* @link https://php.net/manual/en/arrayaccess.offsetexists.php
71+
* @param mixed $offset <p>
72+
* An offset to check for.
73+
* </p>
74+
* @return boolean true on success or false on failure.
75+
* </p>
76+
* <p>
77+
* The return value will be casted to boolean if non-boolean was returned.
78+
* @since 5.0.0
79+
*/
80+
public function offsetExists($offset)
81+
{
82+
return false;
83+
}
84+
85+
/**
86+
* Offset to retrieve
87+
* @link https://php.net/manual/en/arrayaccess.offsetget.php
88+
* @param mixed $offset <p>
89+
* The offset to retrieve.
90+
* </p>
91+
* @return mixed Can return all value types.
92+
* @since 5.0.0
93+
*/
94+
public function offsetGet($offset)
95+
{
96+
throw new TDBMInvalidOffsetException('Offset "'.$offset.'" does not exist in result set.');
97+
}
98+
99+
/**
100+
* Offset to set
101+
* @link https://php.net/manual/en/arrayaccess.offsetset.php
102+
* @param mixed $offset <p>
103+
* The offset to assign the value to.
104+
* </p>
105+
* @param mixed $value <p>
106+
* The value to set.
107+
* </p>
108+
* @return void
109+
* @since 5.0.0
110+
*/
111+
public function offsetSet($offset, $value)
112+
{
113+
throw new TDBMInvalidOperationException('You cannot set values in a TDBM result set.');
114+
}
115+
116+
/**
117+
* Offset to unset
118+
* @link https://php.net/manual/en/arrayaccess.offsetunset.php
119+
* @param mixed $offset <p>
120+
* The offset to unset.
121+
* </p>
122+
* @return void
123+
* @since 5.0.0
124+
*/
125+
public function offsetUnset($offset)
126+
{
127+
throw new TDBMInvalidOperationException('You cannot unset values in a TDBM result set.');
128+
}
129+
130+
/**
131+
* Count elements of an object
132+
* @link https://php.net/manual/en/countable.count.php
133+
* @return int The custom count as an integer.
134+
* </p>
135+
* <p>
136+
* The return value is cast to an integer.
137+
* @since 5.1.0
138+
*/
139+
public function count()
140+
{
141+
return 0;
142+
}
143+
}

src/InnerResultIterator.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
/**
3232
* Iterator used to retrieve results.
3333
*/
34-
class InnerResultIterator implements \Iterator, \Countable, \ArrayAccess
34+
class InnerResultIterator implements \Iterator, InnerResultIteratorInterface
3535
{
3636
/**
3737
* @var Statement
@@ -258,9 +258,6 @@ public function next()
258258
*/
259259
public function rewind()
260260
{
261-
if ($this->count === 0) {
262-
return;
263-
}
264261
$this->executeQuery();
265262
$this->key = -1;
266263
$this->next();
@@ -272,9 +269,6 @@ public function rewind()
272269
*/
273270
public function valid()
274271
{
275-
if ($this->count === 0) {
276-
return false;
277-
}
278272
return $this->current !== null;
279273
}
280274

src/InnerResultIteratorInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\TDBM;
5+
6+
/**
7+
* Interface implemented by all "inner result iterators"
8+
*/
9+
interface InnerResultIteratorInterface extends \Traversable, \Countable, \ArrayAccess
10+
{
11+
}

src/MapIterator.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace TheCodingMachine\TDBM;
66

77
use Iterator;
8+
use IteratorAggregate;
9+
use Traversable;
810

911
/**
1012
* An iterator that maps element of another iterator by calling a callback on it.
@@ -22,7 +24,7 @@ class MapIterator implements Iterator, \JsonSerializable
2224
protected $callable;
2325

2426
/**
25-
* @param Iterator|array $iterator
27+
* @param Traversable|array $iterator
2628
* @param callable $callable This can have two parameters
2729
*
2830
* @throws TDBMException
@@ -31,10 +33,15 @@ public function __construct($iterator, callable $callable)
3133
{
3234
if (is_array($iterator)) {
3335
$this->iterator = new \ArrayIterator($iterator);
34-
} elseif (!($iterator instanceof Iterator)) {
35-
throw new TDBMException('$iterator parameter must be an instance of Iterator');
36-
} else {
36+
} elseif ($iterator instanceof Iterator) {
37+
$this->iterator = $iterator;
38+
} elseif ($iterator instanceof IteratorAggregate) {
39+
while (!$iterator instanceof Iterator) {
40+
$iterator = $iterator->getIterator();
41+
}
3742
$this->iterator = $iterator;
43+
} else {
44+
throw new TDBMException('$iterator parameter must be an instance of Iterator');
3845
}
3946

4047
if ($callable instanceof \Closure) {

src/PageIterator.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,13 @@ public static function createEmpyIterator(ResultIterator $parentResult): self
105105
/**
106106
* Retrieve an external iterator.
107107
*
108-
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
109-
*
110-
* @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or
111-
* <b>Traversable</b>
112-
*
113-
* @since 5.0.0
108+
* @return InnerResultIteratorInterface
114109
*/
115110
public function getIterator()
116111
{
117112
if ($this->innerResultIterator === null) {
118113
if ($this->parentResult->count() === 0) {
119-
$this->innerResultIterator = InnerResultIterator::createEmpyIterator();
114+
$this->innerResultIterator = new EmptyInnerResultIterator();
120115
} elseif ($this->mode === TDBMService::MODE_CURSOR) {
121116
$this->innerResultIterator = InnerResultIterator::createInnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
122117
} else {
@@ -251,7 +246,7 @@ public function offsetGet($offset)
251246
*/
252247
public function offsetSet($offset, $value)
253248
{
254-
return $this->getIterator()->offsetSet($offset, $value);
249+
$this->getIterator()->offsetSet($offset, $value);
255250
}
256251

257252
/**
@@ -267,7 +262,7 @@ public function offsetSet($offset, $value)
267262
*/
268263
public function offsetUnset($offset)
269264
{
270-
return $this->getIterator()->offsetUnset($offset);
265+
$this->getIterator()->offsetUnset($offset);
271266
}
272267

273268
/**

src/ResultIterator.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ResultIterator implements Result, \ArrayAccess, \JsonSerializable
6565
private $queryFactory;
6666

6767
/**
68-
* @var InnerResultIterator|null
68+
* @var InnerResultIteratorInterface|null
6969
*/
7070
private $innerResultIterator;
7171

@@ -160,18 +160,13 @@ public function map(callable $callable): MapIterator
160160
/**
161161
* Retrieve an external iterator.
162162
*
163-
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
164-
*
165-
* @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or
166-
* <b>Traversable</b>
167-
*
168-
* @since 5.0.0
163+
* @return InnerResultIteratorInterface
169164
*/
170165
public function getIterator()
171166
{
172167
if ($this->innerResultIterator === null) {
173168
if ($this->totalCount === 0) {
174-
$this->innerResultIterator = InnerResultArray::createEmpyIterator();
169+
$this->innerResultIterator = new EmptyInnerResultIterator();
175170
} elseif ($this->mode === TDBMService::MODE_CURSOR) {
176171
$this->innerResultIterator = InnerResultIterator::createInnerResultIterator($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
177172
} else {
@@ -251,7 +246,7 @@ public function offsetGet($offset)
251246
*/
252247
public function offsetSet($offset, $value)
253248
{
254-
return $this->getIterator()->offsetSet($offset, $value);
249+
$this->getIterator()->offsetSet($offset, $value);
255250
}
256251

257252
/**
@@ -267,7 +262,7 @@ public function offsetSet($offset, $value)
267262
*/
268263
public function offsetUnset($offset)
269264
{
270-
return $this->getIterator()->offsetUnset($offset);
265+
$this->getIterator()->offsetUnset($offset);
271266
}
272267

273268
/**

0 commit comments

Comments
 (0)