-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paginator.php
152 lines (126 loc) · 4.45 KB
/
Paginator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Doctrine\Odm;
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
use ApiPlatform\State\Pagination\PaginatorInterface;
use Doctrine\ODM\MongoDB\Iterator\Iterator;
use Doctrine\ODM\MongoDB\UnitOfWork;
/**
* Decorates the Doctrine MongoDB ODM paginator.
*
* @author Kévin Dunglas <[email protected]>
* @author Alan Poulain <[email protected]>
*/
final class Paginator implements \IteratorAggregate, PaginatorInterface
{
public const LIMIT_ZERO_MARKER_FIELD = '___';
public const LIMIT_ZERO_MARKER = 'limit0';
private ?\ArrayIterator $iterator = null;
private readonly int $firstResult;
private readonly int $maxResults;
private readonly int $totalItems;
public function __construct(private readonly Iterator $mongoDbOdmIterator, private readonly UnitOfWork $unitOfWork, private readonly string $resourceClass, private readonly array $pipeline)
{
$resultsFacetInfo = $this->getFacetInfo('results');
$this->getFacetInfo('count');
/*
* Since the {@see \MongoDB\Driver\Cursor} class does not expose information about
* skip/limit parameters of the query, the values set in the facet stage are used instead.
*/
$this->firstResult = $this->getStageInfo($resultsFacetInfo, '$skip');
$this->maxResults = $this->hasLimitZeroStage($resultsFacetInfo) ? 0 : $this->getStageInfo($resultsFacetInfo, '$limit');
$this->totalItems = $mongoDbOdmIterator->toArray()[0]['count'][0]['count'] ?? 0;
}
/**
* {@inheritdoc}
*/
public function getCurrentPage(): float
{
if (0 >= $this->maxResults) {
return 1.;
}
return floor($this->firstResult / $this->maxResults) + 1.;
}
/**
* {@inheritdoc}
*/
public function getLastPage(): float
{
if (0 >= $this->maxResults) {
return 1.;
}
return ceil($this->totalItems / $this->maxResults) ?: 1.;
}
/**
* {@inheritdoc}
*/
public function getItemsPerPage(): float
{
return (float) $this->maxResults;
}
/**
* {@inheritdoc}
*/
public function getTotalItems(): float
{
return (float) $this->totalItems;
}
/**
* {@inheritdoc}
*/
public function getIterator(): \Traversable
{
return $this->iterator ?? $this->iterator = new \ArrayIterator(array_map(fn ($result): object => $this->unitOfWork->getOrCreateDocument($this->resourceClass, $result), $this->mongoDbOdmIterator->toArray()[0]['results']));
}
/**
* {@inheritdoc}
*/
public function count(): int
{
return is_countable($this->mongoDbOdmIterator->toArray()[0]['results']) ? \count($this->mongoDbOdmIterator->toArray()[0]['results']) : 0;
}
/**
* @throws InvalidArgumentException
*/
private function getFacetInfo(string $field): array
{
foreach ($this->pipeline as $indexStage => $infoStage) {
if (\array_key_exists('$facet', $infoStage)) {
if (!isset($this->pipeline[$indexStage]['$facet'][$field])) {
throw new InvalidArgumentException("\"$field\" facet was not applied to the aggregation pipeline.");
}
return $this->pipeline[$indexStage]['$facet'][$field];
}
}
throw new InvalidArgumentException('$facet stage was not applied to the aggregation pipeline.');
}
/**
* @throws InvalidArgumentException
*/
private function getStageInfo(array $resultsFacetInfo, string $stage): int
{
foreach ($resultsFacetInfo as $resultFacetInfo) {
if (isset($resultFacetInfo[$stage])) {
return $resultFacetInfo[$stage];
}
}
throw new InvalidArgumentException("$stage stage was not applied to the facet stage of the aggregation pipeline.");
}
private function hasLimitZeroStage(array $resultsFacetInfo): bool
{
foreach ($resultsFacetInfo as $resultFacetInfo) {
if (self::LIMIT_ZERO_MARKER === ($resultFacetInfo['$match'][self::LIMIT_ZERO_MARKER_FIELD] ?? null)) {
return true;
}
}
return false;
}
}