1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Core\Specification; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Selector\Callback; |
14
|
|
|
use Cubiche\Core\Selector\Count; |
15
|
|
|
use Cubiche\Core\Selector\Key; |
16
|
|
|
use Cubiche\Core\Selector\Method; |
17
|
|
|
use Cubiche\Core\Selector\Property; |
18
|
|
|
use Cubiche\Core\Selector\SelectorInterface; |
19
|
|
|
use Cubiche\Core\Selector\Value; |
20
|
|
|
use Cubiche\Core\Specification\Constraint\Equal; |
21
|
|
|
use Cubiche\Core\Specification\Constraint\GreaterThan; |
22
|
|
|
use Cubiche\Core\Specification\Constraint\GreaterThanEqual; |
23
|
|
|
use Cubiche\Core\Specification\Constraint\LessThan; |
24
|
|
|
use Cubiche\Core\Specification\Constraint\LessThanEqual; |
25
|
|
|
use Cubiche\Core\Specification\Constraint\NotEqual; |
26
|
|
|
use Cubiche\Core\Specification\Constraint\NotSame; |
27
|
|
|
use Cubiche\Core\Specification\Constraint\Same; |
28
|
|
|
use Cubiche\Core\Specification\Quantifier\All; |
29
|
|
|
use Cubiche\Core\Specification\Quantifier\AtLeast; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Selector Specification Class. |
33
|
|
|
* |
34
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class Selector extends Specification implements SelectorInterface |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var SelectorInterface |
40
|
|
|
*/ |
41
|
|
|
protected $selector; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param SelectorInterface $selector |
45
|
|
|
*/ |
46
|
|
|
public function __construct(SelectorInterface $selector) |
47
|
|
|
{ |
48
|
|
|
$this->selector = $selector; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return \Cubiche\Core\Selector\SelectorInterface |
53
|
|
|
*/ |
54
|
|
|
public function selector() |
55
|
|
|
{ |
56
|
|
|
return $this->selector; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function evaluate($value) |
63
|
|
|
{ |
64
|
|
|
return $this->apply($value) === true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function apply($value) |
71
|
|
|
{ |
72
|
|
|
return $this->selector()->apply($value); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function select($selector) |
79
|
|
|
{ |
80
|
|
|
return new self($this->selector()->select($selector)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $key |
85
|
|
|
* |
86
|
|
|
* @return \Cubiche\Core\Specification\Selector |
87
|
|
|
*/ |
88
|
|
|
public function key($key) |
89
|
|
|
{ |
90
|
|
|
return $this->select(new Key($key)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $property |
95
|
|
|
* |
96
|
|
|
* @return \Cubiche\Core\Specification\Selector |
97
|
|
|
*/ |
98
|
|
|
public function property($property) |
99
|
|
|
{ |
100
|
|
|
return $this->select(new Property($property)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $method |
105
|
|
|
* |
106
|
|
|
* @return \Cubiche\Core\Specification\Selector |
107
|
|
|
*/ |
108
|
|
|
public function method($method) |
109
|
|
|
{ |
110
|
|
|
return $this->select(new Method($method)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param callable $callable |
115
|
|
|
* |
116
|
|
|
* @return \Cubiche\Core\Specification\Selector |
117
|
|
|
*/ |
118
|
|
|
public function custom(callable $callable) |
119
|
|
|
{ |
120
|
|
|
return $this->select(new Callback($callable)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return \Cubiche\Core\Specification\Selector |
125
|
|
|
*/ |
126
|
|
|
public function count() |
127
|
|
|
{ |
128
|
|
|
return $this->select(new Count()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param SpecificationInterface $specification |
133
|
|
|
* |
134
|
|
|
* @return \Cubiche\Core\Specification\Quantifier\All |
135
|
|
|
*/ |
136
|
|
|
public function all(SpecificationInterface $specification) |
137
|
|
|
{ |
138
|
|
|
return new All($this->selector(), $specification); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param int $count |
143
|
|
|
* @param SpecificationInterface $specification |
144
|
|
|
* |
145
|
|
|
* @return \Cubiche\Core\Specification\Quantifier\AtLeast |
146
|
|
|
*/ |
147
|
|
|
public function atLeast($count, SpecificationInterface $specification) |
148
|
|
|
{ |
149
|
|
|
return new AtLeast($count, $this->selector(), $specification); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param SpecificationInterface $specification |
154
|
|
|
* |
155
|
|
|
* @return \Cubiche\Core\Specification\Quantifier\AtLeast |
156
|
|
|
*/ |
157
|
|
|
public function any(SpecificationInterface $specification) |
158
|
|
|
{ |
159
|
|
|
return $this->atLeast(1, $specification); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param SelectorInterface|mixed $value |
164
|
|
|
* |
165
|
|
|
* @return \Cubiche\Domain\Specification\Constraint\GreaterThan |
166
|
|
|
*/ |
167
|
|
|
public function gt($value) |
168
|
|
|
{ |
169
|
|
|
return new GreaterThan($this->selector(), $this->createSelector($value)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param SelectorInterface|mixed $value |
174
|
|
|
* |
175
|
|
|
* @return \Cubiche\Core\Specification\Constraint\GreaterThanEqual |
176
|
|
|
*/ |
177
|
|
|
public function gte($value) |
178
|
|
|
{ |
179
|
|
|
return new GreaterThanEqual($this->selector(), $this->createSelector($value)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param SelectorInterface|mixed $value |
184
|
|
|
* |
185
|
|
|
* @return \Cubiche\Core\Specification\Constraint\LessThan |
186
|
|
|
*/ |
187
|
|
|
public function lt($value) |
188
|
|
|
{ |
189
|
|
|
return new LessThan($this->selector(), $this->createSelector($value)); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param SelectorInterface|mixed $value |
194
|
|
|
* |
195
|
|
|
* @return \Cubiche\Core\Specification\Constraint\LessThanEqual |
196
|
|
|
*/ |
197
|
|
|
public function lte($value) |
198
|
|
|
{ |
199
|
|
|
return new LessThanEqual($this->selector(), $this->createSelector($value)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param SelectorInterface|mixed $value |
204
|
|
|
* |
205
|
|
|
* @return \Cubiche\Core\Specification\Constraint\Equal |
206
|
|
|
*/ |
207
|
|
|
public function eq($value) |
208
|
|
|
{ |
209
|
|
|
return new Equal($this->selector(), $this->createSelector($value)); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param SelectorInterface|mixed $value |
214
|
|
|
* |
215
|
|
|
* @return \Cubiche\Core\Specification\Constraint\NotEqual |
216
|
|
|
*/ |
217
|
|
|
public function neq($value) |
218
|
|
|
{ |
219
|
|
|
return new NotEqual($this->selector(), $this->createSelector($value)); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param SelectorInterface|mixed $value |
224
|
|
|
* |
225
|
|
|
* @return \Cubiche\Core\Specification\Constraint\Same |
226
|
|
|
*/ |
227
|
|
|
public function same($value) |
228
|
|
|
{ |
229
|
|
|
return new Same($this->selector(), $this->createSelector($value)); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param SelectorInterface|mixed $value |
234
|
|
|
* |
235
|
|
|
* @return \Cubiche\Core\Specification\Constraint\NotSame |
236
|
|
|
*/ |
237
|
|
|
public function notSame($value) |
238
|
|
|
{ |
239
|
|
|
return new NotSame($this->selector(), $this->createSelector($value)); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return \Cubiche\Core\Specification\Constraint\Same |
244
|
|
|
*/ |
245
|
|
|
public function isNull() |
246
|
|
|
{ |
247
|
|
|
return new Same($this->selector(), $this->createSelector(null)); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return \Cubiche\Core\Specification\Constraint\NotSame |
252
|
|
|
*/ |
253
|
|
|
public function notNull() |
254
|
|
|
{ |
255
|
|
|
return new NotSame($this->selector(), $this->createSelector(null)); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @return \Cubiche\Core\Specification\Constraint\Same |
260
|
|
|
*/ |
261
|
|
|
public function isTrue() |
262
|
|
|
{ |
263
|
|
|
return new Same($this->selector(), $this->createSelector(true)); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return \Cubiche\Core\Specification\Constraint\Same |
268
|
|
|
*/ |
269
|
|
|
public function isFalse() |
270
|
|
|
{ |
271
|
|
|
return new Same($this->selector(), $this->createSelector(false)); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param SelectorInterface|mixed $value |
276
|
|
|
* |
277
|
|
|
* @return \Cubiche\Core\Specification\SelectorInterface |
278
|
|
|
*/ |
279
|
|
|
protected function createSelector($value) |
280
|
|
|
{ |
281
|
|
|
return $value instanceof SelectorInterface ? $value : new Value($value); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|