Failed Conditions
Push — refactor/improve-static-analys... ( a7b39f...bdf823 )
by Bas
12:11
created

HandlesAqlGrammar   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 59
c 1
b 0
f 0
dl 0
loc 132
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A quoteString() 0 7 2
A getDateFormat() 0 3 1
A parameter() 0 3 2
A wrapTable() 0 8 2
A wrapValue() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
6
7
trait HandlesAqlGrammar
8
{
9
    /**
10
     * Available predicate operators.
11
     *
12
     * @var array<string, int>
13
     */
14
    protected array $comparisonOperators = [
15
        '=='      => 1,
16
        '!='      => 1,
17
        '<'       => 1,
18
        '>'       => 1,
19
        '<='      => 1,
20
        '>='      => 1,
21
        'IN'      => 1,
22
        'NOT IN'  => 1,
23
        'LIKE'    => 1,
24
        '~'       => 1,
25
        '!~'      => 1,
26
        'ALL =='  => 1,
27
        'ALL !='  => 1,
28
        'ALL <'   => 1,
29
        'ALL >'   => 1,
30
        'ALL <='  => 1,
31
        'ALL >='  => 1,
32
        'ALL IN'  => 1,
33
        'ANY =='  => 1,
34
        'ANY !='  => 1,
35
        'ANY <'   => 1,
36
        'ANY >'   => 1,
37
        'ANY <='  => 1,
38
        'ANY >='  => 1,
39
        'ANY IN'  => 1,
40
        'NONE ==' => 1,
41
        'NONE !=' => 1,
42
        'NONE <'  => 1,
43
        'NONE >'  => 1,
44
        'NONE <=' => 1,
45
        'NONE >=' => 1,
46
        'NONE IN' => 1,
47
    ];
48
49
    /**
50
     * @var array|int[]
51
     */
52
    protected array $arithmeticOperators = [
53
        '+' => 1,
54
        '-' => 1,
55
        '*' => 1,
56
        '/' => 1,
57
        '%' => 1,
58
    ];
59
60
    /**
61
     * @var array|int[]
62
     */
63
    protected array $logicalOperators = [
64
        'AND' => 1,
65
        '&&'  => 1,
66
        'OR'  => 1,
67
        '||'  => 1,
68
        'NOT' => 1,
69
        '!'   => 1,
70
    ];
71
72
    protected string $rangeOperator = '..';
73
74
    /**
75
     * Get the format for database stored dates.
76
     *
77
     * @return string
78
     */
79
    public function getDateFormat(): string
80
    {
81
        return 'Y-m-d\TH:i:s.vp';
82
    }
83
84
    /**
85
     * Get the appropriate query parameter place-holder for a value.
86
     *
87
     * @param  mixed  $value
88
     */
89
    public function parameter($value): string
90
    {
91
        return $this->isExpression($value) ? $this->getValue($value) : (string) $value;
0 ignored issues
show
Bug introduced by
It seems like getValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        return $this->isExpression($value) ? $this->/** @scrutinizer ignore-call */ getValue($value) : (string) $value;
Loading history...
Bug introduced by
It seems like isExpression() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        return $this->/** @scrutinizer ignore-call */ isExpression($value) ? $this->getValue($value) : (string) $value;
Loading history...
92
    }
93
94
95
    /**
96
     * Quote the given string literal.
97
     *
98
     * @param  string|array  $value
99
     * @return string
100
     */
101
    public function quoteString($value)
102
    {
103
        if (is_array($value)) {
104
            return implode(', ', array_map([$this, __FUNCTION__], $value));
105
        }
106
107
        return "`$value`";
108
    }
109
110
    /**
111
     * Wrap a table in keyword identifiers.
112
     *
113
     * @param  \Illuminate\Database\Query\Expression|string  $table
114
     * @return string
115
     */
116
    public function wrapTable($table)
117
    {
118
        if (! $this->isExpression($table)) {
119
            return $this->tablePrefix.$table;
120
//            return $this->wrap($this->tablePrefix.$table, true);
121
        }
122
123
        return $this->getValue($table);
124
    }
125
126
    /**
127
     * Wrap a single string in keyword identifiers.
128
     *
129
     * @param  string  $value
130
     * @return string
131
     */
132
    protected function wrapValue($value)
133
    {
134
        if ($value === '*') {
135
            return $value;
136
        }
137
138
        return '`'.str_replace('`', '``', $value).'`';
139
    }
140
141
142
143
}
144