Passed
Pull Request — 2.x (#135)
by Maxim
20:06
created

WhereJsonTrait::orWhereJsonContains()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 1
nop 2
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\CompileJsonDoesntContain;
17
18
/**
19
 * @internal
20
 */
21
trait WhereJsonTrait
22
{
23
    /**
24
     * @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...
25
     *
26
     * @return $this|self
27
     */
28
    public function whereJson(string $column, mixed $value): self
29
    {
30
        $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

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

34
            $this->/** @scrutinizer ignore-call */ 
35
                   whereWrapper()
Loading history...
35
        );
36
37
        return $this;
38
    }
39
40
    /**
41
     * @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...
42
     *
43
     * @return $this|self
44
     */
45
    public function andWhereJson(string $column, mixed $value): self
46
    {
47
        $this->registerToken(
48
            'AND',
49
            [new CompileJson($column), $value],
50
            $this->whereTokens,
51
            $this->whereWrapper()
52
        );
53
54
        return $this;
55
    }
56
57
    /**
58
     * @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...
59
     *
60
     * @return $this|self
61
     */
62
    public function orWhereJson(string $column, mixed $value): self
63
    {
64
        $this->registerToken(
65
            'OR',
66
            [new CompileJson($column), $value],
67
            $this->whereTokens,
68
            $this->whereWrapper()
69
        );
70
71
        return $this;
72
    }
73
74
    /**
75
     * @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...
76
     *
77
     * @return $this|self
78
     */
79
    public function whereJsonContains(string $column, mixed $value): self
80
    {
81
        $this->registerToken(
82
            'AND',
83
            [new CompileJsonContains($column, json_validate($value) ? $value : \json_encode($value))],
84
            $this->whereTokens,
85
            $this->whereWrapper()
86
        );
87
88
        return $this;
89
    }
90
91
    /**
92
     * @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...
93
     *
94
     * @return $this|self
95
     */
96
    public function andWhereJsonContains(string $column, mixed $value): self
97
    {
98
        return $this->whereJsonContains($column, $value);
99
    }
100
101
    /**
102
     * @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...
103
     *
104
     * @return $this|self
105
     */
106
    public function orWhereJsonContains(string $column, mixed $value): self
107
    {
108
        $this->registerToken(
109
            'OR',
110
            [new CompileJsonContains($column, json_validate($value) ? $value : \json_encode($value))],
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
     *
121
     * @return $this|self
122
     */
123
    public function whereJsonDoesntContain(string $column, mixed $value): self
124
    {
125
        $this->registerToken(
126
            'AND',
127
            [new CompileJsonDoesntContain($column, json_validate($value) ? $value : \json_encode($value))],
128
            $this->whereTokens,
129
            $this->whereWrapper()
130
        );
131
132
        return $this;
133
    }
134
135
    /**
136
     * @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...
137
     *
138
     * @return $this|self
139
     */
140
    public function andWhereJsonDoesntContain(string $column, mixed $value): self
141
    {
142
        return $this->whereJsonDoesntContain($column, $value);
143
    }
144
145
    /**
146
     * @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...
147
     *
148
     * @return $this|self
149
     */
150
    public function orWhereJsonDoesntContain(string $column, mixed $value): self
151
    {
152
        $this->registerToken(
153
            'OR',
154
            [new CompileJsonDoesntContain($column, json_validate($value) ? $value : \json_encode($value))],
155
            $this->whereTokens,
156
            $this->whereWrapper()
157
        );
158
159
        return $this;
160
    }
161
}
162