Completed
Push — master ( c0739b...54ec6b )
by Alex
21s queued 14s
created

QueryResult::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace POData\Providers\Query;
6
7
use InvalidArgumentException;
8
9
/**
10
 * Class QueryResult.
11
 * @package POData\Providers\Query
12
 */
13
class QueryResult
14
{
15
    /**
16
     * @var object[]|object|null
17
     */
18
    public $results;
19
20
    /***
21
     * @var int|null
22
     */
23
    public $count;
24
25
    /***
26
     * @var bool|null
27
     */
28
    public $hasMore;
29
30
    /**
31
     * QueryResult constructor.
32
     * @param object|object[]|null $results
33
     * @param int|null             $count
34
     * @param bool|null            $hasMore
35
     */
36
    public function __construct($results = null, ?int $count = null, ?bool $hasMore = null)
37
    {
38
        $this->results = $results;
39
        $this->count   = $count;
40
        $this->hasMore = $hasMore;
41
    }
42
43
    /**
44
     * @param int      $count
45
     * @param int|null $top
46
     * @param int|null $skip
47
     *
48
     * @throws InvalidArgumentException if $count is not numeric
49
     * @return int                      the paging adjusted count
50
     */
51
    public static function adjustCountForPaging(int $count, ?int $top, ?int $skip)
52
    {
53
        //treat nulls like 0
54
        if (null === $skip) {
55
            $skip = 0;
56
        }
57
58
        $count = $count - $skip; //eliminate the skipped records
59
        if ($count < 0) {
60
            return 0;
61
        } //if there aren't enough to skip, the count is 0
62
63
        if (null === $top) {
64
            return $count;
65
        } //if there's no top, then it's the count as is
66
67
        return intval(min($count, $top)); //count is top, unless there aren't enough records
68
    }
69
}
70