1 | <?php |
||
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 = "") |
||
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 = "") |
||
37 | |||
38 | /** |
||
39 | * Perform a true assertion. |
||
40 | * |
||
41 | * @param mixed $actual |
||
42 | * @param string $message |
||
43 | */ |
||
44 | public function isTrue($value, $message = "") |
||
49 | |||
50 | /** |
||
51 | * Perform a false assertion. |
||
52 | * |
||
53 | * @param mixed $value |
||
54 | * @param string $message |
||
55 | */ |
||
56 | public function isFalse($value, $message = "") |
||
61 | |||
62 | /** |
||
63 | * Perform a null assertion. |
||
64 | * |
||
65 | * @param mixed $value |
||
66 | * @param string $message |
||
67 | */ |
||
68 | public function isNull($value, $message = "") |
||
73 | |||
74 | /** |
||
75 | * Perform a negated null assertion. |
||
76 | * |
||
77 | * @param mixed $actual |
||
78 | * @param string $message |
||
79 | */ |
||
80 | public function isNotNull($value, $message = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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 = "") |
||
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: