1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Cycle\DataReader; |
6
|
|
|
|
7
|
|
|
use Countable; |
8
|
|
|
use Cycle\ORM\Select; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use Spiral\Database\Query\QueryInterface; |
11
|
|
|
use Spiral\Database\Query\SelectQuery; |
12
|
|
|
use Spiral\Pagination\PaginableInterface; |
13
|
|
|
use Yiisoft\Data\Reader\CountableDataInterface; |
14
|
|
|
use Yiisoft\Data\Reader\DataReaderInterface; |
15
|
|
|
use Yiisoft\Data\Reader\OffsetableDataInterface; |
16
|
|
|
use Yiisoft\Yii\Cycle\DataReader\Cache\CachedCount; |
17
|
|
|
use Yiisoft\Yii\Cycle\DataReader\Cache\CachedCollection; |
18
|
|
|
|
19
|
|
|
final class OffsetDataReader implements DataReaderInterface, OffsetableDataInterface, CountableDataInterface |
20
|
|
|
{ |
21
|
|
|
/** @var QueryInterface|Select */ |
22
|
|
|
private $query; |
23
|
|
|
private ?int $limit = null; |
24
|
|
|
private ?int $offset = null; |
25
|
|
|
private CachedCount $countCache; |
26
|
|
|
private CachedCollection $itemsCache; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Select|SelectQuery $query |
30
|
|
|
*/ |
31
|
|
|
public function __construct($query) |
32
|
|
|
{ |
33
|
|
|
if (!$query instanceof Countable) { |
|
|
|
|
34
|
|
|
throw new InvalidArgumentException(sprintf('Query should implement %s interface', Countable::class)); |
35
|
|
|
} |
36
|
|
|
if (!$query instanceof PaginableInterface) { |
|
|
|
|
37
|
|
|
throw new InvalidArgumentException( |
38
|
|
|
sprintf('Query should implement %s interface', PaginableInterface::class) |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
$this->query = clone $query; |
42
|
|
|
$this->countCache = new CachedCount($this->query); |
43
|
|
|
$this->itemsCache = new CachedCollection(); |
44
|
|
|
} |
45
|
|
|
public function withLimit(int $limit): self |
46
|
|
|
{ |
47
|
|
|
$clone = clone $this; |
48
|
|
|
$clone->setLimit($limit); |
49
|
|
|
return $clone; |
50
|
|
|
} |
51
|
|
|
public function withOffset(int $offset): self |
52
|
|
|
{ |
53
|
|
|
$clone = clone $this; |
54
|
|
|
$clone->setOffset($offset); |
55
|
|
|
return $clone; |
56
|
|
|
} |
57
|
|
|
public function count(): int |
58
|
|
|
{ |
59
|
|
|
return $this->countCache->getCount(); |
60
|
|
|
} |
61
|
|
|
public function read(): iterable |
62
|
|
|
{ |
63
|
|
|
if ($this->itemsCache->getCollection() !== null) { |
64
|
|
|
return $this->itemsCache->getCollection(); |
65
|
|
|
} |
66
|
|
|
$newQuery = clone $this->query; |
67
|
|
|
if ($this->offset !== null) { |
68
|
|
|
$newQuery->offset($this->offset); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
if ($this->limit !== null) { |
71
|
|
|
$newQuery->limit($this->limit); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
$this->itemsCache->setCollection($newQuery->fetchAll()); |
|
|
|
|
74
|
|
|
return $this->itemsCache->getCollection(); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function setLimit(?int $limit): void |
78
|
|
|
{ |
79
|
|
|
if ($this->limit !== $limit) { |
80
|
|
|
$this->limit = $limit; |
81
|
|
|
$this->itemsCache = new CachedCollection(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
private function setOffset(?int $offset): void |
85
|
|
|
{ |
86
|
|
|
if ($this->offset !== $offset) { |
87
|
|
|
$this->offset = $offset; |
88
|
|
|
$this->itemsCache = new CachedCollection(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|