| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Orm; |
| 4 |
|
| 5 |
use ArrayIterator; |
| 6 |
use Generator; |
| 7 |
use Iterator; |
| 8 |
use Traversable; |
| 9 |
|
| 10 |
class ResultSet implements Iterator |
| 11 |
{ |
| 12 |
protected ArrayIterator $cache; |
| 13 |
|
| 14 |
/** @var bool Whether cache is disabled */ |
| 15 |
protected bool $isCacheDisabled = false; |
| 16 |
|
| 17 |
protected Generator $generator; |
| 18 |
|
| 19 |
protected ?int $limit; |
| 20 |
|
| 21 |
protected ?int $position = null; |
| 22 |
|
| 23 |
public function __construct(Traversable $traversable, ?int $limit = null) |
| 24 |
{ |
| 25 |
$this->cache = new ArrayIterator(); |
| 26 |
$this->generator = $this->yieldTraversable($traversable); |
| 27 |
$this->limit = $limit; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Create a new result set from the given query |
| 32 |
* |
| 33 |
* @param Query $query |
| 34 |
* |
| 35 |
* @return static |
| 36 |
*/ |
| 37 |
public static function fromQuery(Query $query) |
| 38 |
{ |
| 39 |
return new static($query->yieldResults(), $query->getLimit()); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Do not cache query result |
| 44 |
* |
| 45 |
* ResultSet instance can only be iterated once |
| 46 |
* |
| 47 |
* @return $this |
| 48 |
*/ |
| 49 |
public function disableCache(): static |
| 50 |
{ |
| 51 |
$this->isCacheDisabled = true; |
| 52 |
|
| 53 |
return $this; |
| 54 |
} |
| 55 |
|
| 56 |
public function hasMore(): bool |
| 57 |
{ |
| 58 |
return $this->generator->valid(); |
| 59 |
} |
| 60 |
|
| 61 |
public function hasResult(): bool |
| 62 |
{ |
| 63 |
return $this->generator->valid(); |
| 64 |
} |
| 65 |
|
| 66 |
#[\ReturnTypeWillChange] |
| 67 |
public function current() |
| 68 |
{ |
| 69 |
if ($this->position === null) { |
| 70 |
$this->advance(); |
| 71 |
} |
| 72 |
|
| 73 |
return $this->isCacheDisabled ? $this->generator->current() : $this->cache->current(); |
| 74 |
} |
| 75 |
|
| 76 |
public function next(): void |
| 77 |
{ |
| 78 |
if (! $this->isCacheDisabled) { |
| 79 |
$this->cache->next(); |
| 80 |
} |
| 81 |
|
| 82 |
if ($this->isCacheDisabled || ! $this->cache->valid()) { |
| 83 |
$this->generator->next(); |
| 84 |
$this->advance(); |
| 85 |
} else { |
| 86 |
$this->position += 1; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
public function key(): int |
| 91 |
{ |
| 92 |
if ($this->position === null) { |
| 93 |
$this->advance(); |
| 94 |
} |
| 95 |
|
| 96 |
return $this->isCacheDisabled ? $this->generator->key() : $this->cache->key(); |
| 97 |
} |
| 98 |
|
| 99 |
public function valid(): bool |
| 100 |
{ |
| 101 |
if ($this->limit !== null && $this->position === $this->limit) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
return $this->cache->valid() || $this->generator->valid(); |
| 106 |
} |
| 107 |
|
| 108 |
public function rewind(): void |
| 109 |
{ |
| 110 |
if (! $this->isCacheDisabled) { |
| 111 |
$this->cache->rewind(); |
| 112 |
} |
| 113 |
|
| 114 |
if ($this->position === null) { |
| 115 |
$this->advance(); |
| 116 |
} else { |
| 117 |
$this->position = 0; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
protected function advance(): void |
| 122 |
{ |
| 123 |
if (! $this->generator->valid()) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
if (! $this->isCacheDisabled) { |
| 128 |
$this->cache[$this->generator->key()] = $this->generator->current(); |
| 129 |
} |
| 130 |
|
| 131 |
if ($this->position === null) { |
| 132 |
$this->position = 0; |
| 133 |
} else { |
| 134 |
$this->position += 1; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
protected function yieldTraversable(Traversable $traversable) |
| 139 |
{ |
| 140 |
foreach ($traversable as $key => $value) { |
| 141 |
yield $key => $value; |
| 142 |
} |
| 143 |
} |
| 144 |
} |