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