|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Copyright Iain Cambridge 2020-2023. |
|
7
|
|
|
* |
|
8
|
|
|
* Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license. |
|
9
|
|
|
* |
|
10
|
|
|
* Change Date: TBD ( 3 years after 2.2.0 release ) |
|
11
|
|
|
* |
|
12
|
|
|
* On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Parthenon\Athena; |
|
16
|
|
|
|
|
17
|
|
|
use Parthenon\Athena\Exception\InvalidSortKeyException; |
|
18
|
|
|
use Parthenon\Common\FieldAccesorTrait; |
|
19
|
|
|
|
|
20
|
|
|
final class ResultSet |
|
21
|
|
|
{ |
|
22
|
|
|
use FieldAccesorTrait; |
|
23
|
|
|
private array $results; |
|
24
|
|
|
private string $sortKey; |
|
25
|
|
|
private int $limit; |
|
26
|
|
|
private string $sortType; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(array $results, string $sortKey, string $sortType, int $limit) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->results = $results; |
|
31
|
|
|
$this->sortKey = $sortKey; |
|
32
|
|
|
$this->limit = $limit; |
|
33
|
|
|
$this->sortType = $sortType; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getResults(): array |
|
37
|
|
|
{ |
|
38
|
|
|
if ($this->limit < 1) { |
|
39
|
|
|
return $this->results; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return array_slice($this->results, 0, $this->limit); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getSortKey(): string |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->sortKey; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getSortType(): string |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->sortType; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function hasMore(): bool |
|
56
|
|
|
{ |
|
57
|
|
|
return count($this->results) > $this->limit; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getFirstKey() |
|
61
|
|
|
{ |
|
62
|
|
|
$results = $this->getResults(); |
|
63
|
|
|
$lastItem = current($results); |
|
64
|
|
|
if (false === $lastItem) { |
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->getFieldData($lastItem, $this->sortKey); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @throws InvalidSortKeyException |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getLastKey() |
|
75
|
|
|
{ |
|
76
|
|
|
$results = $this->getResults(); |
|
77
|
|
|
$lastItem = end($results); |
|
78
|
|
|
if (false === $lastItem) { |
|
79
|
|
|
return null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $this->getFieldData($lastItem, $this->sortKey); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|