1
+ <?php
2
+ /***************************************************************************
3
+ * Copyright (C) 2005-2008 by Konstantin V. Arkhipov *
4
+ * *
5
+ * This program is free software; you can redistribute it and/or modify *
6
+ * it under the terms of the GNU Lesser General Public License as *
7
+ * published by the Free Software Foundation; either version 3 of the *
8
+ * License, or (at your option) any later version. *
9
+ * *
10
+ ***************************************************************************/
11
+ /* $Id$ */
12
+
13
+ /**
14
+ * Widely used assertions.
15
+ *
16
+ * @ingroup Base
17
+ **/
18
+ final class Assert extends StaticFactory
19
+ {
20
+ public static function isTrue ($ boolean , $ message = null )
21
+ {
22
+ if ($ boolean !== true )
23
+ throw new WrongArgumentException (
24
+ $ message .', ' .self ::dumpArgument ($ boolean )
25
+ );
26
+ }
27
+
28
+ public static function isFalse ($ boolean , $ message = null )
29
+ {
30
+ if ($ boolean !== false )
31
+ throw new WrongArgumentException (
32
+ $ message .', ' .self ::dumpArgument ($ boolean )
33
+ );
34
+ }
35
+
36
+ public static function isNotFalse ($ boolean , $ message = null )
37
+ {
38
+ if ($ boolean === false )
39
+ throw new WrongArgumentException (
40
+ $ message .', ' .self ::dumpArgument ($ boolean )
41
+ );
42
+ }
43
+
44
+ public static function isNull ($ variable , $ message = null )
45
+ {
46
+ if ($ variable !== null )
47
+ throw new WrongArgumentException (
48
+ $ message .', ' .self ::dumpArgument ($ variable )
49
+ );
50
+ }
51
+
52
+ public static function isEmpty ($ variable , $ message = null )
53
+ {
54
+ if (!empty ($ variable ))
55
+ throw new WrongArgumentException (
56
+ $ message .', ' .self ::dumpArgument ($ variable )
57
+ );
58
+ }
59
+
60
+ public static function isNotEmpty ($ variable , $ message = null )
61
+ {
62
+ if (empty ($ variable ))
63
+ throw new WrongArgumentException (
64
+ $ message .', ' .self ::dumpArgument ($ variable )
65
+ );
66
+ }
67
+
68
+ public static function isIndexExists ($ array , $ key , $ message = null )
69
+ {
70
+ Assert::isArray ($ array );
71
+
72
+ if (!array_key_exists ($ key , $ array ))
73
+ throw new WrongArgumentException (
74
+ $ message .', ' .self ::dumpArgument ($ key )
75
+ );
76
+ }
77
+
78
+ public static function isNotNull ($ variable , $ message = null )
79
+ {
80
+ if ($ variable === null )
81
+ throw new WrongArgumentException ($ message );
82
+ }
83
+
84
+ public static function isScalar ($ variable , $ message = null )
85
+ {
86
+ if (!is_scalar ($ variable ))
87
+ throw new WrongArgumentException (
88
+ $ message .', ' .self ::dumpArgument ($ variable )
89
+ );
90
+ }
91
+
92
+ public static function isArray ($ variable , $ message = null )
93
+ {
94
+ if (!is_array ($ variable ))
95
+ throw new WrongArgumentException (
96
+ $ message .', ' .self ::dumpArgument ($ variable )
97
+ );
98
+ }
99
+
100
+ public static function isNotEmptyArray (&$ variable , $ message = null )
101
+ {
102
+ self ::isArray ($ variable , $ message );
103
+
104
+ if (!$ variable )
105
+ throw new WrongArgumentException (
106
+ $ message .', ' .self ::dumpArgument ($ variable )
107
+ );
108
+ }
109
+
110
+ public static function isInteger ($ variable , $ message = null )
111
+ {
112
+ if (
113
+ !(
114
+ is_numeric ($ variable )
115
+ && $ variable == (int ) $ variable
116
+ )
117
+ )
118
+ throw new WrongArgumentException (
119
+ $ message .', ' .self ::dumpArgument ($ variable )
120
+ );
121
+ }
122
+
123
+ public static function isPositiveInteger ($ variable , $ message = null )
124
+ {
125
+ if (
126
+ !self ::checkInteger ($ variable )
127
+ || $ variable < 0
128
+ )
129
+ throw new WrongArgumentException (
130
+ $ message .', ' .self ::dumpArgument ($ variable )
131
+ );
132
+ }
133
+
134
+ public static function isFloat ($ variable , $ message = null )
135
+ {
136
+ if (!self ::checkFloat ($ variable ))
137
+ throw new WrongArgumentException (
138
+ $ message .', ' .self ::dumpArgument ($ variable )
139
+ );
140
+ }
141
+
142
+ public static function isString ($ variable , $ message = null )
143
+ {
144
+ if (!is_string ($ variable ))
145
+ throw new WrongArgumentException (
146
+ $ message .', ' .self ::dumpArgument ($ variable )
147
+ );
148
+ }
149
+
150
+ public static function isBoolean ($ variable , $ message = null )
151
+ {
152
+ if (!($ variable === true || $ variable === false ))
153
+ throw new WrongArgumentException (
154
+ $ message .', ' .self ::dumpArgument ($ variable )
155
+ );
156
+ }
157
+
158
+ public static function isTernaryBase ($ variable , $ message = null )
159
+ {
160
+ if (
161
+ !(
162
+ ($ variable === true )
163
+ || ($ variable === false )
164
+ || ($ variable === null )
165
+ )
166
+ )
167
+ throw new WrongArgumentException (
168
+ $ message .', ' .self ::dumpArgument ($ variable )
169
+ );
170
+ }
171
+
172
+ public static function brothers ($ first , $ second , $ message = null )
173
+ {
174
+ if (get_class ($ first ) !== get_class ($ second ))
175
+ throw new WrongArgumentException (
176
+ $ message .', ' .self ::dumpOppositeArguments ($ first , $ second )
177
+ );
178
+ }
179
+
180
+ public static function isEqual ($ first , $ second , $ message = null )
181
+ {
182
+ if ($ first != $ second )
183
+ throw new WrongArgumentException (
184
+ $ message .', ' .self ::dumpOppositeArguments ($ first , $ second )
185
+ );
186
+ }
187
+
188
+ public static function isNotEqual ($ first , $ second , $ message = null )
189
+ {
190
+ if ($ first == $ second )
191
+ throw new WrongArgumentException (
192
+ $ message .', ' .self ::dumpOppositeArguments ($ first , $ second )
193
+ );
194
+ }
195
+
196
+ public static function isSame ($ first , $ second , $ message = null )
197
+ {
198
+ if ($ first !== $ second )
199
+ throw new WrongArgumentException (
200
+ $ message .', ' .self ::dumpOppositeArguments ($ first , $ second )
201
+ );
202
+ }
203
+
204
+ public static function isNotSame ($ first , $ second , $ message = null )
205
+ {
206
+ if ($ first === $ second )
207
+ throw new WrongArgumentException (
208
+ $ message .', ' .self ::dumpOppositeArguments ($ first , $ second )
209
+ );
210
+ }
211
+
212
+ public static function isTypelessEqual ($ first , $ second , $ message = null )
213
+ {
214
+ if ($ first != $ second )
215
+ throw new WrongArgumentException (
216
+ $ message .', ' .self ::dumpOppositeArguments ($ first , $ second )
217
+ );
218
+ }
219
+
220
+ public static function isLesser ($ first , $ second , $ message = null )
221
+ {
222
+ if (!($ first < $ second ))
223
+ throw new WrongArgumentException (
224
+ $ message .', ' .self ::dumpOppositeArguments ($ first , $ second )
225
+ );
226
+ }
227
+
228
+ public static function isGreater ($ first , $ second , $ message = null )
229
+ {
230
+ if (!($ first > $ second ))
231
+ throw new WrongArgumentException (
232
+ $ message .', ' .self ::dumpOppositeArguments ($ first , $ second )
233
+ );
234
+ }
235
+
236
+ public static function isLesserOrEqual ($ first , $ second , $ message = null )
237
+ {
238
+ if (!($ first <= $ second ))
239
+ throw new WrongArgumentException (
240
+ $ message .', ' .self ::dumpOppositeArguments ($ first , $ second )
241
+ );
242
+ }
243
+
244
+ public static function isGreaterOrEqual ($ first , $ second , $ message = null )
245
+ {
246
+ if (!($ first >= $ second ))
247
+ throw new WrongArgumentException (
248
+ $ message .', ' .self ::dumpOppositeArguments ($ first , $ second )
249
+ );
250
+ }
251
+
252
+ public static function isInstance ($ first , $ second , $ message = null )
253
+ {
254
+ if (!ClassUtils::isInstanceOf ($ first , $ second ))
255
+ throw new WrongArgumentException (
256
+ $ message .', ' .self ::dumpOppositeArguments ($ first , $ second )
257
+ );
258
+ }
259
+
260
+ public static function classExists ($ className , $ message = null )
261
+ {
262
+ if (!class_exists ($ className , true ))
263
+ throw new WrongArgumentException (
264
+ $ message .', class " ' .$ className .'" does not exists '
265
+ );
266
+ }
267
+
268
+ public static function isUnreachable ($ message = 'unreachable code reached ' )
269
+ {
270
+ throw new WrongArgumentException ($ message );
271
+ }
272
+
273
+ /// exceptionless methods
274
+ //@{
275
+ public static function checkInteger ($ value )
276
+ {
277
+ return (
278
+ is_numeric ($ value )
279
+ && ($ value == (int ) $ value )
280
+ && (strlen ($ value ) == strlen ((int ) $ value ))
281
+ );
282
+ }
283
+
284
+ public static function checkFloat ($ value )
285
+ {
286
+ return (
287
+ is_numeric ($ value )
288
+ && ($ value == (float ) $ value )
289
+ );
290
+ }
291
+
292
+ public static function dumpArgument ($ argument )
293
+ {
294
+ return 'argument: [ ' .print_r ($ argument , true ).'] ' ;
295
+ }
296
+
297
+ public static function dumpOppositeArguments ($ first , $ second )
298
+ {
299
+ return
300
+ 'arguments: [ ' .print_r ($ first , true ).'] '
301
+ .'vs. [ ' .print_r ($ second , true ).'] ' ;
302
+ }
303
+ //@}
304
+ }
305
+ ?>
0 commit comments