Failed Conditions
Push — master ( 1a89c8...5be1f5 )
by Bas
13:35
created

ValidatesExpressions::isFunction()   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 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace LaravelFreelancerNL\FluentAQL\Traits;
4
5
use LaravelFreelancerNL\FluentAQL\Expressions\FunctionExpression;
6
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
7
8
trait ValidatesExpressions
9
{
10
    /**
11
     * @param mixed $value
12
     *
13
     * @return bool
14
     */
15 1
    public function isBind($value): bool
16
    {
17 1
        if (is_string($value)) {
18 1
            return true;
19
        }
20
        if (is_object($value)) {
21
            return true;
22
        }
23
24
        return false;
25
    }
26
27
    /**
28
     * @param mixed $value
29
     *
30
     * @return bool
31
     */
32
    public function isCollectionBind($value): bool
33
    {
34
        if (is_string($value)) {
35
            return true;
36
        }
37
38
        return false;
39
    }
40
41
    /**
42
     * @param mixed $value
43
     *
44
     * @return bool
45
     */
46 3
    public function isRange($value): bool
47
    {
48 3
        if (is_string($value) && preg_match('/^[0-9]+(?:\.[0-9]+)?+\.{2}[0-9]+(?:\.[0-9]+)?$/', $value)) {
49 1
            return true;
50
        }
51
52 3
        return false;
53
    }
54
55
    /**
56
     * @param mixed $value
57
     *
58
     * @return bool
59
     */
60 4
    public function isBoolean($value): bool
61
    {
62 4
        return is_bool($value) || $value === 'true' || $value === 'false';
63
    }
64
65
    /**
66
     * @param $value
67
     *
68
     * @return bool
69
     */
70 4
    public function isNull($value): bool
71
    {
72 4
        return $value === null || $value == 'null';
73
    }
74
75
    /**
76
     * @param $value
77
     *
78
     * @return bool
79
     */
80 5
    public function isNumber($value): bool
81
    {
82 5
        return is_numeric($value) && !is_string($value);
83
    }
84
85
    /**
86
     * @param mixed $value
87
     *
88
     * @return bool
89
     */
90 5
    public function isList($value): bool
91
    {
92 5
        return is_array($value) && $this->isIndexedArray($value);
93
    }
94
95
    /**
96
     * @param mixed $value
97
     * @return bool
98
     */
99
    public function isQuery($value): bool
100
    {
101
        return $value instanceof QueryBuilder;
102
    }
103
104
    /**
105
     * @param mixed $value
106
     * @return bool
107
     */
108 5
    public function isFunction($value): bool
109
    {
110 5
        return $value instanceof FunctionExpression;
111
    }
112
113
    /**
114
     * @param mixed $operator
115
     * @return bool
116
     */
117 1
    public function isLogicalOperator($operator): bool
118
    {
119 1
        return isset($this->logicalOperators[strtoupper($operator)]);
120
    }
121
122
    /**
123
     * @param mixed $operator
124
     * @return bool
125
     */
126 4
    public function isComparisonOperator($operator): bool
127
    {
128 4
        return isset($this->comparisonOperators[strtoupper($operator)]);
129
    }
130
131
    /**
132
     * @param mixed $operator
133
     * @return bool
134
     */
135
    public function isArithmeticOperator($operator): bool
136
    {
137
        return isset($this->arithmeticOperators[$operator]);
138
    }
139
140
    /**
141
     * @param mixed $value
142
     * @return bool
143
     */
144 1
    public function isSortDirection($value): bool
145
    {
146 1
        if (is_string($value) && preg_match('/asc|desc/i', $value)) {
147 1
            return true;
148
        }
149
150 1
        return false;
151
    }
152
153
    /**
154
     * @param mixed $value
155
     * @return bool
156
     */
157 1
    public function isGraphDirection($value): bool
158
    {
159 1
        if (is_string($value) && preg_match('/outbound|inbound|any/i', $value)) {
160 1
            return true;
161
        }
162
163 1
        return false;
164
    }
165
166
    /**
167
     * @param mixed $value
168
     *
169
     * @return bool
170
     */
171 5
    public function isCollection($value): bool
172
    {
173 5
        if (is_string($value) && preg_match('/^[a-zA-Z0-9_-]+$/', $value)) {
174 5
            return true;
175
        }
176
177 3
        return false;
178
    }
179
180
    /**
181
     * @param mixed $value
182
     * @return bool
183
     */
184
    public function isGraph($value): bool
185
    {
186
        return $this->isCollection($value);
187
    }
188
189
    /**
190
     * @param mixed $value
191
     * @return bool
192
     */
193 2
    public function isKey($value): bool
194
    {
195
        if (
196 2
            is_string($value) &&
197 2
            preg_match("/^[a-zA-Z0-9_-]+\/?[a-zA-Z0-9_\-\:\.\@\(\)\+\,\=\;\$\!\*\'\%]+$/", $value)
198
        ) {
199 1
            return true;
200
        }
201
202 2
        return false;
203
    }
204
205
    /**
206
     * @param mixed $value
207
     * @return bool
208
     */
209 2
    public function isId($value): bool
210
    {
211
        if (
212 2
            is_string($value) &&
213 2
            preg_match("/^[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+\/?[a-zA-Z0-9_\-\:\.\@\(\)\+\,\=\;\$\!\*\'\%]+$/", $value)
214
        ) {
215 1
            return true;
216
        }
217
218 2
        return false;
219
    }
220
221
    /**
222
     * @param mixed $value
223
     *
224
     * @return bool
225
     */
226 5
    public function isVariable($value): bool
227
    {
228 5
        if (is_string($value) && preg_match('/^\$?[a-zA-Z_][a-zA-Z0-9_]*+$/', $value)) {
229 5
            return true;
230
        }
231
232 3
        return false;
233
    }
234
235
    /**
236
     * @param mixed  $value
237
     * @param  array  $registeredVariables
238
     * @return bool
239
     */
240
    public function isRegisteredVariable($value, $registeredVariables = []): bool
241
    {
242
        return isset($registeredVariables[$value]);
243
    }
244
245
    /**
246
     * @param mixed $value
247
     *
248
     * @return bool
249
     */
250 1
    public function isAttribute($value): bool
251
    {
252
        $pattern = '/^(@?[\d\w_]+|`@?[\d\w_]+`)(\[\`.+\`\]|\[[\d\w\*]*\])*'
253 1
            . '(\.(\`.+\`|@?[\d\w]*)(\[\`.+\`\]|\[[\d\w\*]*\])*)*$/';
254
        if (
255 1
            is_string($value) &&
256 1
            preg_match($pattern, $value)
257
        ) {
258 1
            return true;
259
        }
260
261 1
        return false;
262
    }
263
264
    /**
265
     * @param mixed $value
266
     * @param array $registeredVariables
267
     * @return bool
268
     */
269 5
    public function isReference($value, $registeredVariables = []): bool
270
    {
271 5
        $variables = '';
272 5
        if (!empty($registeredVariables)) {
273 5
            $variables = implode('|', $registeredVariables);
274
        }
275
276 5
        if (! is_string($value) || empty($value)) {
277
            return false;
278
        }
279
280 5
        return (bool) preg_match(
281 5
            '/^('
282 5
                . $variables
283 5
                . '|CURRENT|NEW|OLD)(\[\`.+\`\]|\[[\d\w\*]*\])*(\.(\`.+\`|@?[\d\w]*)(\[\`.+\`\]|\[[\d\w\*]*\])*)*$/',
284
            $value
285
        );
286
    }
287
288
    /**
289
     * @param mixed $value
290
     *
291
     * @return bool
292
     */
293 5
    public function isObject($value): bool
294
    {
295 5
        if (is_object($value) || (is_array($value) && $this->isAssociativeArray($value))) {
296 1
            return true;
297
        }
298
299 5
        return false;
300
    }
301
302
    /**
303
     * @param mixed $bindParameter
304
     * @return bool
305
     */
306 1
    public function isBindParameter($bindParameter): bool
307
    {
308 1
        if (preg_match('/^@?[a-zA-Z0-9][a-zA-Z0-9_]*$/', $bindParameter)) {
309 1
            return true;
310
        }
311
312 1
        return false;
313
    }
314
315
    /**
316
     * Check if the array is associative.
317
     *
318
     * @param array $array
319
     *
320
     * @return bool
321
     */
322 4
    public function isAssociativeArray(array $array): bool
323
    {
324 4
        if (empty($array)) {
325 1
            return true;
326
        }
327
328 4
        return !ctype_digit(implode('', array_keys($array)));
329
    }
330
331
    /**
332
     * Check if the array is numeric.
333
     *
334
     * @param array $array
335
     *
336
     * @return bool
337
     */
338 2
    public function isIndexedArray(array $array): bool
339
    {
340 2
        if (empty($array)) {
341 1
            return true;
342
        }
343
344 2
        return ctype_digit(implode('', array_keys($array)));
345
    }
346
}
347