Failed Conditions
Branch prepare-alpha (89acfc)
by Bas
08:11
created

ValidatesExpressions::isId()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 10
ccs 5
cts 5
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 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 1
    public function isGraphDirection($value): bool
116
    {
117 1
        if (preg_match('/outbound|inbound|any/i', $value)) {
118 1
            return true;
119
        }
120
121 1
        return false;
122
    }
123
124
    /**
125
     * @param $value
126
     *
127
     * @return bool
128
     */
129 5
    public function isCollection($value): bool
130
    {
131 5
        if (is_string($value) && preg_match('/^[a-zA-Z0-9_-]+$/', $value)) {
132 5
            return true;
133
        }
134
135 3
        return false;
136
    }
137
138
    public function isGraph($value): bool
139
    {
140
        return $this->isCollection($value);
141
    }
142
143 2
    public function isKey($value): bool
144
    {
145
        if (
146 2
            is_string($value) &&
147 2
            preg_match("/^[a-zA-Z0-9_-]+\/?[a-zA-Z0-9_\-\:\.\@\(\)\+\,\=\;\$\!\*\'\%]+$/", $value)
148
        ) {
149 1
            return true;
150
        }
151
152 2
        return false;
153
    }
154
155 2
    public function isId($value): bool
156
    {
157
        if (
158 2
            is_string($value) &&
159 2
            preg_match("/^[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+\/?[a-zA-Z0-9_\-\:\.\@\(\)\+\,\=\;\$\!\*\'\%]+$/", $value)
160
        ) {
161 1
            return true;
162
        }
163
164 2
        return false;
165
    }
166
167
    /**
168
     * @param $value
169
     *
170
     * @return bool
171
     */
172 5
    public function isVariable($value)
173
    {
174 5
        if (is_string($value) && preg_match('/^\$?[a-zA-Z_][a-zA-Z0-9_]*+$/', $value)) {
175 5
            return true;
176
        }
177
178 3
        return false;
179
    }
180
181
    public function isRegisteredVariable($value, $registeredVariables = []): bool
182
    {
183
        return isset($registeredVariables[$value]);
184
    }
185
186
    /**
187
     * @param $value
188
     *
189
     * @return bool
190
     */
191 1
    public function isAttribute($value): bool
192
    {
193
        $pattern = '/^(@?[\d\w_]+|`@?[\d\w_]+`)(\[\`.+\`\]|\[[\d\w\*]*\])*'
194 1
            . '(\.(\`.+\`|@?[\d\w]*)(\[\`.+\`\]|\[[\d\w\*]*\])*)*$/';
195
        if (
196 1
            is_string($value) &&
197 1
            preg_match($pattern, $value)
198
        ) {
199 1
            return true;
200
        }
201
202 1
        return false;
203
    }
204
205
    /**
206
     * @param mixed $value
207
     * @param array $registeredVariables
208
     * @return bool
209
     */
210 5
    public function isReference($value, $registeredVariables = []): bool
211
    {
212 5
        $variables = '';
213 5
        if (!empty($registeredVariables)) {
214 5
            $variables = implode('|', $registeredVariables);
215
        }
216
217 5
        if (! is_string($value)) {
218
            return false;
219
        }
220
221 5
        return (bool) preg_match(
222
            '/^('
223 5
                . $variables
224 5
                . '|CURRENT|NEW|OLD)(\[\`.+\`\]|\[[\d\w\*]*\])*(\.(\`.+\`|@?[\d\w]*)(\[\`.+\`\]|\[[\d\w\*]*\])*)*$/',
225 5
            $value
226
        );
227
    }
228
229
    /**
230
     * @param $value
231
     *
232
     * @return bool
233
     */
234 5
    public function isObject($value): bool
235
    {
236 5
        if (is_object($value) || (is_array($value) && $this->isAssociativeArray($value))) {
237 1
            return true;
238
        }
239
240 5
        return false;
241
    }
242
243 1
    public function isBindParameter($bindParameter): bool
244
    {
245 1
        if (preg_match('/^@?[a-zA-Z0-9][a-zA-Z0-9_]*$/', $bindParameter)) {
246 1
            return true;
247
        }
248
249 1
        return false;
250
    }
251
252
    /**
253
     * Check if the array is associative.
254
     *
255
     * @param array $array
256
     *
257
     * @return bool
258
     */
259 4
    public function isAssociativeArray(array $array)
260
    {
261 4
        if (empty($array)) {
262 1
            return true;
263
        }
264
265 4
        return !ctype_digit(implode('', array_keys($array)));
266
    }
267
268
    /**
269
     * Check if the array is numeric.
270
     *
271
     * @param array $array
272
     *
273
     * @return bool
274
     */
275 2
    public function isIndexedArray(array $array)
276
    {
277 2
        if (empty($array)) {
278 1
            return true;
279
        }
280
281 2
        return ctype_digit(implode('', array_keys($array)));
282
    }
283
}
284