-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionIterator.php
44 lines (33 loc) · 1.1 KB
/
CollectionIterator.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
<?php
declare(strict_types=1);
namespace Manyou\LeanStorage;
use Generator;
use InvalidArgumentException;
use Manyou\LeanStorage\Request\QueryCollection;
class CollectionIterator
{
public function __construct(private LeanStorageClient $client)
{
}
/** @return Generator|array[] */
public function __invoke(string $collection, array $where = [], int $limit = 1000): Generator
{
if (isset($where['objectId'])) {
throw new InvalidArgumentException('"objectId" will be used as pagination cursor.');
}
do {
$count = 0;
$query = ['order' => 'objectId', 'limit' => $limit];
if ($where !== []) {
$query += ['where' => $where];
}
$response = $this->client->request(new QueryCollection($collection, $query))->wait();
foreach ($response['results'] as $result) {
$objectId = $result['objectId'];
$count++;
yield $result;
}
$where['objectId'] = ['$gt' => $objectId];
} while ($count === $limit);
}
}