Range::getDomain()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

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
ccs 1
cts 1
cp 1
crap 1
rs 10
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|null
33
     */
34
    private $startType;
35
36
    /**
37
     * @var string|null
38
     */
39
    private $endType;
40
41
    /**
42
     * @var Flags|null
43
     */
44
    private $flags;
45
46 23
47
    /**
48
     * @param int|float|string $startValue
49
     * @param int|float|string $endValue
50
     */
51
    public function __construct(
52
        string $lexeme,
53
        int $position,
54
        string $domain,
55 23
        $startValue,
56 16
        $endValue,
57 10
        ?string $startType,
58
        ?string $endType,
59 10
        ?Flags $flags = null
60 10
    ) {
61 10
        $this->ensureValidType($startType);
62 10
        $this->ensureValidType($endType);
63 10
        parent::__construct(Tokenizer::TOKEN_TERM, $lexeme, $position);
64 10
65
        $this->domain = $domain;
66
        $this->startValue = $startValue;
67 7
        $this->endValue = $endValue;
68
        $this->startType = $startType;
69 7
        $this->endType = $endType;
70
        $this->flags = $flags;
71
    }
72
73
74
    public function getDomain(): string
75
    {
76 6
        return $this->domain;
77
    }
78 6
79
80
    /**
81
     * @return int|float|string
82
     */
83
    public function getStartValue()
84
    {
85 5
        return $this->startValue;
86
    }
87 5
88
89
    /**
90
     * @return int|float|string
91 7
     */
92
    public function getEndValue()
93 7
    {
94
        return $this->endValue;
95
    }
96
97 1
98
    public function getStartType(): ?string
99 1
    {
100 1
        return $this->startType;
101
    }
102
103 6
104
    public function setStartType(?string $startType): void
105 6
    {
106
        $this->startType = $startType;
107
    }
108
109 1
110
    public function getEndType(): ?string
111 1
    {
112 1
        return $this->endType;
113
    }
114
115 1
116
    public function setEndType(?string $endType): void
117 1
    {
118
        $this->endType = $endType;
119
    }
120
121 1
122
    public function getStartSign(): string
123 1
    {
124
        return $this->getStartType() === Range::TYPE_INCLUSIVE ? '>=' : '>';
125
    }
126
127 1
128
    public function getEndSign(): string
129 1
    {
130
        return $this->getEndType() === Range::TYPE_INCLUSIVE ? '<=' : '<';
131
    }
132
133 1
134
    public function isStartDefined(): bool
135 1
    {
136
        return $this->getStartValue() !== '*';
137
    }
138
139 23
140
    public function isEndDefined(): bool
141 23
    {
142 13
        return $this->getEndValue() !== '*';
143
    }
144 16
145
146
    public function getFlags(): ?Flags
147
    {
148
        return $this->flags;
149
    }
150
151
152
    private function ensureValidType(?string $type): void
153
    {
154
        if (! in_array($type, [self::TYPE_EXCLUSIVE, self::TYPE_INCLUSIVE], true)) {
155
            throw new InvalidArgumentException(sprintf('Invalid range type: %s', $type));
156
        }
157
    }
158
159
}
160