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

WhereJsonTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 182
rs 10
wmc 16

12 Methods

Rating   Name   Duplication   Size   Complexity  
A whereJsonContains() 0 10 2
A orWhereJsonContains() 0 10 2
A whereJson() 0 10 1
A andWhereJson() 0 3 1
A orWhereJson() 0 10 1
A andWhereJsonLength() 0 3 1
A orWhereJsonDoesntContain() 0 10 2
A andWhereJsonContains() 0 3 1
A orWhereJsonLength() 0 10 1
A whereJsonLength() 0 10 1
A andWhereJsonDoesntContain() 0 3 1
A whereJsonDoesntContain() 0 10 2
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\CompileJsonDoesntContain;
17
use Cycle\Database\Driver\MySQL\Injection\CompileJsonLength;
18
19
/**
20
 * @internal
21
 */
22
trait WhereJsonTrait
23
{
24
    /**
25
     * @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...
26
     *
27
     * @return $this|self
28
     */
29
    public function whereJson(string $column, mixed $value): self
30
    {
31
        $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

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

35
            $this->/** @scrutinizer ignore-call */ 
36
                   whereWrapper()
Loading history...
36
        );
37
38
        return $this;
39
    }
40
41
    /**
42
     * @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...
43
     *
44
     * @return $this|self
45
     */
46
    public function andWhereJson(string $column, mixed $value): self
47
    {
48
        return $this->whereJson($column, $value);
49
    }
50
51
    /**
52
     * @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...
53
     *
54
     * @return $this|self
55
     */
56
    public function orWhereJson(string $column, mixed $value): self
57
    {
58
        $this->registerToken(
59
            'OR',
60
            [new CompileJson($column), $value],
61
            $this->whereTokens,
62
            $this->whereWrapper()
63
        );
64
65
        return $this;
66
    }
67
68
    /**
69
     * @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...
70
     *
71
     * @return $this|self
72
     */
73
    public function whereJsonContains(string $column, mixed $value): self
74
    {
75
        $this->registerToken(
76
            'AND',
77
            [new CompileJsonContains($column, json_validate($value) ? $value : \json_encode($value))],
78
            $this->whereTokens,
79
            $this->whereWrapper()
80
        );
81
82
        return $this;
83
    }
84
85
    /**
86
     * @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...
87
     *
88
     * @return $this|self
89
     */
90
    public function andWhereJsonContains(string $column, mixed $value): self
91
    {
92
        return $this->whereJsonContains($column, $value);
93
    }
94
95
    /**
96
     * @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...
97
     *
98
     * @return $this|self
99
     */
100
    public function orWhereJsonContains(string $column, mixed $value): self
101
    {
102
        $this->registerToken(
103
            'OR',
104
            [new CompileJsonContains($column, json_validate($value) ? $value : \json_encode($value))],
105
            $this->whereTokens,
106
            $this->whereWrapper()
107
        );
108
109
        return $this;
110
    }
111
112
    /**
113
     * @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...
114
     *
115
     * @return $this|self
116
     */
117
    public function whereJsonDoesntContain(string $column, mixed $value): self
118
    {
119
        $this->registerToken(
120
            'AND',
121
            [new CompileJsonDoesntContain($column, json_validate($value) ? $value : \json_encode($value))],
122
            $this->whereTokens,
123
            $this->whereWrapper()
124
        );
125
126
        return $this;
127
    }
128
129
    /**
130
     * @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...
131
     *
132
     * @return $this|self
133
     */
134
    public function andWhereJsonDoesntContain(string $column, mixed $value): self
135
    {
136
        return $this->whereJsonDoesntContain($column, $value);
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
     * @return $this|self
143
     */
144
    public function orWhereJsonDoesntContain(string $column, mixed $value): self
145
    {
146
        $this->registerToken(
147
            'OR',
148
            [new CompileJsonDoesntContain($column, json_validate($value) ? $value : \json_encode($value))],
149
            $this->whereTokens,
150
            $this->whereWrapper()
151
        );
152
153
        return $this;
154
    }
155
156
    /**
157
     * @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...
158
     * @param 0|positive-int $length
159
     * @param non-empty-string $operator
160
     *
161
     * @return $this|self
162
     */
163
    public function whereJsonLength(string $column, int $length, string $operator = '='): self
164
    {
165
        $this->registerToken(
166
            'AND',
167
            [new CompileJsonLength($column, $length, $operator)],
168
            $this->whereTokens,
169
            $this->whereWrapper()
170
        );
171
172
        return $this;
173
    }
174
175
    /**
176
     * @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...
177
     * @param 0|positive-int $length
178
     * @param non-empty-string $operator
179
     *
180
     * @return $this|self
181
     */
182
    public function andWhereJsonLength(string $column, int $length, string $operator = '='): self
183
    {
184
        return $this->whereJsonLength($column, $length, $operator);
185
    }
186
187
    /**
188
     * @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...
189
     * @param 0|positive-int $length
190
     * @param non-empty-string $operator
191
     *
192
     * @return $this|self
193
     */
194
    public function orWhereJsonLength(string $column, int $length, string $operator = '='): self
195
    {
196
        $this->registerToken(
197
            'OR',
198
            [new CompileJsonLength($column, $length, $operator)],
199
            $this->whereTokens,
200
            $this->whereWrapper()
201
        );
202
203
        return $this;
204
    }
205
}
206