Passed
Pull Request — master (#12)
by Bas
02:32
created

ValidatesExpressions::isAssociativeArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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