Passed
Push — master ( 4a15cc...52d15c )
by Bas
07:55
created

ValidatesExpressions::isLogicalOperator()   A

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