Row::and()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
c 0
b 0
f 0
rs 9.9666
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TBolier\RethinkQL\Query;
5
6
use TBolier\RethinkQL\Query\Exception\QueryException;
7
use TBolier\RethinkQL\Query\Logic\AndLogic;
8
use TBolier\RethinkQL\Query\Logic\EqualLogic;
9
use TBolier\RethinkQL\Query\Logic\FuncLogic;
10
use TBolier\RethinkQL\Query\Logic\GreaterThanLogic;
11
use TBolier\RethinkQL\Query\Logic\GreaterThanOrEqualToLogic;
12
use TBolier\RethinkQL\Query\Logic\LowerThanLogic;
13
use TBolier\RethinkQL\Query\Logic\LowerThanOrEqualToLogic;
14
use TBolier\RethinkQL\Query\Logic\NotEqualLogic;
15
use TBolier\RethinkQL\Query\Logic\NotLogic;
16
use TBolier\RethinkQL\Query\Logic\OrLogic;
17
use TBolier\RethinkQL\Query\Manipulation\GetField;
18
use TBolier\RethinkQL\Query\Manipulation\RowHasFields;
19
use TBolier\RethinkQL\Query\Manipulation\ManipulationTrait;
20
use TBolier\RethinkQL\RethinkInterface;
21
22
class Row extends AbstractQuery
23
{
24
    use ManipulationTrait;
25
26
    /**
27
     * @var QueryInterface
28
     */
29
    private $function;
30
31
    /**
32
     * @var QueryInterface
33
     */
34
    private $query;
35
36
    /**
37
     * @var string
38
     */
39
    private $value;
40
41
    public function __construct(
42
        RethinkInterface $rethink,
43
        ?string $value
44
    ) {
45
        parent::__construct($rethink);
46
47
        $this->value = $value;
48
        $this->rethink = $rethink;
49
    }
50
51
    /**
52
     * @throws QueryException
53
     */
54
    public function eq($value): Row
55
    {
56
        if (!\is_scalar($value)) {
57
            throw new QueryException('Only scalar types are supported for lower than manipulations.');
58
        }
59
60
        $this->function = new EqualLogic(
61
            $this->rethink,
62
            new GetField($this->rethink, $this->value),
63
            $value
64
        );
65
66
        $this->query = new FuncLogic(
67
            $this->rethink,
68
            $this->function
69
        );
70
71
        return $this;
72
    }
73
74
    /**
75
     * @throws QueryException
76
     */
77
    public function ne($value): Row
78
    {
79
        if (!\is_scalar($value)) {
80
            throw new QueryException('Only scalar types are supported for lower than manipulations.');
81
        }
82
83
        $this->function = new NotEqualLogic(
84
            $this->rethink,
85
            new GetField($this->rethink, $this->value),
86
            $value
87
        );
88
89
        $this->query = new FuncLogic(
90
            $this->rethink,
91
            $this->function
92
        );
93
94
        return $this;
95
    }
96
97
    /**
98
     * @throws QueryException
99
     */
100
    public function lt($value): Row
101
    {
102
        if (!\is_scalar($value)) {
103
            throw new QueryException('Only scalar types are supported for lower than manipulations.');
104
        }
105
106
        $this->function = new LowerThanLogic(
107
            $this->rethink,
108
            new GetField($this->rethink, $this->value),
109
            $value
110
        );
111
112
        $this->query = new FuncLogic(
113
            $this->rethink,
114
            $this->function
115
        );
116
117
        return $this;
118
    }
119
120
    /**
121
     * @throws QueryException
122
     */
123
    public function le($value): Row
124
    {
125
        if (!\is_scalar($value)) {
126
            throw new QueryException('Only scalar types are supported for lower than or equal manipulations.');
127
        }
128
129
        $this->function = new LowerThanOrEqualToLogic(
130
            $this->rethink,
131
            new GetField($this->rethink, $this->value),
132
            $value
133
        );
134
135
        $this->query = new FuncLogic(
136
            $this->rethink,
137
            $this->function
138
        );
139
140
        return $this;
141
    }
142
143
    /**
144
     * @throws QueryException
145
     */
146
    public function gt($value): Row
147
    {
148
        if (!\is_scalar($value)) {
149
            throw new QueryException('Only scalar types are supported for greater than manipulations.');
150
        }
151
152
        $this->function = new GreaterThanLogic(
153
            $this->rethink,
154
            new GetField($this->rethink, $this->value),
155
            $value
156
        );
157
158
        $this->query = new FuncLogic(
159
            $this->rethink,
160
            $this->function
161
        );
162
163
        return $this;
164
    }
165
166
    /**
167
     * @throws QueryException
168
     */
169
    public function ge($value): Row
170
    {
171
        if (!\is_scalar($value)) {
172
            throw new QueryException('Only scalar types are supported for greater than or equal manipulations.');
173
        }
174
175
        $this->function = new GreaterThanOrEqualToLogic(
176
            $this->rethink,
177
            new GetField($this->rethink, $this->value),
178
            $value
179
        );
180
181
        $this->query = new FuncLogic(
182
            $this->rethink,
183
            $this->function
184
        );
185
186
        return $this;
187
    }
188
189
    public function and(Row $row): Row
190
    {
191
        $this->function = new AndLogic(
192
            $this->rethink,
193
            $this->function,
194
            $row->getFunction()
195
        );
196
197
        $this->query = new FuncLogic(
198
            $this->rethink,
199
            $this->function
200
        );
201
202
        return $this;
203
    }
204
205
    public function or(Row $row): Row
206
    {
207
        $this->function = new OrLogic(
208
            $this->rethink,
209
            $this->function,
210
            $row->getFunction()
211
        );
212
213
        $this->query = new FuncLogic(
214
            $this->rethink,
215
            $this->function
216
        );
217
218
        return $this;
219
    }
220
221
    public function not(): Row
222
    {
223
        $this->function = new NotLogic(
224
            $this->rethink,
225
            $this->function
226
        );
227
228
        $this->query = new FuncLogic(
229
            $this->rethink,
230
            $this->function
231
        );
232
233
        return $this;
234
    }
235
236
    public function hasFields(...$keys)
237
    {
238
        $this->function = new RowHasFields(
239
            $this->rethink,
240
            $keys
241
        );
242
243
        $this->query = new FuncLogic(
244
            $this->rethink,
245
            $this->function
246
        );
247
248
        return $this;
249
    }
250
251
    public function toArray(): array
252
    {
253
        return $this->query->toArray();
254
    }
255
256
    public function getFunction(): QueryInterface
257
    {
258
        return $this->function;
259
    }
260
}
261