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