-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paginator.php
118 lines (99 loc) · 3.22 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
<?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\Elasticsearch;
use ApiPlatform\Elasticsearch\Serializer\DocumentNormalizer;
use ApiPlatform\State\Pagination\PaginatorInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
/**
* Paginator for Elasticsearch.
*
* @experimental
*
* @author Baptiste Meyer <[email protected]>
*/
final class Paginator implements \IteratorAggregate, PaginatorInterface
{
private array $cachedDenormalizedDocuments = [];
public function __construct(private readonly DenormalizerInterface $denormalizer, private readonly array $documents, private readonly string $resourceClass, private readonly int $limit, private readonly int $offset, private readonly array $denormalizationContext = [])
{
}
/**
* {@inheritdoc}
*/
public function count(): int
{
return isset($this->documents['hits']['hits']) ? \count($this->documents['hits']['hits']) : 0;
}
/**
* {@inheritdoc}
*/
public function getLastPage(): float
{
if (0 >= $this->limit) {
return 1.;
}
return ceil($this->getTotalItems() / $this->limit) ?: 1.;
}
/**
* {@inheritdoc}
*/
public function getTotalItems(): float
{
// for elastic search version > 7.0.0
if (\is_array($this->documents['hits']['total'])) {
return (float) ($this->documents['hits']['total']['value'] ?? 0.);
}
// for elastic search old versions
return (float) ($this->documents['hits']['total'] ?? 0.);
}
/**
* {@inheritdoc}
*/
public function getCurrentPage(): float
{
if (0 >= $this->limit) {
return 1.;
}
return floor($this->offset / $this->limit) + 1.;
}
/**
* {@inheritdoc}
*/
public function getItemsPerPage(): float
{
return (float) $this->limit;
}
/**
* {@inheritdoc}
*/
public function getIterator(): \Traversable
{
$denormalizationContext = array_merge([AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true], $this->denormalizationContext);
foreach ($this->documents['hits']['hits'] ?? [] as $document) {
$cacheKey = isset($document['_index'], $document['_id']) ? hash('xxh3', "{$document['_index']}_{$document['_id']}") : null;
if ($cacheKey && \array_key_exists($cacheKey, $this->cachedDenormalizedDocuments)) {
$object = $this->cachedDenormalizedDocuments[$cacheKey];
} else {
$object = $this->denormalizer->denormalize(
$document,
$this->resourceClass,
DocumentNormalizer::FORMAT,
$denormalizationContext
);
if ($cacheKey) {
$this->cachedDenormalizedDocuments[$cacheKey] = $object;
}
}
yield $object;
}
}
}