1
|
|
|
<?php |
2
|
|
|
namespace Peridot\Leo\Interfaces\Assert; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* TypeAssertTrait contains assertions that primarily deal |
6
|
|
|
* with making assertions about a value's type. |
7
|
|
|
* |
8
|
|
|
* @package Peridot\Leo\Interfaces\Assert |
9
|
|
|
*/ |
10
|
|
|
trait TypeAssertTrait |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Performs a type assertion. |
14
|
|
|
* |
15
|
|
|
* @param mixed $actual |
16
|
|
|
* @param string $expected |
17
|
|
|
* @param string $message |
18
|
|
|
*/ |
19
|
|
|
public function typeOf($actual, $expected, $message = "") |
20
|
|
|
{ |
21
|
|
|
$this->assertion->setActual($actual); |
|
|
|
|
22
|
|
|
return $this->assertion->to->be->a($expected, $message); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Performs a negated type assertion. |
27
|
|
|
* |
28
|
|
|
* @param mixed $actual |
29
|
|
|
* @param string $expected |
30
|
|
|
* @param string $message |
31
|
|
|
*/ |
32
|
|
|
public function notTypeOf($actual, $expected, $message = "") |
33
|
|
|
{ |
34
|
|
|
$this->assertion->setActual($actual); |
35
|
|
|
return $this->assertion->to->not->be->a($expected, $message); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Perform a true assertion. |
40
|
|
|
* |
41
|
|
|
* @param mixed $actual |
|
|
|
|
42
|
|
|
* @param string $message |
43
|
|
|
*/ |
44
|
|
|
public function isTrue($value, $message = "") |
45
|
|
|
{ |
46
|
|
|
$this->assertion->setActual($value); |
47
|
|
|
return $this->assertion->to->be->true($message); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Perform a false assertion. |
52
|
|
|
* |
53
|
|
|
* @param mixed $value |
54
|
|
|
* @param string $message |
55
|
|
|
*/ |
56
|
|
|
public function isFalse($value, $message = "") |
57
|
|
|
{ |
58
|
|
|
$this->assertion->setActual($value); |
59
|
|
|
return $this->assertion->to->be->false($message); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Perform a null assertion. |
64
|
|
|
* |
65
|
|
|
* @param mixed $value |
66
|
|
|
* @param string $message |
67
|
|
|
*/ |
68
|
|
|
public function isNull($value, $message = "") |
69
|
|
|
{ |
70
|
|
|
$this->assertion->setActual($value); |
71
|
|
|
return $this->assertion->to->be->null($message); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Perform a negated null assertion. |
76
|
|
|
* |
77
|
|
|
* @param mixed $actual |
|
|
|
|
78
|
|
|
* @param string $message |
79
|
|
|
*/ |
80
|
|
|
public function isNotNull($value, $message = "") |
81
|
|
|
{ |
82
|
|
|
$this->assertion->setActual($value); |
83
|
|
|
return $this->assertion->to->not->be->null($message); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Performs a predicate assertion to check if actual |
88
|
|
|
* value is callable. |
89
|
|
|
* |
90
|
|
|
* @param mixed $value |
91
|
|
|
* @param string $message |
92
|
|
|
*/ |
93
|
|
|
public function isCallable($value, $message = "") |
94
|
|
|
{ |
95
|
|
|
$this->assertion->setActual($value); |
96
|
|
|
return $this->assertion->to->satisfy('is_callable', $message); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Performs a negated predicate assertion to check if actual |
101
|
|
|
* value is not a callable. |
102
|
|
|
* |
103
|
|
|
* @param mixed $value |
104
|
|
|
* @param string $message |
105
|
|
|
*/ |
106
|
|
|
public function isNotCallable($value, $message = "") |
107
|
|
|
{ |
108
|
|
|
$this->assertion->setActual($value); |
109
|
|
|
return $this->assertion->to->not->satisfy('is_callable', $message); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Perform a type assertion for type "object." |
114
|
|
|
* |
115
|
|
|
* @param mixed $value |
116
|
|
|
* @param string $message |
117
|
|
|
*/ |
118
|
|
|
public function isObject($value, $message = "") |
119
|
|
|
{ |
120
|
|
|
return $this->typeOf($value, 'object', $message); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Perform a negative type assertion for type "object." |
125
|
|
|
* |
126
|
|
|
* @param mixed $value |
127
|
|
|
* @param string $message |
128
|
|
|
*/ |
129
|
|
|
public function isNotObject($value, $message = "") |
130
|
|
|
{ |
131
|
|
|
return $this->notTypeOf($value, 'object', $message); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Perform a type assertion for type "array." |
136
|
|
|
* |
137
|
|
|
* @param mixed $value |
138
|
|
|
* @param string $message |
139
|
|
|
*/ |
140
|
|
|
public function isArray($value, $message = "") |
141
|
|
|
{ |
142
|
|
|
return $this->typeOf($value, 'array', $message); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Performs a negative type assertion for type "array." |
147
|
|
|
* |
148
|
|
|
* @param mixed $value |
149
|
|
|
* @param string $message |
150
|
|
|
*/ |
151
|
|
|
public function isNotArray($value, $message = "") |
152
|
|
|
{ |
153
|
|
|
return $this->notTypeOf($value, 'array', $message); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Perform a type assertion for type "string." |
158
|
|
|
* |
159
|
|
|
* @param mixed $value |
160
|
|
|
* @param string $message |
161
|
|
|
*/ |
162
|
|
|
public function isString($value, $message = "") |
163
|
|
|
{ |
164
|
|
|
return $this->typeOf($value, 'string', $message); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Perform a negated type assertion for type "string." |
169
|
|
|
* |
170
|
|
|
* @param mixed $value |
171
|
|
|
* @param string $message |
172
|
|
|
*/ |
173
|
|
|
public function isNotString($value, $message = "") |
174
|
|
|
{ |
175
|
|
|
return $this->notTypeOf($value, 'string', $message); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Performs a predicate assertion to check if actual |
180
|
|
|
* value is numeric. |
181
|
|
|
* |
182
|
|
|
* @param mixed $value |
183
|
|
|
* @param string $message |
184
|
|
|
*/ |
185
|
|
|
public function isNumeric($value, $message = "") |
186
|
|
|
{ |
187
|
|
|
$this->assertion->setActual($value); |
188
|
|
|
return $this->assertion->to->satisfy('is_numeric', $message); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Performs a negated predicate assertion to check if actual |
193
|
|
|
* value is numeric. |
194
|
|
|
* |
195
|
|
|
* @param mixed $value |
196
|
|
|
* @param string $message |
197
|
|
|
*/ |
198
|
|
|
public function isNotNumeric($value, $message = "") |
199
|
|
|
{ |
200
|
|
|
$this->assertion->setActual($value); |
201
|
|
|
return $this->assertion->not->to->satisfy('is_numeric', $message); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Perform a type assertion for type "integer." |
206
|
|
|
* |
207
|
|
|
* @param $value |
208
|
|
|
* @param string $message |
209
|
|
|
*/ |
210
|
|
|
public function isInteger($value, $message = "") |
211
|
|
|
{ |
212
|
|
|
return $this->typeOf($value, 'integer', $message); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Perform a negated type assertion for type "integer." |
217
|
|
|
* |
218
|
|
|
* @param mixed $value |
219
|
|
|
* @param string $message |
220
|
|
|
*/ |
221
|
|
|
public function isNotInteger($value, $message = "") |
222
|
|
|
{ |
223
|
|
|
return $this->notTypeOf($value, 'integer', $message); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Perform a type assertion for type "double." |
228
|
|
|
* |
229
|
|
|
* @param mixed $value |
230
|
|
|
* @param string $message |
231
|
|
|
*/ |
232
|
|
|
public function isDouble($value, $message = "") |
233
|
|
|
{ |
234
|
|
|
return $this->typeOf($value, 'double', $message); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Perform a negated type assertion for type "double." |
239
|
|
|
* |
240
|
|
|
* @param mixed $value |
241
|
|
|
* @param string $message |
242
|
|
|
*/ |
243
|
|
|
public function isNotDouble($value, $message = "") |
244
|
|
|
{ |
245
|
|
|
return $this->notTypeOf($value, 'double', $message); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Perform a type assertion for type "resource." |
250
|
|
|
* |
251
|
|
|
* @param mixed $value |
252
|
|
|
* @param string $message |
253
|
|
|
*/ |
254
|
|
|
public function isResource($value, $message = "") |
255
|
|
|
{ |
256
|
|
|
return $this->typeOf($value, 'resource', $message); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Perform a negated type assertion for type "resource." |
261
|
|
|
* |
262
|
|
|
* @param mixed $value |
263
|
|
|
* @param string $message |
264
|
|
|
*/ |
265
|
|
|
public function isNotResource($value, $message = "") |
266
|
|
|
{ |
267
|
|
|
return $this->notTypeOf($value, 'resource', $message); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Perform a type assertion for type "boolean." |
272
|
|
|
* |
273
|
|
|
* @param mixed $value |
274
|
|
|
* @param string $message |
275
|
|
|
*/ |
276
|
|
|
public function isBoolean($value, $message = "") |
277
|
|
|
{ |
278
|
|
|
return $this->typeOf($value, 'boolean', $message); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Perform a negated type assertion for type "boolean." |
283
|
|
|
* |
284
|
|
|
* @param mixed $value |
285
|
|
|
* @param string $message |
286
|
|
|
*/ |
287
|
|
|
public function isNotBoolean($value, $message = "") |
288
|
|
|
{ |
289
|
|
|
return $this->notTypeOf($value, 'boolean', $message); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: