Limit::validate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.5923

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
crap 4.5923
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace mav3rick177\RapidPagination\Base\Query;
4
5
use mav3rick177\RapidPagination\Base\Exceptions\Query\LimitParameterException;
6
7
/**
8
 * Class Limit
9
 */
10
class Limit
11
{
12
    /**
13
     * @var int
14
     */
15
    protected $limit;
16
17
    /**
18
     * @var int
19
     */
20
    protected $originalLimit;
21
22
    /**
23
     * @var bool
24
     */
25
    protected $isSupportQuery;
26
27
    /**
28
     * Limit constructor.
29
     *
30
     * @param int  $originalLimit
31
     * @param bool $isSupportQuery
32
     */
33 17
    public function __construct($originalLimit, $isSupportQuery = false)
34
    {
35 17
        $this->limit = static::validate($originalLimit, $isSupportQuery);
36 17
        $this->originalLimit = (int)$originalLimit;
37 17
        $this->isSupportQuery = (bool)$isSupportQuery;
38
    }
39
40
    /**
41
     * @param  int  $limit
42
     * @param  bool $isSupportQuery
43
     * @return int
44
     */
45 17
    protected static function validate($limit, $isSupportQuery)
46
    {
47 17
        if (!ctype_digit("$limit")) {
48
            throw new LimitParameterException('Limit must be integer');
49
        }
50 17
        if ($limit < 1) {
51
            throw new LimitParameterException('Limit must be positive integer');
52
        }
53 17
        return $isSupportQuery ? 1 : ($limit + 1);
54
    }
55
56
    /**
57
     * @return int
58
     */
59 17
    public function toInteger()
60
    {
61 17
        return $this->limit;
62
    }
63
64
    /**
65
     * @return int
66
     */
67 17
    public function original()
68
    {
69 17
        return $this->originalLimit;
70
    }
71
72
    /**
73
     * @return bool
74
     */
75
    public function isMainQuery()
76
    {
77
        return !$this->isSupportQuery;
78
    }
79
80
    /**
81
     * @return bool
82
     */
83
    public function isSupportQuery()
84
    {
85
        return $this->isSupportQuery;
86
    }
87
88
    /**
89
     * @return static
90
     */
91 10
    public function inverse()
92
    {
93 10
        return new static($this->originalLimit, !$this->isSupportQuery);
94
    }
95
}
96