Passed
Push — master ( 44542b...077e9d )
by Tomáš
03:58
created

Range::getRangeTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace Apicart\FQL\Token\Token;
4
5
use Apicart\FQL\Tokenizer\Tokenizer;
6
use Apicart\FQL\Value\Token;
7
use InvalidArgumentException;
8
9
final class Range extends Token
10
{
11
12
    public const TYPE_INCLUSIVE = 'inclusive';
13
14
    public const TYPE_EXCLUSIVE = 'exclusive';
15
16
    /**
17
     * @var string
18
     */
19
    private $domain;
20
21
    /**
22
     * @var int|float|string
23
     */
24
    private $startValue;
25
26
    /**
27
     * @var int|float|string
28
     */
29
    private $endValue;
30
31
    /**
32
     * @var string
33
     */
34
    private $startType;
35
36
    /**
37
     * @var string
38
     */
39
    private $endType;
40
41
42
    /**
43
     * @param int|float|string $startValue
44
     * @param int|float|string $endValue
45
     */
46 23
    public function __construct(
47
        string $lexeme,
48
        int $position,
49
        string $domain,
50
        $startValue,
51
        $endValue,
52
        ?string $startType,
53
        ?string $endType
54
    ) {
55 23
        $this->ensureValidType($startType);
56 16
        $this->ensureValidType($endType);
57 10
        parent::__construct(Tokenizer::TOKEN_TERM, $lexeme, $position);
58
59 10
        $this->domain = $domain;
60 10
        $this->startValue = $startValue;
61 10
        $this->endValue = $endValue;
62 10
        $this->startType = $startType;
63 10
        $this->endType = $endType;
64 10
    }
65
66
67 7
    public function getDomain(): string
68
    {
69 7
        return $this->domain;
70
    }
71
72
73
    /**
74
     * @return int|float|string
75
     */
76 6
    public function getStartValue()
77
    {
78 6
        return $this->startValue;
79
    }
80
81
82
    /**
83
     * @return int|float|string
84
     */
85 5
    public function getEndValue()
86
    {
87 5
        return $this->endValue;
88
    }
89
90
91 7
    public function getStartType(): string
92
    {
93 7
        return $this->startType;
94
    }
95
96
97 1
    public function setStartType(string $startType): void
98
    {
99 1
        $this->startType = $startType;
100 1
    }
101
102
103 6
    public function getEndType(): string
104
    {
105 6
        return $this->endType;
106
    }
107
108
109 1
    public function setEndType(string $endType): void
110
    {
111 1
        $this->endType = $endType;
112 1
    }
113
114
115 1
    public function getStartSign(): string
116
    {
117 1
        return $this->getStartType() === Range::TYPE_INCLUSIVE ? '>=' : '>';
118
    }
119
120
121 1
    public function getEndSign(): string
122
    {
123 1
        return $this->getEndType() === Range::TYPE_INCLUSIVE ? '<=' : '<';
124
    }
125
126
127 1
    public function isStartDefined(): bool
128
    {
129 1
        return $this->getStartValue() !== '*';
130
    }
131
132
133 1
    public function isEndDefined(): bool
134
    {
135 1
        return $this->getEndValue() !== '*';
136
    }
137
138
139 23
    private function ensureValidType(?string $type): void
140
    {
141 23
        if (! in_array($type, [self::TYPE_EXCLUSIVE, self::TYPE_INCLUSIVE], true)) {
142 13
            throw new InvalidArgumentException(sprintf('Invalid range type: %s', $type));
143
        }
144 16
    }
145
146
}
147