ResultSet::getSortKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2020-2025 Iain Cambridge
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
10
 * the Free Software Foundation, either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Parthenon\Athena;
23
24
use Parthenon\Athena\Exception\InvalidSortKeyException;
25
use Parthenon\Common\FieldAccesorTrait;
26
27
final class ResultSet
28
{
29
    use FieldAccesorTrait;
30
    private array $results;
31
    private string $sortKey;
32
    private int $limit;
33
    private string $sortType;
34
35
    public function __construct(array $results, string $sortKey, string $sortType, int $limit)
36
    {
37
        $this->results = $results;
38
        $this->sortKey = $sortKey;
39
        $this->limit = $limit;
40
        $this->sortType = $sortType;
41
    }
42
43
    public function getResults(): array
44
    {
45
        if ($this->limit < 1) {
46
            return $this->results;
47
        }
48
49
        return array_slice($this->results, 0, $this->limit);
50
    }
51
52
    public function getSortKey(): string
53
    {
54
        return $this->sortKey;
55
    }
56
57
    public function getSortType(): string
58
    {
59
        return $this->sortType;
60
    }
61
62
    public function hasMore(): bool
63
    {
64
        return count($this->results) > $this->limit;
65
    }
66
67
    public function getFirstKey()
68
    {
69
        $results = $this->getResults();
70
        $lastItem = current($results);
71
        if (false === $lastItem) {
72
            return null;
73
        }
74
75
        return $this->getFieldData($lastItem, $this->sortKey);
76
    }
77
78
    /**
79
     * @throws InvalidSortKeyException
80
     */
81
    public function getLastKey()
82
    {
83
        $results = $this->getResults();
84
        $lastItem = end($results);
85
        if (false === $lastItem) {
86
            return null;
87
        }
88
89
        return $this->getFieldData($lastItem, $this->sortKey);
90
    }
91
}
92