Passed
Push — master ( b4f91d...b5810c )
by Bas
07:20 queued 04:57
created

ValidatesReferences::isCollectionBind()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
1
<?php
2
3
namespace LaravelFreelancerNL\FluentAQL\Traits;
4
5
trait ValidatesReferences
6
{
7
    /**
8
     * @param mixed $value
9
     *
10
     * @return bool
11
     */
12 1
    public function isBind($value): bool
13
    {
14 1
        if (is_string($value)) {
15 1
            return true;
16
        }
17
        if (is_object($value)) {
18
            return true;
19
        }
20
21
        return false;
22
    }
23
24
    /**
25
     * @param mixed $value
26
     *
27
     * @return bool
28
     */
29
    public function isCollectionBind($value): bool
30
    {
31
        if (is_string($value)) {
32
            return true;
33
        }
34
35
        return false;
36
    }
37
38
    /**
39
     * @param mixed $value
40
     *
41
     * @return bool
42
     */
43 5
    public function isVariable($value): bool
44
    {
45 5
        if (is_string($value) && preg_match('/^\$?[a-zA-Z_][a-zA-Z0-9_]*+$/', $value)) {
46 5
            return true;
47
        }
48
49 3
        return false;
50
    }
51
52
    /**
53
     * @param mixed  $value
54
     * @param  array  $registeredVariables
55
     * @return bool
56
     */
57 1
    public function isRegisteredVariable($value, $registeredVariables = []): bool
58
    {
59 1
        return isset($registeredVariables[$value]);
60
    }
61
62
    /**
63
     * @param mixed $value
64
     *
65
     * @return bool
66
     */
67
    public function isAttribute($value): bool
68
    {
69
        $pattern = '/^(@?[\d\w_]+|`@?[\d\w_]+`)(\[\`.+\`\]|\[[\d\w\*]*\])*'
70
            . '(\.(\`.+\`|@?[\d\w]*)(\[\`.+\`\]|\[[\d\w\*]*\])*)*$/';
71
        if (
72
            is_string($value) &&
73
            preg_match($pattern, $value)
74
        ) {
75
            return true;
76
        }
77
78
        return false;
79
    }
80
81
    /**
82
     * @param mixed $value
83
     * @param array $registeredVariables
84
     * @return bool
85
     */
86 5
    public function isReference($value, $registeredVariables = []): bool
87
    {
88 5
        $variables = '';
89 5
        if (!empty($registeredVariables)) {
90 5
            $variables = implode('|', $registeredVariables);
91
        }
92
93 5
        if (! is_string($value) || empty($value)) {
94
            return false;
95
        }
96
97 5
        return (bool) preg_match(
98 5
            '/^('
99 5
            . $variables
100 5
            . '|CURRENT|NEW|OLD)(\[\`.+\`\]|\[[\d\w\*]*\])*(\.(\`.+\`|@?[\d\w]*)(\[\`.+\`\]|\[[\d\w\*]*\])*)*$/',
101
            $value
102
        );
103
    }
104
}
105