Completed
Push — master ( 3a5af3...5969bf )
by Dmitriy
05:50
created

Criteria::isNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace T4webInfrastructure;
4
5
use T4webDomainInterface\Infrastructure\CriteriaInterface;
6
use Zend\Db\Sql\Select;
7
8
class Criteria implements CriteriaInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: getLimit, getOffset, getOr, getOrder, getPredicate, getRelations, orCriteria
Loading history...
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $entityName;
14
15
    /**
16
     * @var Config
17
     */
18
    protected $config;
19
20
    /**
21
     * @var Select
22
     */
23
    protected $select;
24
25
    /**
26
     * @param string $entityName
27
     */
28
    public function __construct($entityName, Config $config, Select $select = null)
29
    {
30
        $this->entityName = $entityName;
31
        $this->config = $config;
32
        if ($select === null) {
33
            $this->select = new Select();
34
            $this->select->from($this->config->getTable($this->entityName));
35
        } else {
36
            $this->select = $select;
37
        }
38
    }
39
40
    /**
41
     * @return Select
42
     */
43
    public function getQuery()
44
    {
45
        return $this->select;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getEntityName()
52
    {
53
        return $this->entityName;
54
    }
55
56
    /**
57
     * @param string $entityName
58
     * @return CriteriaInterface
59
     */
60
    public function relation($entityName)
61
    {
62
        if ($this->config->isRelationManyToMany($this->entityName, $entityName)) {
63
64
            list($linkTable, $mainField, $joinedField) = $this->config->getRelationManyToMany($this->entityName, $entityName);
65
66
            $mainTable = $this->config->getTable($this->entityName);
67
            $joinedTable = $this->config->getTable($entityName);
68
69
            $this->select->join(
70
                $linkTable,
71
                "$linkTable.$mainField = $mainTable.id",
72
                []
73
            );
74
75
            $this->select->join(
76
                $joinedTable,
77
                "$linkTable.$joinedField = $joinedTable.id",
78
                []
79
            );
80
81
        } else {
82
            $table = $this->config->getTable($entityName);
83
84
            $this->select->join(
85
                $table,
86
                $this->config->getRelationExpression($this->entityName, $entityName),
87
                []
88
            );
89
        }
90
91
        $relationCriteria = new self($entityName, $this->config, $this->select);
92
93
        return $relationCriteria;
94
    }
95
96
    /**
97
     * @param string $attribute
98
     * @param bool|int|float|string $value
99
     * @return $this
100
     */
101
    public function equalTo($attribute, $value)
102
    {
103
        $this->select->where->equalTo($this->getField($attribute), $value);
104
105
        return $this;
106
    }
107
108
    /**
109
     * @param string $attribute
110
     * @param bool|int|float|string $value
111
     * @return $this
112
     */
113
    public function notEqualTo($attribute, $value)
114
    {
115
        $this->select->where->notEqualTo($this->getField($attribute), $value);
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param string $attribute
122
     * @param int|float $value
123
     * @return $this
124
     */
125
    public function lessThan($attribute, $value)
126
    {
127
        $this->select->where->lessThan($this->getField($attribute), $value);
128
129
        return $this;
130
    }
131
132
    /**
133
     * @param string $attribute
134
     * @param int|float $value
135
     * @return $this
136
     */
137
    public function greaterThan($attribute, $value)
138
    {
139
        $this->select->where->greaterThan($this->getField($attribute), $value);
140
141
        return $this;
142
    }
143
144
    /**
145
     * @param string $attribute
146
     * @param int|float $value
147
     * @return $this
148
     */
149
    public function greaterThanOrEqualTo($attribute, $value)
150
    {
151
        $this->select->where->greaterThanOrEqualTo($this->getField($attribute), $value);
152
153
        return $this;
154
    }
155
156
    /**
157
     * @param string $attribute
158
     * @param int|float $value
159
     * @return $this
160
     */
161
    public function lessThanOrEqualTo($attribute, $value)
162
    {
163
        $this->select->where->lessThanOrEqualTo($this->getField($attribute), $value);
164
165
        return $this;
166
    }
167
168
    /**
169
     * @param string $attribute
170
     * @param int|float $value
171
     * @return $this
172
     */
173
    public function like($attribute, $value)
174
    {
175
        $this->select->where->like($this->getField($attribute), $value);
176
177
        return $this;
178
    }
179
180
    /**
181
     * @param string $attribute
182
     * @return $this
183
     */
184
    public function isNull($attribute)
185
    {
186
        $this->select->where->isNull($this->getField($attribute));
187
188
        return $this;
189
    }
190
191
    /**
192
     * @param string $attribute
193
     * @return $this
194
     */
195
    public function isNotNull($attribute)
196
    {
197
        $this->select->where->isNotNull($this->getField($attribute));
198
199
        return $this;
200
    }
201
202
    /**
203
     * @param string $attribute
204
     * @param array $values
205
     * @return $this
206
     */
207
    public function in($attribute, array $values)
208
    {
209
        $this->select->where->in($this->getField($attribute), $values);
210
211
        return $this;
212
    }
213
214
    /**
215
     * @param string $attribute
216
     * @param int|float|string $minValue
217
     * @param int|float|string $maxValue
218
     * @return $this
219
     */
220
    public function between($attribute, $minValue, $maxValue)
221
    {
222
        $this->select->where->between($this->getField($attribute), $minValue, $maxValue);
223
224
        return $this;
225
    }
226
227
    /**
228
     * @param string $attribute
229
     * @return $this
230
     */
231
    public function order($attribute)
232
    {
233
        $exploded = explode(' ', $attribute);
234
        if (count($exploded) == 2) {
235
            $order = $this->getField($exploded[0]) . ' ' . $exploded[1];
236
        } else {
237
            $order = $attribute;
238
        }
239
240
        $this->select->order($order);
241
242
        return $this;
243
    }
244
245
    /**
246
     * @param int $limit
247
     * @return $this
248
     */
249
    public function limit($limit)
250
    {
251
        $this->select->limit($limit);
252
253
        return $this;
254
    }
255
256
    /**
257
     * @param int $offset
258
     * @return $this
259
     */
260
    public function offset($offset)
261
    {
262
        $this->select->offset($offset);
263
264
        return $this;
265
    }
266
267
    /**
268
     * @param $attribute
269
     * @return string
270
     */
271
    public function getField($attribute)
272
    {
273
        $table = $this->config->getTable($this->entityName);
274
        $field = $this->config->getFiled($this->entityName, $attribute);
275
276
        return $table.".".$field;
277
    }
278
}