1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dgame\Expectation; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Expectations |
7
|
|
|
* @package Dgame\Expectation |
8
|
|
|
*/ |
9
|
|
|
final class Expectations |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var mixed |
13
|
|
|
*/ |
14
|
|
|
private $value; |
15
|
|
|
/** |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
private $approved = true; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Expectations constructor. |
22
|
|
|
* |
23
|
|
|
* @param $value |
24
|
|
|
*/ |
25
|
|
|
public function __construct($value) |
26
|
|
|
{ |
27
|
|
|
$this->value = $value; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return Expectations |
32
|
|
|
*/ |
33
|
|
|
private function reject(): self |
34
|
|
|
{ |
35
|
|
|
$this->approved = false; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param bool $condition |
42
|
|
|
* |
43
|
|
|
* @return Expectations |
44
|
|
|
*/ |
45
|
|
|
private function approveIf(bool $condition): self |
46
|
|
|
{ |
47
|
|
|
$this->approved = $condition; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function isApproved(): bool |
56
|
|
|
{ |
57
|
|
|
return $this->approved; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param callable $callback |
62
|
|
|
* |
63
|
|
|
* @return Expectations |
64
|
|
|
*/ |
65
|
|
|
public function is(callable $callback): self |
66
|
|
|
{ |
67
|
|
|
return $this->approveIf($callback($this->value)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param callable $callback |
72
|
|
|
* |
73
|
|
|
* @return Expectations |
74
|
|
|
*/ |
75
|
|
|
public function isNot(callable $callback): self |
76
|
|
|
{ |
77
|
|
|
return $this->approveIf(!$callback($this->value)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return Expectations |
82
|
|
|
*/ |
83
|
|
|
public function isInt(): self |
84
|
|
|
{ |
85
|
|
|
return $this->is('is_int'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return Expectations |
90
|
|
|
*/ |
91
|
|
|
public function isFloat(): self |
92
|
|
|
{ |
93
|
|
|
return $this->is('is_float'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return Expectations |
98
|
|
|
*/ |
99
|
|
|
public function isScalar(): self |
100
|
|
|
{ |
101
|
|
|
return $this->is('is_scalar'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return Expectations |
106
|
|
|
*/ |
107
|
|
|
public function isNumeric(): self |
108
|
|
|
{ |
109
|
|
|
return $this->is('is_numeric'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return Expectations |
114
|
|
|
*/ |
115
|
|
|
public function isString(): self |
116
|
|
|
{ |
117
|
|
|
return $this->is('is_string'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return Expectations |
122
|
|
|
*/ |
123
|
|
|
public function isBool(): self |
124
|
|
|
{ |
125
|
|
|
return $this->is('is_bool'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return Expectations |
130
|
|
|
*/ |
131
|
|
|
public function isArray(): self |
132
|
|
|
{ |
133
|
|
|
return $this->is('is_array'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return Expectations |
138
|
|
|
*/ |
139
|
|
|
public function isObject(): self |
140
|
|
|
{ |
141
|
|
|
return $this->is('is_object'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param $object |
146
|
|
|
* |
147
|
|
|
* @return Expectations |
148
|
|
|
*/ |
149
|
|
|
public function isInstanceOf($object): self |
150
|
|
|
{ |
151
|
|
|
if ($this->isObject()->isApproved()) { |
152
|
|
|
return $this->approveIf($this->value instanceof $object); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return Expectations |
160
|
|
|
*/ |
161
|
|
|
public function isCallable(): self |
162
|
|
|
{ |
163
|
|
|
return $this->is('is_callable'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return Expectations |
168
|
|
|
*/ |
169
|
|
|
public function isDir(): self |
170
|
|
|
{ |
171
|
|
|
return $this->is('is_dir'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return Expectations |
176
|
|
|
*/ |
177
|
|
|
public function isFile(): self |
178
|
|
|
{ |
179
|
|
|
return $this->is('is_file'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return Expectations |
184
|
|
|
*/ |
185
|
|
|
public function isEmpty(): self |
186
|
|
|
{ |
187
|
|
|
return $this->approveIf(empty($this->value)); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return Expectations |
192
|
|
|
*/ |
193
|
|
|
public function isNotEmpty(): self |
194
|
|
|
{ |
195
|
|
|
return $this->approveIf(!empty($this->value)); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return Expectations |
200
|
|
|
*/ |
201
|
|
|
public function isNull(): self |
202
|
|
|
{ |
203
|
|
|
return $this->isIdenticalTo(null); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return Expectations |
208
|
|
|
*/ |
209
|
|
|
public function isNotNull(): self |
210
|
|
|
{ |
211
|
|
|
return $this->isNotIdenticalTo(null); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return Expectations |
216
|
|
|
*/ |
217
|
|
|
public function isTrue(): self |
218
|
|
|
{ |
219
|
|
|
return $this->isIdenticalTo(true); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return Expectations |
224
|
|
|
*/ |
225
|
|
|
public function isFalse(): self |
226
|
|
|
{ |
227
|
|
|
return $this->isIdenticalTo(false); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param $value |
232
|
|
|
* |
233
|
|
|
* @return Expectations |
234
|
|
|
*/ |
235
|
|
|
public function isEqual($value): self |
236
|
|
|
{ |
237
|
|
|
return $this->approveIf($this->value == $value); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param $value |
242
|
|
|
* |
243
|
|
|
* @return Expectations |
244
|
|
|
*/ |
245
|
|
|
public function isNotEqual($value): self |
246
|
|
|
{ |
247
|
|
|
return $this->approveIf($this->value != $value); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param $value |
252
|
|
|
* |
253
|
|
|
* @return Expectations |
254
|
|
|
*/ |
255
|
|
|
public function isIdenticalTo($value): self |
256
|
|
|
{ |
257
|
|
|
return $this->approveIf($this->value === $value); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param $value |
262
|
|
|
* |
263
|
|
|
* @return Expectations |
264
|
|
|
*/ |
265
|
|
|
public function isNotIdenticalTo($value): self |
266
|
|
|
{ |
267
|
|
|
return $this->approveIf($this->value !== $value); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param int $lhs |
272
|
|
|
* @param int $rhs |
273
|
|
|
* |
274
|
|
|
* @return Expectations |
275
|
|
|
*/ |
276
|
|
|
public function isBetween(int $lhs, int $rhs): self |
277
|
|
|
{ |
278
|
|
|
if ($this->isNumeric()->isApproved()) { |
279
|
|
|
return $this->approveIf($this->value >= $lhs && $this->value <= $rhs); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $this->reject(); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param int $length |
287
|
|
|
* |
288
|
|
|
* @return Expectations |
289
|
|
|
*/ |
290
|
|
|
public function hasLength(int $length): self |
291
|
|
|
{ |
292
|
|
|
if ($this->isString()->isApproved()) { |
293
|
|
|
return $this->approveIf(strlen($this->value) === $length); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
if ($this->isArray()->isApproved()) { |
297
|
|
|
return $this->approveIf(count($this->value) === $length); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return $this->reject(); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param array $values |
305
|
|
|
* |
306
|
|
|
* @return Expectations |
307
|
|
|
*/ |
308
|
|
|
public function isIn(array $values): self |
309
|
|
|
{ |
310
|
|
|
return $this->approveIf(in_array($this->value, $values)); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @param array $values |
315
|
|
|
* |
316
|
|
|
* @return Expectations |
317
|
|
|
*/ |
318
|
|
|
public function isKeyOf(array $values): self |
319
|
|
|
{ |
320
|
|
|
return $this->approveIf(array_key_exists($this->value, $values)); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @param int $value |
325
|
|
|
* |
326
|
|
|
* @return Expectations |
327
|
|
|
*/ |
328
|
|
|
public function isBelow(int $value): self |
329
|
|
|
{ |
330
|
|
|
if ($this->isNumeric()->isApproved()) { |
331
|
|
|
return $this->approveIf($this->value < $value); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
return $this->reject(); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @param int $value |
339
|
|
|
* |
340
|
|
|
* @return Expectations |
341
|
|
|
*/ |
342
|
|
|
public function isAbove(int $value): self |
343
|
|
|
{ |
344
|
|
|
if ($this->isNumeric()->isApproved()) { |
345
|
|
|
return $this->approveIf($this->value > $value); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
return $this->reject(); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param int $value |
353
|
|
|
* |
354
|
|
|
* @return Expectations |
355
|
|
|
*/ |
356
|
|
|
public function isBelowOrEqual(int $value): self |
357
|
|
|
{ |
358
|
|
|
if ($this->isNumeric()->isApproved()) { |
359
|
|
|
return $this->approveIf($this->value <= $value); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
return $this->reject(); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @param int $value |
367
|
|
|
* |
368
|
|
|
* @return Expectations |
369
|
|
|
*/ |
370
|
|
|
public function isAboveOrEqual(int $value): self |
371
|
|
|
{ |
372
|
|
|
if ($this->isNumeric()->isApproved()) { |
373
|
|
|
return $this->approveIf($this->value >= $value); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
return $this->reject(); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @return Expectations |
381
|
|
|
*/ |
382
|
|
|
public function isPositive(): self |
383
|
|
|
{ |
384
|
|
|
return $this->isAboveOrEqual(0); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @return Expectations |
389
|
|
|
*/ |
390
|
|
|
public function isNegative(): self |
391
|
|
|
{ |
392
|
|
|
return $this->isBelow(0); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* @return Expectations |
397
|
|
|
*/ |
398
|
|
View Code Duplication |
public function isEven(): self |
|
|
|
|
399
|
|
|
{ |
400
|
|
|
if ($this->isNumeric()->isApproved()) { |
401
|
|
|
return $this->approveIf(($this->value & 1) === 0); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
return $this->reject(); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @return Expectations |
409
|
|
|
*/ |
410
|
|
View Code Duplication |
public function isOdd(): self |
|
|
|
|
411
|
|
|
{ |
412
|
|
|
if ($this->isNumeric()->isApproved()) { |
413
|
|
|
return $this->approveIf(($this->value & 1) === 1); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
return $this->reject(); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* @param string $pattern |
421
|
|
|
* |
422
|
|
|
* @return Expectations |
423
|
|
|
*/ |
424
|
|
|
public function match(string $pattern): self |
425
|
|
|
{ |
426
|
|
|
if ($this->isString()->isApproved()) { |
427
|
|
|
return $this->approveIf(preg_match($pattern, $this->value) === 1); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
return $this->reject(); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* @param $value |
435
|
|
|
* |
436
|
|
|
* @return mixed |
437
|
|
|
*/ |
438
|
|
|
public function then($value) |
439
|
|
|
{ |
440
|
|
|
return $this->isApproved() ? $value : $this->value; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* @param $default |
445
|
|
|
* |
446
|
|
|
* @return mixed |
447
|
|
|
*/ |
448
|
|
|
public function else($default) |
|
|
|
|
449
|
|
|
{ |
450
|
|
|
return $this->isApproved() ? $this->value : $default; |
451
|
|
|
} |
452
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.