Skip to content

Commit 36c690d

Browse files
authored
Merge pull request #179 from moufmouf/inneriteratorinterface
Creating an abstraction on top of InnerResultIterator
2 parents 3b7d130 + fd65393 commit 36c690d

10 files changed

+256
-52
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/InnerResultArray.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ class InnerResultArray extends InnerResultIterator
5353
*/
5454
public function offsetExists($offset)
5555
{
56-
if ($this->count === 0) {
57-
return false;
58-
}
5956
try {
6057
$this->toIndex($offset);
6158
} catch (TDBMInvalidOffsetException $e) {
@@ -125,9 +122,6 @@ public function next()
125122
*/
126123
public function rewind()
127124
{
128-
if ($this->count === 0) {
129-
return;
130-
}
131125
if (!$this->fetchStarted) {
132126
$this->executeQuery();
133127
}

src/InnerResultIterator.php

Lines changed: 1 addition & 15 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
@@ -93,14 +93,6 @@ public static function createInnerResultIterator(string $magicSql, array $parame
9393
return $iterator;
9494
}
9595

96-
public static function createEmpyIterator(): self
97-
{
98-
$iterator = new static();
99-
$iterator->count = 0;
100-
$iterator->logger = new NullLogger();
101-
return $iterator;
102-
}
103-
10496
private function getQuery(): string
10597
{
10698
$sql = $this->magicQuery->buildPreparedStatement($this->magicSql, $this->parameters);
@@ -258,9 +250,6 @@ public function next()
258250
*/
259251
public function rewind()
260252
{
261-
if ($this->count === 0) {
262-
return;
263-
}
264253
$this->executeQuery();
265254
$this->key = -1;
266255
$this->next();
@@ -272,9 +261,6 @@ public function rewind()
272261
*/
273262
public function valid()
274263
{
275-
if ($this->count === 0) {
276-
return false;
277-
}
278264
return $this->current !== null;
279265
}
280266

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)