-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnsure.php
More file actions
249 lines (222 loc) · 7.69 KB
/
Ensure.php
File metadata and controls
249 lines (222 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* EnsureBundle
* Copyright (c) 2014, 20steps Digital Full Service Boutique, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
namespace twentysteps\Commons\EnsureBundle;
/**
* Static helper for checking code preconditions and throwing an LogicException in the case of
* errors early with a meaningful message. The message may be generated in sprintf fashion like
* shown in the following example:
*
* Ensure::isTrue($user->isValid(), 'user [%s] is not valid', $user->getId());
*
* All functions do return the checked value, so the check and assignment may occur in one line:
*
* $this->name = Ensure::isNotNull($name, 'name must not be null');
*/
final class Ensure {
// TODO refactor varargs when switching to newer PHP version (>= 5.6)
private function __construct() {
// no instantiation possible
}
/**
* Fails with the given message if $value is null: $value === null.
* @return mixed
*/
public static function isNotNull($value, $format, $args = null, $_ = null) {
if ($value === null) {
self::vfail($format, array_slice(func_get_args(), 2));
}
return $value;
}
/**
* Fails with the given message if $value is not null: $value !== null.
* @return mixed
*/
public static function isNull($value, $format, $args = null, $_ = null) {
if ($value !== null) {
self::vfail($format, array_slice(func_get_args(), 2));
}
return $value;
}
/**
* Fails with the given message if the variable $value is not set or null: !isset($value).
* @return mixed
*/
public static function isExisting(&$value, $format, $args = null, $_ = null) {
if (!isset($value)) {
self::vfail($format, array_slice(func_get_args(), 2));
}
return $value;
}
/**
* Fails with the given message if the variable $value is set: isset($value).
* @return mixed
*/
public static function isNotExisting(&$value, $format, $args = null, $_ = null) {
if (isset($value)) {
self::vfail($format, array_slice(func_get_args(), 2));
}
return $value;
}
/**
* Fails with the given message if $value is not empty: !empty($value).
* @return mixed
*/
public static function isEmpty($value, $format, $args = null, $_ = null) {
if (!empty($value)) {
self::vfail($format, array_slice(func_get_args(), 2));
}
return $value;
}
/**
* Fails with the given message if $value is empty: empty($value).
* @return mixed
*/
public static function isNotEmpty($value, $format, $args = null, $_ = null) {
if (empty($value)) {
self::vfail($format, array_slice(func_get_args(), 2));
}
return $value;
}
/**
* Fails with the given message if $value is not true: !$value.
* @return mixed
*/
public static function isTrue($value, $format, $args = null, $_ = null) {
if (!$value) {
self::vfail($format, array_slice(func_get_args(), 2));
}
return $value;
}
/**
* Fails with the given message if $value is true: $value.
* @return mixed
*/
public static function isFalse($value, $format, $args = null, $_ = null) {
if ($value) {
self::vfail($format, array_slice(func_get_args(), 2));
}
return $value;
}
/**
* Fails with the given message if $value not equal: ($expected != $value).
* @return mixed
*/
public static function isEqual($expected, $value, $format, $args = null, $_ = null) {
if ($expected != $value) {
self::vfail($format, array_slice(func_get_args(), 3));
}
return $value;
}
/**
* Fails with the given message if $value equal: ($expected != $value).
* @return mixed
*/
public static function isNotEqual($expected, $value, $format, $args = null, $_ = null) {
if ($expected == $value) {
self::vfail($format, array_slice(func_get_args(), 3));
}
return $value;
}
/**
* Fails with the given message if $value <= $expected.
* @return mixed
*/
public static function isGreaterThan($expected, $value, $format, $args = null, $_ = null) {
if ($expected >= $value) {
self::vfail($format, array_slice(func_get_args(), 3));
}
return $value;
}
/**
* Fails with the given message if $value < $expected.
* @return mixed
*/
public static function isGreaterThanOrEqual($expected, $value, $format, $args = null, $_ = null) {
if ($expected > $value) {
self::vfail($format, array_slice(func_get_args(), 3));
}
return $value;
}
/**
* Fails with the given message if $value >= $expected.
* @return mixed
*/
public static function isLessThan($expected, $value, $format, $args = null, $_ = null) {
if ($expected <= $value) {
self::vfail($format, array_slice(func_get_args(), 3));
}
return $value;
}
/**
* Fails with the given message if $value > $expected.
* @return mixed
*/
public static function isLessThanOrEqual($expected, $value, $format, $args = null, $_ = null) {
if ($expected < $value) {
self::vfail($format, array_slice(func_get_args(), 3));
}
return $value;
}
/**
* Fails with the given message if $value is not instanceof $cl: !($value instanceof $cl).
* @return mixed
*/
public static function isInstanceOf($cl, $value, $format, $args = null, $_ = null) {
if (!($value instanceof $cl)) {
self::vfail($format, array_slice(func_get_args(), 3));
}
return $value;
}
/**
* Fails with the given message if $value is instanceof $cl: ($value instanceof $cl).
* @return mixed
*/
public static function isNotInstanceOf($cl, $value, $format, $args = null, $_ = null) {
if ($value instanceof $cl) {
self::vfail($format, array_slice(func_get_args(), 3));
}
return $value;
}
/**
* Fails with the given message if $value is not a valid number.
* @return mixed
*/
public static function isNumeric($value, $format, $args = null, $_ = null) {
if (!is_numeric($value)) {
self::vfail($format, array_slice(func_get_args(), 2));
}
return $value;
}
/**
* Simply fails with the given message in sprintf format. May be used when
* the code has reached a point where it shouldn't be, for example the default
* case of a switch.
*/
public static function fail($format, $args = null, $_ = null) {
throw new EnsureException(vsprintf($format, array_slice(func_get_args(), 1)));
}
/**
* Simply fails with the given message in sprintf format. May be used when
* the code has reached a point where it shouldn't be, for example the default
* case of a switch.
*/
private static function vfail($format, $args) {
throw new EnsureException(vsprintf($format, $args));
}
}