Passed
Pull Request — 2.x (#135)
by Aleksei
16:08
created

WhereJsonTrait::whereJsonContains()   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 4
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\Jsoner;
15
use Cycle\Database\Driver\MySQL\Injection\CompileJson;
16
use Cycle\Database\Driver\MySQL\Injection\CompileJsonContains;
17
use Cycle\Database\Driver\MySQL\Injection\CompileJsonContainsKey;
18
use Cycle\Database\Driver\MySQL\Injection\CompileJsonDoesntContain;
19
use Cycle\Database\Driver\MySQL\Injection\CompileJsonDoesntContainKey;
20
use Cycle\Database\Driver\MySQL\Injection\CompileJsonLength;
21
22
/**
23
 * @internal
24
 *
25
 * @psalm-internal Cycle\Database\Driver\MySQL
26
 */
27
trait WhereJsonTrait
28
{
29
    /**
30
     * @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...
31
     */
32
    public function whereJson(string $column, mixed $value): static
33
    {
34
        $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

34
        $this->/** @scrutinizer ignore-call */ 
35
               registerToken(
Loading history...
35
            'AND',
36
            [new CompileJson($column), $value],
37
            $this->whereTokens,
38
            $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

38
            $this->/** @scrutinizer ignore-call */ 
39
                   whereWrapper()
Loading history...
39
        );
40
41
        return $this;
42
    }
43
44
    /**
45
     * @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...
46
     */
47
    public function orWhereJson(string $column, mixed $value): static
48
    {
49
        $this->registerToken(
50
            'OR',
51
            [new CompileJson($column), $value],
52
            $this->whereTokens,
53
            $this->whereWrapper()
54
        );
55
56
        return $this;
57
    }
58
59
    /**
60
     * @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...
61
     * @param bool $encode Encode the value into JSON.
62
     * @param bool $validate Check that $value is a valid JSON string if the $encode parameter is false.
63
     */
64
    public function whereJsonContains(string $column, mixed $value, bool $encode = true, bool $validate = true): static
65
    {
66
        $this->registerToken(
67
            'AND',
68
            [new CompileJsonContains($column, Jsoner::toJson($value, $encode, $validate))],
69
            $this->whereTokens,
70
            $this->whereWrapper()
71
        );
72
73
        return $this;
74
    }
75
76
    /**
77
     * @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...
78
     * @param bool $encode Encode the value into JSON.
79
     * @param bool $validate Check that $value is a valid JSON string if the $encode parameter is false.
80
     */
81
    public function orWhereJsonContains(
82
        string $column,
83
        mixed $value,
84
        bool $encode = true,
85
        bool $validate = true
86
    ): static {
87
        $this->registerToken(
88
            'OR',
89
            [new CompileJsonContains($column, Jsoner::toJson($value, $encode, $validate))],
90
            $this->whereTokens,
91
            $this->whereWrapper()
92
        );
93
94
        return $this;
95
    }
96
97
    /**
98
     * @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...
99
     * @param bool $encode Encode the value into JSON.
100
     * @param bool $validate Check that $value is a valid JSON string if the $encode parameter is false.
101
     */
102
    public function whereJsonDoesntContain(
103
        string $column,
104
        mixed $value,
105
        bool $encode = true,
106
        bool $validate = true
107
    ): static {
108
        $this->registerToken(
109
            'AND',
110
            [new CompileJsonDoesntContain($column, Jsoner::toJson($value, $encode, $validate))],
111
            $this->whereTokens,
112
            $this->whereWrapper()
113
        );
114
115
        return $this;
116
    }
117
118
    /**
119
     * @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...
120
     * @param bool $encode Encode the value into JSON.
121
     * @param bool $validate Check that $value is a valid JSON string if the $encode parameter is false.
122
     */
123
    public function orWhereJsonDoesntContain(
124
        string $column,
125
        mixed $value,
126
        bool $encode = true,
127
        bool $validate = true
128
    ): static {
129
        $this->registerToken(
130
            'OR',
131
            [new CompileJsonDoesntContain($column, Jsoner::toJson($value, $encode, $validate))],
132
            $this->whereTokens,
133
            $this->whereWrapper()
134
        );
135
136
        return $this;
137
    }
138
139
    /**
140
     * @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...
141
     */
142
    public function whereJsonContainsKey(string $column): static
143
    {
144
        $this->registerToken(
145
            'AND',
146
            [new CompileJsonContainsKey($column)],
147
            $this->whereTokens,
148
            $this->whereWrapper()
149
        );
150
151
        return $this;
152
    }
153
154
    /**
155
     * @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...
156
     */
157
    public function orWhereJsonContainsKey(string $column): static
158
    {
159
        $this->registerToken(
160
            'OR',
161
            [new CompileJsonContainsKey($column)],
162
            $this->whereTokens,
163
            $this->whereWrapper()
164
        );
165
166
        return $this;
167
    }
168
169
    /**
170
     * @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...
171
     */
172
    public function whereJsonDoesntContainKey(string $column): static
173
    {
174
        $this->registerToken(
175
            'AND',
176
            [new CompileJsonDoesntContainKey($column)],
177
            $this->whereTokens,
178
            $this->whereWrapper()
179
        );
180
181
        return $this;
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
    public function orWhereJsonDoesntContainKey(string $column): static
188
    {
189
        $this->registerToken(
190
            'OR',
191
            [new CompileJsonDoesntContainKey($column)],
192
            $this->whereTokens,
193
            $this->whereWrapper()
194
        );
195
196
        return $this;
197
    }
198
199
    /**
200
     * @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...
201
     * @param int<0, max> $length
202
     * @param non-empty-string $operator
203
     */
204
    public function whereJsonLength(string $column, int $length, string $operator = '='): static
205
    {
206
        $this->registerToken(
207
            'AND',
208
            [new CompileJsonLength($column, $length, $operator)],
209
            $this->whereTokens,
210
            $this->whereWrapper()
211
        );
212
213
        return $this;
214
    }
215
216
    /**
217
     * @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...
218
     * @param int<0, max> $length
219
     * @param non-empty-string $operator
220
     */
221
    public function orWhereJsonLength(string $column, int $length, string $operator = '='): static
222
    {
223
        $this->registerToken(
224
            'OR',
225
            [new CompileJsonLength($column, $length, $operator)],
226
            $this->whereTokens,
227
            $this->whereWrapper()
228
        );
229
230
        return $this;
231
    }
232
}
233