Test Failed
Push — master ( 3b363c...e4d46c )
by Timon
05:21
created

Row::le()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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