Passed
Pull Request — 2.1 (#66)
by Vincent
22:57 queued 15:10
created

Operator::setContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Bdf\Prime\Query\Expression;
4
5
use Bdf\Prime\Platform\PlatformInterface;
6
use Bdf\Prime\Query\Compiler\CompilerInterface;
7
8
use Bdf\Prime\Types\TypeInterface;
9
10
use function count;
11
use function is_array;
12
13
/**
14
 * Wrap an operator as expression object
15
 * It can be used on Criteria setters to specify the operator without concatenation with the column name
16
 */
17
final class Operator implements ExpressionTransformerInterface, TypedExpressionInterface
18
{
19
    private string $operator;
20
    private $value;
21
    private string $column;
22
    private ?TypeInterface $type = null;
23
    private ?PlatformInterface $platform = null;
24
25
    /**
26
     * Prefer use of static methods
27
     *
28
     * @param string $operator Comparison operator. Should be one of operators handled by compiler, and not a raw SQL one
29
     * @param mixed $value The value to compare
30
     */
31 4
    public function __construct(string $operator, $value)
32
    {
33 4
        $this->operator = $operator;
34 4
        $this->value = $value;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 2
    public function setType(TypeInterface $type): void
41
    {
42 2
        $this->type = $type;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 4
    public function setContext(object $compiler, string $column, string $operator): void
49
    {
50 4
        $this->column = $column;
51
52 4
        if ($compiler instanceof CompilerInterface) {
53 4
            $this->platform = $compiler->platform();
54
        }
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 4
    public function getValue()
61
    {
62 4
        if (!$this->type && !$this->platform) {
63
            return $this->value;
64
        }
65
66 4
        $type = $this->type ?? $this->platform->types();
0 ignored issues
show
Bug introduced by
The method types() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        $type = $this->type ?? $this->platform->/** @scrutinizer ignore-call */ types();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
68 4
        if (!is_array($this->value)) {
69 4
            return $type->toDatabase($this->value);
70
        }
71
72 2
        return array_map([$type, 'toDatabase'], $this->value);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 4
    public function getOperator(): string
79
    {
80 4
        return $this->operator;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 4
    public function getColumn(): string
87
    {
88 4
        return $this->column;
89
    }
90
91
    /**
92
     * Create an operator using the operator name as method name
93
     * If multiple arguments are passed, they will be used as the value (value will be an array)
94
     *
95
     * @param string $operator
96
     * @param list<mixed> $arguments
0 ignored issues
show
Bug introduced by
The type Bdf\Prime\Query\Expression\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
97
     *
98
     * @return self
99
     */
100 3
    public static function __callStatic(string $operator, array $arguments): self
101
    {
102 3
        return new self($operator, count($arguments) === 1 ? $arguments[0] : $arguments);
103
    }
104
105
    /**
106
     * Create a less than `<` operator
107
     *
108
     * @param scalar $value Comparison value
109
     *
110
     * @return self
111
     */
112 2
    public static function lessThan($value): self
113
    {
114 2
        return new self('<', $value);
115
    }
116
117
    /**
118
     * Create a less than or equal `<=` operator
119
     *
120
     * @param scalar $value Comparison value
121
     *
122
     * @return self
123
     */
124 1
    public static function lessThanOrEqual($value): self
125
    {
126 1
        return new self('<=', $value);
127
    }
128
129
    /**
130
     * Create a greater than `>` operator
131
     *
132
     * @param scalar $value Comparison value
133
     *
134
     * @return self
135
     */
136 1
    public static function greaterThan($value): self
137
    {
138 1
        return new self('>', $value);
139
    }
140
141
    /**
142
     * Create a greater than or equal `>=` operator
143
     *
144
     * @param scalar $value Comparison value
145
     *
146
     * @return self
147
     */
148 1
    public static function greaterThanOrEqual($value): self
149
    {
150 1
        return new self('>=', $value);
151
    }
152
153
    /**
154
     * Create a regex `~=` operator
155
     *
156
     * @param string $pattern Regex pattern
157
     *
158
     * @return self
159
     */
160 1
    public static function regex(string $pattern): self
161
    {
162 1
        return new self('~=', $pattern);
163
    }
164
165
    /**
166
     * Create a like operator
167
     *
168
     * @param string $pattern Like pattern
169
     *
170
     * @return self
171
     *
172
     * @see Like for a dedicated API
173
     */
174 1
    public static function like(string $pattern): self
175
    {
176 1
        return new self(':like', $pattern);
177
    }
178
179
    /**
180
     * Create a not like `!like` operator
181
     *
182
     * @param string $pattern Like pattern
183
     *
184
     * @return self
185
     */
186 1
    public static function notlike(string $pattern): self
187
    {
188 1
        return new self('!like', $pattern);
189
    }
190
191
    /**
192
     * Create in operator
193
     *
194
     * @param mixed ...$values
195
     *
196
     * @return self
197
     */
198 2
    public static function in(...$values): self
199
    {
200 2
        return new self('in', $values);
201
    }
202
203
    /**
204
     * Create not in operator
205
     *
206
     * @param mixed ...$values
207
     *
208
     * @return self
209
     */
210 1
    public static function notIn(...$values): self
211
    {
212 1
        return new self('!in', $values);
213
    }
214
215
    /**
216
     * Create between operator
217
     *
218
     * @param scalar $min Minimum value
219
     * @param scalar $max Maximum value
220
     *
221
     * @return self
222
     */
223 1
    public static function between($min, $max): self
224
    {
225 1
        return new self('between', [$min, $max]);
226
    }
227
228
    /**
229
     * Create not between operator
230
     *
231
     * @param scalar $min Minimum value
232
     * @param scalar $max Maximum value
233
     *
234
     * @return self
235
     */
236 1
    public static function notBetween($min, $max): self
237
    {
238 1
        return new self('!between', [$min, $max]);
239
    }
240
241
    /**
242
     * Create not equal `!=` operator
243
     *
244
     * @param mixed $value Comparison value
245
     *
246
     * @return self
247
     */
248 1
    public static function notEqual($value): self
249
    {
250 1
        return new self('!=', $value);
251
    }
252
}
253