1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Dgame\Ensurance; |
4
|
|
|
|
5
|
|
|
/**
|
6
|
|
|
* Class Ensurance
|
7
|
|
|
* @package Dgame\Ensurance
|
8
|
|
|
*/
|
9
|
|
|
final class Ensurance implements EnsuranceInterface |
10
|
|
|
{ |
11
|
|
|
use EnsuranceTrait;
|
12
|
|
|
|
13
|
|
|
/**
|
14
|
|
|
* Ensurance constructor.
|
15
|
|
|
*
|
16
|
|
|
* @param mixed $value
|
17
|
|
|
*/
|
18
|
|
|
public function __construct($value) |
19
|
|
|
{ |
20
|
|
|
$this->value = $value;
|
21
|
|
|
$this->ensure($value !== null && $value !== false); |
22
|
|
|
}
|
23
|
57 |
|
|
24
|
|
|
/**
|
25
|
57 |
|
* @return ArrayEnsurance
|
26
|
57 |
|
*/
|
27
|
|
|
public function isArray(): ArrayEnsurance |
28
|
|
|
{ |
29
|
|
|
$this->ensure(is_array($this->value))->orThrow('Value "%a" is not an array', $this->value);
|
30
|
|
|
|
31
|
|
|
return new ArrayEnsurance($this); |
32
|
|
|
}
|
33
|
|
|
|
34
|
|
|
/**
|
35
|
|
|
* @return ReflectionEnsurance
|
36
|
|
|
* @throws \ReflectionException
|
37
|
|
|
*/
|
38
|
|
|
public function isObject(): ReflectionEnsurance |
39
|
9 |
|
{ |
40
|
|
|
$this->ensure(is_object($this->value))->orThrow('Value "%s" is not an object', $this->value);
|
41
|
9 |
|
|
42
|
|
|
return new ReflectionEnsurance($this); |
43
|
9 |
|
}
|
44
|
|
|
|
45
|
|
|
/**
|
46
|
|
|
* @return Ensurance
|
47
|
|
|
*/
|
48
|
|
|
public function isResource(): self |
49
|
9 |
|
{ |
50
|
|
|
$this->ensure(is_resource($this->value))->orThrow('Value "%s" is not a resource', $this->value);
|
51
|
9 |
|
|
52
|
|
|
return $this; |
53
|
9 |
|
}
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* @return ScalarEnsurance
|
57
|
|
|
*/
|
58
|
|
|
public function isScalar(): ScalarEnsurance |
59
|
1 |
|
{ |
60
|
|
|
$this->ensure(is_scalar($this->value))->orThrow('Value "%s" is not a scalar', $this->value);
|
61
|
1 |
|
|
62
|
1 |
|
return new ScalarEnsurance($this); |
63
|
|
|
}
|
64
|
|
|
|
65
|
|
|
/**
|
66
|
|
|
* @return StringEnsurance
|
67
|
26 |
|
*/
|
68
|
|
|
public function isString(): StringEnsurance |
69
|
26 |
|
{ |
70
|
|
|
return $this->isScalar()->isString(); |
71
|
26 |
|
}
|
72
|
|
|
|
73
|
|
|
/**
|
74
|
|
|
* @return NumericEnsurance
|
75
|
|
|
*/
|
76
|
|
|
public function isNumeric(): NumericEnsurance |
77
|
12 |
|
{ |
78
|
|
|
return $this->isScalar()->isNumeric(); |
79
|
12 |
|
}
|
80
|
|
|
|
81
|
|
|
/**
|
82
|
|
|
* @return BooleanEnsurance
|
83
|
|
|
*/
|
84
|
|
|
public function isBool(): BooleanEnsurance |
85
|
12 |
|
{ |
86
|
|
|
return $this->isScalar()->isBool(); |
87
|
12 |
|
}
|
88
|
|
|
|
89
|
|
|
/**
|
90
|
|
|
* @return NumericEnsurance
|
91
|
|
|
*/
|
92
|
|
|
public function isInt(): NumericEnsurance |
93
|
2 |
|
{ |
94
|
|
|
return $this->isNumeric()->isInt(); |
95
|
2 |
|
}
|
96
|
|
|
|
97
|
|
|
/**
|
98
|
|
|
* @return NumericEnsurance
|
99
|
|
|
*/
|
100
|
|
|
public function isFloat(): NumericEnsurance |
101
|
1 |
|
{ |
102
|
|
|
return $this->isNumeric()->isFloat(); |
103
|
1 |
|
}
|
104
|
|
|
|
105
|
|
|
/**
|
106
|
|
|
* @return BooleanEnsurance
|
107
|
|
|
*/
|
108
|
|
|
public function isTrue(): BooleanEnsurance |
109
|
1 |
|
{ |
110
|
|
|
return $this->isBool()->isTrue(); |
111
|
1 |
|
}
|
112
|
|
|
|
113
|
|
|
/**
|
114
|
|
|
* @return BooleanEnsurance
|
115
|
|
|
*/
|
116
|
|
|
public function isFalse(): BooleanEnsurance |
117
|
1 |
|
{ |
118
|
|
|
return $this->isBool()->isFalse(); |
119
|
1 |
|
}
|
120
|
|
|
|
121
|
|
|
/**
|
122
|
|
|
* @return NumericEnsurance
|
123
|
|
|
*/
|
124
|
|
|
public function isPositive(): NumericEnsurance |
125
|
1 |
|
{ |
126
|
|
|
return $this->isNumeric()->isPositive(); |
127
|
1 |
|
}
|
128
|
|
|
|
129
|
|
|
/**
|
130
|
|
|
* @return NumericEnsurance
|
131
|
|
|
*/
|
132
|
|
|
public function isNegative(): NumericEnsurance |
133
|
1 |
|
{ |
134
|
|
|
return $this->isNumeric()->isNegative(); |
135
|
1 |
|
}
|
136
|
|
|
|
137
|
|
|
/**
|
138
|
|
|
* @return NumericEnsurance
|
139
|
|
|
*/
|
140
|
|
|
public function isEven(): NumericEnsurance |
141
|
1 |
|
{ |
142
|
|
|
return $this->isNumeric()->isEven(); |
143
|
1 |
|
}
|
144
|
|
|
|
145
|
|
|
/**
|
146
|
|
|
* @return NumericEnsurance
|
147
|
|
|
*/
|
148
|
|
|
public function isOdd(): NumericEnsurance |
149
|
1 |
|
{ |
150
|
|
|
return $this->isNumeric()->isOdd(); |
151
|
1 |
|
}
|
152
|
|
|
|
153
|
|
|
/**
|
154
|
|
|
* @param string $pattern
|
155
|
|
|
*
|
156
|
|
|
* @return StringEnsurance
|
157
|
1 |
|
*/
|
158
|
|
|
public function matches(string $pattern): StringEnsurance |
159
|
1 |
|
{ |
160
|
|
|
return $this->isString()->matches($pattern); |
161
|
|
|
}
|
162
|
|
|
|
163
|
|
|
/**
|
164
|
|
|
* @return Ensurance
|
165
|
|
|
*/
|
166
|
|
|
public function isNull(): self |
167
|
|
|
{ |
168
|
|
|
$this->ensure($this->value === null)->orThrow('Value "%s" is not null', $this->value);
|
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
}
|
172
|
|
|
|
173
|
|
|
/**
|
174
|
|
|
* @return Ensurance
|
175
|
1 |
|
*/
|
176
|
|
|
public function isNotNull(): self |
177
|
1 |
|
{ |
178
|
|
|
$this->ensure($this->value !== null)->orThrow('Value is null', $this->value);
|
179
|
1 |
|
|
180
|
|
|
return $this; |
181
|
|
|
}
|
182
|
|
|
|
183
|
|
|
/**
|
184
|
|
|
* @return Ensurance
|
185
|
1 |
|
*/
|
186
|
|
|
public function isEmpty(): self |
187
|
1 |
|
{ |
188
|
|
|
$this->ensure(empty($this->value))->orThrow('Value "%s" is not empty', $this->value);
|
189
|
1 |
|
|
190
|
|
|
return $this; |
191
|
|
|
}
|
192
|
|
|
|
193
|
|
|
/**
|
194
|
|
|
* @return Ensurance
|
195
|
1 |
|
*/
|
196
|
|
|
public function isNotEmpty(): self |
197
|
1 |
|
{ |
198
|
|
|
$this->ensure(!empty($this->value))->orThrow('Value "%s" is empty', $this->value);
|
199
|
1 |
|
|
200
|
|
|
return $this; |
201
|
|
|
}
|
202
|
|
|
|
203
|
|
|
/**
|
204
|
|
|
* @param mixed $value
|
205
|
1 |
|
*
|
206
|
|
|
* @return Ensurance
|
207
|
1 |
|
*/
|
208
|
|
|
public function isEqualTo($value): self |
209
|
1 |
|
{ |
210
|
|
|
$this->ensure($this->value == $value)->orThrow('"%s" is not equal to "%s"', $this->value, $value);
|
211
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
}
|
214
|
|
|
|
215
|
|
|
/**
|
216
|
|
|
* @param mixed $value
|
217
|
2 |
|
*
|
218
|
|
|
* @return Ensurance
|
219
|
2 |
|
*/
|
220
|
|
|
public function isNotEqualTo($value): self |
221
|
2 |
|
{ |
222
|
|
|
$this->ensure($this->value != $value)->orThrow('"%s" is equal to "%s"', $this->value, $value);
|
223
|
|
|
|
224
|
|
|
return $this; |
225
|
|
|
}
|
226
|
|
|
|
227
|
|
|
/**
|
228
|
|
|
* @param mixed $value
|
229
|
2 |
|
*
|
230
|
|
|
* @return Ensurance
|
231
|
2 |
|
*/
|
232
|
|
|
public function isIdenticalTo($value): self |
233
|
2 |
|
{ |
234
|
|
|
$this->ensure($this->value === $value)->orThrow('"%s" is not the same as "%s"', $this->value, $value);
|
235
|
|
|
|
236
|
|
|
return $this; |
237
|
|
|
}
|
238
|
|
|
|
239
|
|
|
/**
|
240
|
|
|
* @param mixed $value
|
241
|
1 |
|
*
|
242
|
|
|
* @return Ensurance
|
243
|
1 |
|
*/
|
244
|
|
|
public function isNotIdenticalTo($value): self |
245
|
1 |
|
{ |
246
|
|
|
$this->ensure($this->value !== $value)->orThrow('"%s" is the same as "%s"', $this->value, $value);
|
247
|
|
|
|
248
|
|
|
return $this; |
249
|
|
|
}
|
250
|
|
|
|
251
|
|
|
/**
|
252
|
|
|
* @param array $data
|
253
|
1 |
|
*
|
254
|
|
|
* @return Ensurance
|
255
|
1 |
|
*/
|
256
|
|
|
public function isIn(array $data): self |
257
|
1 |
|
{ |
258
|
|
|
$this->ensure(in_array($this->value, $data, true))->orThrow('"%s" is not a value of %s', $this->value, $data);
|
259
|
|
|
|
260
|
|
|
return $this; |
261
|
|
|
}
|
262
|
|
|
|
263
|
|
|
/**
|
264
|
|
|
* @param array $data
|
265
|
2 |
|
*
|
266
|
|
|
* @return Ensurance
|
267
|
2 |
|
*/
|
268
|
|
|
public function isKeyOf(array $data): self |
269
|
2 |
|
{ |
270
|
|
|
$this->ensure(array_key_exists($this->value, $data))->orThrow('"%s" is not a key of %s', $this->value, $data);
|
271
|
|
|
|
272
|
|
|
return $this; |
273
|
|
|
}
|
274
|
|
|
|
275
|
|
|
/**
|
276
|
|
|
* @param float $value
|
277
|
2 |
|
*
|
278
|
|
|
* @return NumericEnsurance
|
279
|
2 |
|
*/
|
280
|
|
|
public function isGreaterThan(float $value): NumericEnsurance |
281
|
2 |
|
{ |
282
|
|
|
return $this->isNumeric()->isGreaterThan($value); |
283
|
|
|
}
|
284
|
|
|
|
285
|
|
|
/**
|
286
|
|
|
* @param float $value
|
287
|
|
|
*
|
288
|
|
|
* @return NumericEnsurance
|
289
|
|
|
*/
|
290
|
|
|
public function isLessThan(float $value): NumericEnsurance |
291
|
|
|
{ |
292
|
|
|
return $this->isNumeric()->isLessThan($value); |
293
|
|
|
}
|
294
|
|
|
|
295
|
|
|
/**
|
296
|
|
|
* @param float $value
|
297
|
|
|
*
|
298
|
|
|
* @return NumericEnsurance
|
299
|
|
|
*/
|
300
|
|
|
public function isGreaterThanOrEqualTo(float $value): NumericEnsurance |
301
|
|
|
{ |
302
|
|
|
return $this->isNumeric()->isGreaterThanOrEqualTo($value); |
303
|
|
|
}
|
304
|
|
|
|
305
|
|
|
/**
|
306
|
|
|
* @param float $value
|
307
|
|
|
*
|
308
|
|
|
* @return NumericEnsurance
|
309
|
|
|
*/
|
310
|
|
|
public function isLessThanOrEqualTo(float $value): NumericEnsurance |
311
|
|
|
{ |
312
|
|
|
return $this->isNumeric()->isLessThanOrEqualTo($value); |
313
|
|
|
}
|
314
|
|
|
|
315
|
|
|
/**
|
316
|
|
|
* @param float $lhs
|
317
|
|
|
* @param float $rhs
|
318
|
|
|
*
|
319
|
|
|
* @return NumericEnsurance
|
320
|
|
|
*/
|
321
|
|
|
public function isBetween(float $lhs, float $rhs): NumericEnsurance |
322
|
|
|
{ |
323
|
|
|
return $this->isNumeric()->isBetween($lhs, $rhs); |
324
|
|
|
}
|
325
|
|
|
|
326
|
|
|
/**
|
327
|
|
|
* @param float $lhs
|
328
|
|
|
* @param float $rhs
|
329
|
|
|
*
|
330
|
|
|
* @return NumericEnsurance
|
331
|
|
|
*/
|
332
|
|
|
public function isNotBetween(float $lhs, float $rhs): NumericEnsurance |
333
|
|
|
{ |
334
|
|
|
return $this->isNumeric()->isNotBetween($lhs, $rhs); |
335
|
|
|
} |
336
|
|
|
}
|
337
|
|
|
|