Passed
Pull Request — 2.x (#135)
by Maxim
19:43
created

WhereJsonTrait::whereJsonContainsKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 * This file is part of Cycle ORM package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Database\Driver\MySQL\Query\Traits;
13
14
use Cycle\Database\Driver\MySQL\Injection\CompileJson;
15
use Cycle\Database\Driver\MySQL\Injection\CompileJsonContains;
16
use Cycle\Database\Driver\MySQL\Injection\CompileJsonContainsKey;
17
use Cycle\Database\Driver\MySQL\Injection\CompileJsonDoesntContain;
18
use Cycle\Database\Driver\MySQL\Injection\CompileJsonLength;
19
20
/**
21
 * @internal
22
 */
23
trait WhereJsonTrait
24
{
25
    /**
26
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
27
     *
28
     * @return $this|self
29
     */
30
    public function whereJson(string $column, mixed $value): self
31
    {
32
        $this->registerToken(
0 ignored issues
show
Bug introduced by
It seems like registerToken() 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

32
        $this->/** @scrutinizer ignore-call */ 
33
               registerToken(
Loading history...
33
            'AND',
34
            [new CompileJson($column), $value],
35
            $this->whereTokens,
36
            $this->whereWrapper()
0 ignored issues
show
Bug introduced by
It seems like whereWrapper() 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

36
            $this->/** @scrutinizer ignore-call */ 
37
                   whereWrapper()
Loading history...
37
        );
38
39
        return $this;
40
    }
41
42
    /**
43
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
44
     *
45
     * @return $this|self
46
     */
47
    public function andWhereJson(string $column, mixed $value): self
48
    {
49
        return $this->whereJson($column, $value);
50
    }
51
52
    /**
53
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
54
     *
55
     * @return $this|self
56
     */
57
    public function orWhereJson(string $column, mixed $value): self
58
    {
59
        $this->registerToken(
60
            'OR',
61
            [new CompileJson($column), $value],
62
            $this->whereTokens,
63
            $this->whereWrapper()
64
        );
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
71
     *
72
     * @return $this|self
73
     */
74
    public function whereJsonContains(string $column, mixed $value): self
75
    {
76
        $this->registerToken(
77
            'AND',
78
            [new CompileJsonContains($column, json_validate($value) ? $value : \json_encode($value))],
79
            $this->whereTokens,
80
            $this->whereWrapper()
81
        );
82
83
        return $this;
84
    }
85
86
    /**
87
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
88
     *
89
     * @return $this|self
90
     */
91
    public function andWhereJsonContains(string $column, mixed $value): self
92
    {
93
        return $this->whereJsonContains($column, $value);
94
    }
95
96
    /**
97
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
98
     *
99
     * @return $this|self
100
     */
101
    public function orWhereJsonContains(string $column, mixed $value): self
102
    {
103
        $this->registerToken(
104
            'OR',
105
            [new CompileJsonContains($column, json_validate($value) ? $value : \json_encode($value))],
106
            $this->whereTokens,
107
            $this->whereWrapper()
108
        );
109
110
        return $this;
111
    }
112
113
    /**
114
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
115
     *
116
     * @return $this|self
117
     */
118
    public function whereJsonDoesntContain(string $column, mixed $value): self
119
    {
120
        $this->registerToken(
121
            'AND',
122
            [new CompileJsonDoesntContain($column, json_validate($value) ? $value : \json_encode($value))],
123
            $this->whereTokens,
124
            $this->whereWrapper()
125
        );
126
127
        return $this;
128
    }
129
130
    /**
131
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
132
     *
133
     * @return $this|self
134
     */
135
    public function andWhereJsonDoesntContain(string $column, mixed $value): self
136
    {
137
        return $this->whereJsonDoesntContain($column, $value);
138
    }
139
140
    /**
141
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
142
     *
143
     * @return $this|self
144
     */
145
    public function orWhereJsonDoesntContain(string $column, mixed $value): self
146
    {
147
        $this->registerToken(
148
            'OR',
149
            [new CompileJsonDoesntContain($column, json_validate($value) ? $value : \json_encode($value))],
150
            $this->whereTokens,
151
            $this->whereWrapper()
152
        );
153
154
        return $this;
155
    }
156
157
    /**
158
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
159
     *
160
     * @return $this|self
161
     */
162
    public function whereJsonContainsKey(string $column): self
163
    {
164
        $this->registerToken(
165
            'AND',
166
            [new CompileJsonContainsKey($column)],
167
            $this->whereTokens,
168
            $this->whereWrapper()
169
        );
170
171
        return $this;
172
    }
173
174
    /**
175
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
176
     *
177
     * @return $this|self
178
     */
179
    public function andWhereJsonContainsKey(string $column): self
180
    {
181
        return $this->whereJsonContainsKey($column);
182
    }
183
184
    /**
185
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
186
     *
187
     * @return $this|self
188
     */
189
    public function orWhereJsonContainsKey(string $column): self
190
    {
191
        $this->registerToken(
192
            'OR',
193
            [new CompileJsonContainsKey($column)],
194
            $this->whereTokens,
195
            $this->whereWrapper()
196
        );
197
198
        return $this;
199
    }
200
201
    /**
202
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
203
     * @param 0|positive-int $length
204
     * @param non-empty-string $operator
205
     *
206
     * @return $this|self
207
     */
208
    public function whereJsonLength(string $column, int $length, string $operator = '='): self
209
    {
210
        $this->registerToken(
211
            'AND',
212
            [new CompileJsonLength($column, $length, $operator)],
213
            $this->whereTokens,
214
            $this->whereWrapper()
215
        );
216
217
        return $this;
218
    }
219
220
    /**
221
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
222
     * @param 0|positive-int $length
223
     * @param non-empty-string $operator
224
     *
225
     * @return $this|self
226
     */
227
    public function andWhereJsonLength(string $column, int $length, string $operator = '='): self
228
    {
229
        return $this->whereJsonLength($column, $length, $operator);
230
    }
231
232
    /**
233
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
234
     * @param 0|positive-int $length
235
     * @param non-empty-string $operator
236
     *
237
     * @return $this|self
238
     */
239
    public function orWhereJsonLength(string $column, int $length, string $operator = '='): self
240
    {
241
        $this->registerToken(
242
            'OR',
243
            [new CompileJsonLength($column, $length, $operator)],
244
            $this->whereTokens,
245
            $this->whereWrapper()
246
        );
247
248
        return $this;
249
    }
250
}
251