Failed Conditions
Push — master ( e341c2...878df1 )
by Bas
12:12
created

ValidatesExpressions::isRange()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 3
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
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 $value
12
     *
13
     * @return bool
14
     */
15 1
    public function isBind($value)
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 $value
29
     *
30
     * @return bool
31
     */
32
    public function isCollectionBind($value)
33
    {
34
        if (is_string($value)) {
35
            return true;
36
        }
37
38
        return false;
39
    }
40
41
    /**
42
     * @param $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 $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 $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
    public function isQuery($value): bool
96
    {
97
        return $value instanceof QueryBuilder;
98
    }
99
100 5
    public function isFunction($value): bool
101
    {
102 5
        return $value instanceof FunctionExpression;
103
    }
104
105 1
    public function isLogicalOperator($operator): bool
106
    {
107 1
        return isset($this->logicalOperators[strtoupper($operator)]);
108
    }
109
110 4
    public function isComparisonOperator($operator): bool
111
    {
112 4
        return isset($this->comparisonOperators[strtoupper($operator)]);
113
    }
114
115
    public function isArithmeticOperator($operator): bool
116
    {
117
        return isset($this->arithmeticOperators[$operator]);
118
    }
119
120 1
    public function isSortDirection($value): bool
121
    {
122 1
        if (is_string($value) && preg_match('/asc|desc/i', $value)) {
123 1
            return true;
124
        }
125
126 1
        return false;
127
    }
128
129
    /**
130
     * @param mixed $value
131
     * @return bool
132
     */
133 1
    public function isGraphDirection($value): bool
134
    {
135 1
        if (is_string($value) && preg_match('/outbound|inbound|any/i', $value)) {
136 1
            return true;
137
        }
138
139 1
        return false;
140
    }
141
142
    /**
143
     * @param $value
144
     *
145
     * @return bool
146
     */
147 5
    public function isCollection($value): bool
148
    {
149 5
        if (is_string($value) && preg_match('/^[a-zA-Z0-9_-]+$/', $value)) {
150 5
            return true;
151
        }
152
153 3
        return false;
154
    }
155
156
    public function isGraph($value): bool
157
    {
158
        return $this->isCollection($value);
159
    }
160
161 2
    public function isKey($value): bool
162
    {
163
        if (
164 2
            is_string($value) &&
165 2
            preg_match("/^[a-zA-Z0-9_-]+\/?[a-zA-Z0-9_\-\:\.\@\(\)\+\,\=\;\$\!\*\'\%]+$/", $value)
166
        ) {
167 1
            return true;
168
        }
169
170 2
        return false;
171
    }
172
173 2
    public function isId($value): bool
174
    {
175
        if (
176 2
            is_string($value) &&
177 2
            preg_match("/^[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+\/?[a-zA-Z0-9_\-\:\.\@\(\)\+\,\=\;\$\!\*\'\%]+$/", $value)
178
        ) {
179 1
            return true;
180
        }
181
182 2
        return false;
183
    }
184
185
    /**
186
     * @param $value
187
     *
188
     * @return bool
189
     */
190 5
    public function isVariable($value)
191
    {
192 5
        if (is_string($value) && preg_match('/^\$?[a-zA-Z_][a-zA-Z0-9_]*+$/', $value)) {
193 5
            return true;
194
        }
195
196 3
        return false;
197
    }
198
199
    public function isRegisteredVariable($value, $registeredVariables = []): bool
200
    {
201
        return isset($registeredVariables[$value]);
202
    }
203
204
    /**
205
     * @param $value
206
     *
207
     * @return bool
208
     */
209 1
    public function isAttribute($value): bool
210
    {
211
        $pattern = '/^(@?[\d\w_]+|`@?[\d\w_]+`)(\[\`.+\`\]|\[[\d\w\*]*\])*'
212 1
            . '(\.(\`.+\`|@?[\d\w]*)(\[\`.+\`\]|\[[\d\w\*]*\])*)*$/';
213
        if (
214 1
            is_string($value) &&
215 1
            preg_match($pattern, $value)
216
        ) {
217 1
            return true;
218
        }
219
220 1
        return false;
221
    }
222
223
    /**
224
     * @param mixed $value
225
     * @param array $registeredVariables
226
     * @return bool
227
     */
228 5
    public function isReference($value, $registeredVariables = []): bool
229
    {
230 5
        $variables = '';
231 5
        if (!empty($registeredVariables)) {
232 5
            $variables = implode('|', $registeredVariables);
233
        }
234
235 5
        if (! is_string($value) || empty($value)) {
236
            return false;
237
        }
238
239 5
        return (bool) preg_match(
240 5
            '/^('
241 5
                . $variables
242 5
                . '|CURRENT|NEW|OLD)(\[\`.+\`\]|\[[\d\w\*]*\])*(\.(\`.+\`|@?[\d\w]*)(\[\`.+\`\]|\[[\d\w\*]*\])*)*$/',
243
            $value
244
        );
245
    }
246
247
    /**
248
     * @param $value
249
     *
250
     * @return bool
251
     */
252 5
    public function isObject($value): bool
253
    {
254 5
        if (is_object($value) || (is_array($value) && $this->isAssociativeArray($value))) {
255 1
            return true;
256
        }
257
258 5
        return false;
259
    }
260
261 1
    public function isBindParameter($bindParameter): bool
262
    {
263 1
        if (preg_match('/^@?[a-zA-Z0-9][a-zA-Z0-9_]*$/', $bindParameter)) {
264 1
            return true;
265
        }
266
267 1
        return false;
268
    }
269
270
    /**
271
     * Check if the array is associative.
272
     *
273
     * @param array $array
274
     *
275
     * @return bool
276
     */
277 4
    public function isAssociativeArray(array $array)
278
    {
279 4
        if (empty($array)) {
280 1
            return true;
281
        }
282
283 4
        return !ctype_digit(implode('', array_keys($array)));
284
    }
285
286
    /**
287
     * Check if the array is numeric.
288
     *
289
     * @param array $array
290
     *
291
     * @return bool
292
     */
293 2
    public function isIndexedArray(array $array)
294
    {
295 2
        if (empty($array)) {
296 1
            return true;
297
        }
298
299 2
        return ctype_digit(implode('', array_keys($array)));
300
    }
301
}
302