Passed
Pull Request — master (#37)
by Timon
03:14
created

Row::and()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
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\LowerThanLogic;
14
use TBolier\RethinkQL\Query\Logic\NotEqualLogic;
15
use TBolier\RethinkQL\RethinkInterface;
16
17
class Row extends AbstractQuery implements RowInterface
18
{
19
    /**
20
     * @var QueryInterface
21
     */
22
    private $function;
23
24
    /**
25
     * @var QueryInterface
26
     */
27
    private $query;
28
29
    /**
30
     * @var string
31
     */
32
    private $value;
33
34
    /**
35
     * @param RethinkInterface $rethink
36
     * @param MessageInterface $message
37
     * @param string $value
38
     */
39
    public function __construct(
40
        RethinkInterface $rethink,
41
        MessageInterface $message,
42
        string $value
43
    ) {
44
        $this->value = $value;
45
        $this->rethink = $rethink;
46
        $this->message = $message;
47
    }
48
49
    /**
50
     * @inheritdoc
51
     * @throws QueryException
52
     */
53
    public function eq($value): RowInterface
54
    {
55
        if (!\is_scalar($value)) {
56
            throw new QueryException('Only scalar types are supported for lower than manipulations.');
57
        }
58
59
        $this->function = new EqualLogic(
60
            $this->rethink,
61
            $this->message,
62
            new GetFieldLogic($this->rethink, $this->message, $this->value),
63
            $value
64
        );
65
66
        $this->query = new FuncLogic(
67
            $this->rethink,
68
            $this->message,
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
            $this->message,
88
            new GetFieldLogic($this->rethink, $this->message, $this->value),
89
            $value
90
        );
91
92
        $this->query = new FuncLogic(
93
            $this->rethink,
94
            $this->message,
95
            $this->function
96
        );
97
98
        return $this;
99
    }
100
101
    /**
102
     * @inheritdoc
103
     * @throws QueryException
104
     */
105
    public function lt($value): RowInterface
106
    {
107
        if (!\is_scalar($value)) {
108
            throw new QueryException('Only scalar types are supported for lower than manipulations.');
109
        }
110
111
        $this->function = new LowerThanLogic(
112
            $this->rethink,
113
            $this->message,
114
            new GetFieldLogic($this->rethink, $this->message, $this->value),
115
            $value
116
        );
117
118
        $this->query = new FuncLogic(
119
            $this->rethink,
120
            $this->message,
121
            $this->function
122
        );
123
124
        return $this;
125
    }
126
127
    /**
128
     * @inheritdoc
129
     * @throws QueryException
130
     */
131
    public function gt($value): RowInterface
132
    {
133
        if (!\is_scalar($value)) {
134
            throw new QueryException('Only scalar types are supported for greater than manipulations.');
135
        }
136
137
        $this->function = new GreaterThanLogic(
138
            $this->rethink,
139
            $this->message,
140
            new GetFieldLogic($this->rethink, $this->message, $this->value),
141
            $value
142
        );
143
144
        $this->query = new FuncLogic(
145
            $this->rethink,
146
            $this->message,
147
            $this->function
148
        );
149
150
        return $this;
151
    }
152
153
    /**
154
     * @param RowInterface $row
155
     * @return RowInterface
156
     */
157
    public function and(RowInterface $row): RowInterface
158
    {
159
        $this->query = new FuncLogic(
160
            $this->rethink,
161
            $this->message,
162
            new AndLogic(
163
                $this->rethink,
164
                $this->message,
165
                $this->function,
166
                $row->getFunction()
167
            )
168
        );
169
170
        return $this;
171
    }
172
173
    /**
174
     * @inheritdoc
175
     */
176
    public function toArray(): array
177
    {
178
        return $this->query->toArray();
179
    }
180
181
    /**
182
     * @return QueryInterface
183
     */
184
    public function getFunction(): QueryInterface
185
    {
186
        return $this->function;
187
    }
188
}
189