Skip to content

Commit

Permalink
Merge fa943b7 into 3b7d130
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Sep 30, 2019
2 parents 3b7d130 + fa943b7 commit 07dafba
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 52 deletions.
16 changes: 8 additions & 8 deletions src/AlterableResultIterator.php
Expand Up @@ -16,7 +16,7 @@
class AlterableResultIterator implements Result, \ArrayAccess, \JsonSerializable
{
/**
* @var \Iterator|null
* @var \Traversable|null
*/
private $resultIterator;

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

/**
* @param \Iterator|null $resultIterator
* @param \Traversable|null $resultIterator
*/
public function __construct(\Iterator $resultIterator = null)
public function __construct(\Traversable $resultIterator = null)
{
$this->resultIterator = $resultIterator;
$this->alterations = new \SplObjectStorage();
Expand All @@ -48,9 +48,9 @@ public function __construct(\Iterator $resultIterator = null)
/**
* Sets a new iterator as the base iterator to be altered.
*
* @param \Iterator $resultIterator
* @param \Traversable $resultIterator
*/
public function setResultIterator(\Iterator $resultIterator): void
public function setResultIterator(\Traversable $resultIterator): void
{
$this->resultIterator = $resultIterator;
$this->resultArray = null;
Expand All @@ -59,9 +59,9 @@ public function setResultIterator(\Iterator $resultIterator): void
/**
* Returns the non altered result iterator (or null if none exist).
*
* @return \Iterator|null
* @return \Traversable|null
*/
public function getUnderlyingResultIterator(): ?\Iterator
public function getUnderlyingResultIterator(): ?\Traversable
{
return $this->resultIterator;
}
Expand Down Expand Up @@ -231,7 +231,7 @@ public function count()
/**
* Return an iterator over all results of the paginatable.
*
* @return \Iterator
* @return \Traversable
*/
public function getIterator()
{
Expand Down
143 changes: 143 additions & 0 deletions src/EmptyInnerResultIterator.php
@@ -0,0 +1,143 @@
<?php


namespace TheCodingMachine\TDBM;

use Iterator;

/**
* @internal
*/
class EmptyInnerResultIterator implements Iterator, InnerResultIteratorInterface
{

/**
* Return the current element
* @link https://php.net/manual/en/iterator.current.php
* @return mixed Can return any type.
* @since 5.0.0
*/
public function current()
{
return null;
}

/**
* Move forward to next element
* @link https://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored.
* @since 5.0.0
*/
public function next()
{
}

/**
* Return the key of the current element
* @link https://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure.
* @since 5.0.0
*/
public function key()
{
return null;
}

/**
* Checks if current position is valid
* @link https://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
* @since 5.0.0
*/
public function valid()
{
return false;
}

/**
* Rewind the Iterator to the first element
* @link https://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
* @since 5.0.0
*/
public function rewind()
{
}

/**
* Whether a offset exists
* @link https://php.net/manual/en/arrayaccess.offsetexists.php
* @param mixed $offset <p>
* An offset to check for.
* </p>
* @return boolean true on success or false on failure.
* </p>
* <p>
* The return value will be casted to boolean if non-boolean was returned.
* @since 5.0.0
*/
public function offsetExists($offset)
{
return false;
}

/**
* Offset to retrieve
* @link https://php.net/manual/en/arrayaccess.offsetget.php
* @param mixed $offset <p>
* The offset to retrieve.
* </p>
* @return mixed Can return all value types.
* @since 5.0.0
*/
public function offsetGet($offset)
{
throw new TDBMInvalidOffsetException('Offset "'.$offset.'" does not exist in result set.');
}

/**
* Offset to set
* @link https://php.net/manual/en/arrayaccess.offsetset.php
* @param mixed $offset <p>
* The offset to assign the value to.
* </p>
* @param mixed $value <p>
* The value to set.
* </p>
* @return void
* @since 5.0.0
*/
public function offsetSet($offset, $value)
{
throw new TDBMInvalidOperationException('You cannot set values in a TDBM result set.');
}

/**
* Offset to unset
* @link https://php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset <p>
* The offset to unset.
* </p>
* @return void
* @since 5.0.0
*/
public function offsetUnset($offset)
{
throw new TDBMInvalidOperationException('You cannot unset values in a TDBM result set.');
}

/**
* Count elements of an object
* @link https://php.net/manual/en/countable.count.php
* @return int The custom count as an integer.
* </p>
* <p>
* The return value is cast to an integer.
* @since 5.1.0
*/
public function count()
{
return 0;
}
}
6 changes: 0 additions & 6 deletions src/InnerResultArray.php
Expand Up @@ -53,9 +53,6 @@ class InnerResultArray extends InnerResultIterator
*/
public function offsetExists($offset)
{
if ($this->count === 0) {
return false;
}
try {
$this->toIndex($offset);
} catch (TDBMInvalidOffsetException $e) {
Expand Down Expand Up @@ -125,9 +122,6 @@ public function next()
*/
public function rewind()
{
if ($this->count === 0) {
return;
}
if (!$this->fetchStarted) {
$this->executeQuery();
}
Expand Down
16 changes: 1 addition & 15 deletions src/InnerResultIterator.php
Expand Up @@ -31,7 +31,7 @@
/**
* Iterator used to retrieve results.
*/
class InnerResultIterator implements \Iterator, \Countable, \ArrayAccess
class InnerResultIterator implements \Iterator, InnerResultIteratorInterface
{
/**
* @var Statement
Expand Down Expand Up @@ -93,14 +93,6 @@ public static function createInnerResultIterator(string $magicSql, array $parame
return $iterator;
}

public static function createEmpyIterator(): self
{
$iterator = new static();
$iterator->count = 0;
$iterator->logger = new NullLogger();
return $iterator;
}

private function getQuery(): string
{
$sql = $this->magicQuery->buildPreparedStatement($this->magicSql, $this->parameters);
Expand Down Expand Up @@ -258,9 +250,6 @@ public function next()
*/
public function rewind()
{
if ($this->count === 0) {
return;
}
$this->executeQuery();
$this->key = -1;
$this->next();
Expand All @@ -272,9 +261,6 @@ public function rewind()
*/
public function valid()
{
if ($this->count === 0) {
return false;
}
return $this->current !== null;
}

Expand Down
11 changes: 11 additions & 0 deletions src/InnerResultIteratorInterface.php
@@ -0,0 +1,11 @@
<?php


namespace TheCodingMachine\TDBM;

/**
* Interface implemented by all "inner result iterators"
*/
interface InnerResultIteratorInterface extends \Traversable, \Countable, \ArrayAccess
{
}
15 changes: 11 additions & 4 deletions src/MapIterator.php
Expand Up @@ -5,6 +5,8 @@
namespace TheCodingMachine\TDBM;

use Iterator;
use IteratorAggregate;
use Traversable;

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

/**
* @param Iterator|array $iterator
* @param Traversable|array $iterator
* @param callable $callable This can have two parameters
*
* @throws TDBMException
Expand All @@ -31,10 +33,15 @@ public function __construct($iterator, callable $callable)
{
if (is_array($iterator)) {
$this->iterator = new \ArrayIterator($iterator);
} elseif (!($iterator instanceof Iterator)) {
throw new TDBMException('$iterator parameter must be an instance of Iterator');
} else {
} elseif ($iterator instanceof Iterator) {
$this->iterator = $iterator;
} elseif ($iterator instanceof IteratorAggregate) {
while (!$iterator instanceof Iterator) {
$iterator = $iterator->getIterator();
}
$this->iterator = $iterator;
} else {
throw new TDBMException('$iterator parameter must be an instance of Iterator');
}

if ($callable instanceof \Closure) {
Expand Down
13 changes: 4 additions & 9 deletions src/PageIterator.php
Expand Up @@ -105,18 +105,13 @@ public static function createEmpyIterator(ResultIterator $parentResult): self
/**
* Retrieve an external iterator.
*
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
*
* @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or
* <b>Traversable</b>
*
* @since 5.0.0
* @return InnerResultIteratorInterface
*/
public function getIterator()
{
if ($this->innerResultIterator === null) {
if ($this->parentResult->count() === 0) {
$this->innerResultIterator = InnerResultIterator::createEmpyIterator();
$this->innerResultIterator = new EmptyInnerResultIterator();
} elseif ($this->mode === TDBMService::MODE_CURSOR) {
$this->innerResultIterator = InnerResultIterator::createInnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
} else {
Expand Down Expand Up @@ -251,7 +246,7 @@ public function offsetGet($offset)
*/
public function offsetSet($offset, $value)
{
return $this->getIterator()->offsetSet($offset, $value);
$this->getIterator()->offsetSet($offset, $value);
}

/**
Expand All @@ -267,7 +262,7 @@ public function offsetSet($offset, $value)
*/
public function offsetUnset($offset)
{
return $this->getIterator()->offsetUnset($offset);
$this->getIterator()->offsetUnset($offset);
}

/**
Expand Down
15 changes: 5 additions & 10 deletions src/ResultIterator.php
Expand Up @@ -65,7 +65,7 @@ class ResultIterator implements Result, \ArrayAccess, \JsonSerializable
private $queryFactory;

/**
* @var InnerResultIterator|null
* @var InnerResultIteratorInterface|null
*/
private $innerResultIterator;

Expand Down Expand Up @@ -160,18 +160,13 @@ public function map(callable $callable): MapIterator
/**
* Retrieve an external iterator.
*
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
*
* @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or
* <b>Traversable</b>
*
* @since 5.0.0
* @return InnerResultIteratorInterface
*/
public function getIterator()
{
if ($this->innerResultIterator === null) {
if ($this->totalCount === 0) {
$this->innerResultIterator = InnerResultArray::createEmpyIterator();
$this->innerResultIterator = new EmptyInnerResultIterator();
} elseif ($this->mode === TDBMService::MODE_CURSOR) {
$this->innerResultIterator = InnerResultIterator::createInnerResultIterator($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
} else {
Expand Down Expand Up @@ -251,7 +246,7 @@ public function offsetGet($offset)
*/
public function offsetSet($offset, $value)
{
return $this->getIterator()->offsetSet($offset, $value);
$this->getIterator()->offsetSet($offset, $value);
}

/**
Expand All @@ -267,7 +262,7 @@ public function offsetSet($offset, $value)
*/
public function offsetUnset($offset)
{
return $this->getIterator()->offsetUnset($offset);
$this->getIterator()->offsetUnset($offset);
}

/**
Expand Down

0 comments on commit 07dafba

Please sign in to comment.