Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathUtil.php
More file actions
Latest commit
319 lines (286 loc) · 11.9 KB
/
Copy pathUtil.php
File metadata and controls
319 lines (286 loc) · 11.9 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
namespaceTraderInteractive;
useErrorException;
useException;
useInvalidArgumentException;
useReflectionClass;
useReflectionException;
useThrowable;
/**
* Static class with various application functions.
*/
finalclass Util
{
privatestatic$exceptionAliases = ['http' => '\TraderInteractive\HttpException'];
/**
* Returns exception info in array.
*
* @param Throwable $t the exception to return info on
*
* @return array like:
* <pre>
* [
* 'type' => 'Exception',
* 'message' => 'a message',
* 'code' => 0,
* 'file' => '/somePath',
* 'line' => 434,
* 'trace' => 'a stack trace',
* ]
* </pre>
*/
publicstaticfunctiongetExceptionInfo(Throwable$t) : array
{
return [
'type' => get_class($t),
'message' => $t->getMessage(),
'code' => $t->getCode(),
'file' => $t->getFile(),
'line' => $t->getLine(),
'trace' => $t->getTraceAsString(),
];
}
/**
* Ensures that $valueToEnsure is equal to $valueToCheck or it throws
*
* Can be used like: $result = ensure(true, is_string('boo'))
* Or like: $result = ensure(true, is_string('boo'), 'the message')
* Or like: $result = ensure(true, is_string('boo'), 'MyException', ['the message', 2])
* Or like: $result = ensure(true, is_string('boo'), new MyException('the message', 2))
*
* @param mixed $valueToEnsure the value to throw on if $valueToCheck equals it
* @param mixed $valueToCheck the value to check against $valueToEnsure
* @param mixed $exception null, a fully qualified exception class name, string for an Exception
* message, or an Exception. The fully qualified exception class name
* could also be an alias in getExceptionAliases()
*
* @param array|null $exceptionArgs arguments to pass to a new instance of $exception. If using this
* parameter make sure these arguments match the constructor for an
* exception of type $exception.
*
* @return mixed returns $valueToCheck
*
* @throws Exception if $valueToEnsure !== $valueToCheck
* @throws InvalidArgumentException if $exception was not null, a string, or an Exception
*/
publicstaticfunctionensure($valueToEnsure, $valueToCheck, $exception = null, array$exceptionArgs = null)
{
if ($valueToEnsure !== $valueToCheck) {
throwself::buildException(
$exception ?: "'{$valueToEnsure}' did not equal '{$valueToCheck}'",
$exceptionArgs
);
}
return$valueToCheck;
}
/**
* Ensures that $valueToThrowOn is not equal to $valueToCheck or it throws
*
* Can be used like: $curl = ensureNot(false, curl_init('boo'))
* Or like: $curl = ensureNot(false, curl_init('boo'), 'bad message')
* Or like: $curl = ensureNot(false, curl_init('boo'), 'MyException', ['bad message', 2])
* Or like: $curl = ensureNot(false, curl_init('boo'), new MyException('bad message', 2))
*
* @param mixed $valueToThrowOn the value to throw on if $valueToCheck equals it
* @param mixed $valueToCheck the value to check against $valueToThrowOn
* @param null|string|Exception $exception null, a fully qualified exception class name, string for an
* Exception message, or an Exception. The fully qualified exception
* class name could also be an alias in getExceptionAliases()
* @param array|null $exceptionArgs arguments to pass to a new instance of $exception. If using this
* parameter make sure these arguments match the constructor for an
* exception of type $exception.
*
* @return mixed returns $valueToCheck
*
* @throws Exception if $valueToThrowOn === $valueToCheck
* @throws InvalidArgumentException if $exception was not null, a string, or an Exception
*/
publicstaticfunctionensureNot($valueToThrowOn, $valueToCheck, $exception = null, array$exceptionArgs = null)
{
if ($valueToThrowOn === $valueToCheck) {
throwself::buildException($exception ?: "'{$valueToThrowOn}' equals '{$valueToCheck}'", $exceptionArgs);
}
return$valueToCheck;
}
/**
* Helper method to return exception created from ensure[Not] call input.
*
* @param mixed $exception Null, a fully qualified exception class name, string for an Exception message,
* or an Exception. The fully qualified exception class name could also be an
* alias in getExceptionAliases()
* @param array|null $exceptionArgs Arguments to pass to a new instance of $exception. If using this parameter make
* sure these arguments match the constructor for an exception of type $exception.
*
* @return Throwable
*
* @throws ReflectionException
*/
privatestaticfunctionbuildException($exception, array$exceptionArgs = null) : Throwable
{
if ($exceptioninstanceof Throwable) {
return$exception;
}
if (!is_string($exception)) {
thrownewInvalidArgumentException('$exception was not null, a string, or an Exception');
}
if (empty($exceptionArgs)) {
returnnewException($exception);
}
if (array_key_exists($exception, self::$exceptionAliases)) {
$exception = self::$exceptionAliases[$exception];
}
return (newReflectionClass($exception))->newInstanceArgs($exceptionArgs);
}
/**
* Throws a new ErrorException based on the error information provided. To be
* used as a callback for @see set_error_handler()
*
* @param int $level The level of the exception.
* @param string $message The message the exception will give.
* @param string $file The file that the error occurred in.
* @param string $line The line that the exception occurred upon.
*
* @return bool false
*
* @throws ErrorException
*/
publicstaticfunctionraiseException(int$level, string$message, string$file = null, string$line = null) : bool
{
if (error_reporting() === 0) {
returnfalse;
}
thrownewErrorException($message, 0, $level, $file, $line);
}
/**
* Throws an exception if specified variables are not of given types.
*
* @param array $typesToVariables like ['string' => [$var1, $var2], 'int' => [$var1, $var2]] or
* ['string' => $var1, 'integer' => [1, $var2]]. Supported types are the suffixes
* of the is_* functions such as string for is_string and int for is_int
* @param bool $failOnWhitespace whether to fail strings if they are whitespace
* @param bool $allowNulls whether to allow null values to pass through
*
* @return void
*
* @throws InvalidArgumentException if a key in $typesToVariables was not a string
* @throws InvalidArgumentException if a key in $typesToVariables did not have an is_ function
* @throws InvalidArgumentException if a variable is not of correct type
* @throws InvalidArgumentException if a variable is whitespace and $failOnWhitespace is set
*/
publicstaticfunctionthrowIfNotType(
array$typesToVariables,
bool$failOnWhitespace = false,
bool$allowNulls = false
) {
foreach ($typesToVariablesas$type => $variablesOrVariable) {
self::handleTypesToVariables($failOnWhitespace, $allowNulls, $variablesOrVariable, $type);
}
}
/**
* Return the exception aliases.
*
* @return array array where keys are aliases and values are strings to a fully qualified exception class names.
*/
publicstaticfunctiongetExceptionAliases() : array
{
returnself::$exceptionAliases;
}
/**
* Set the exception aliases.
*
* @param array $aliases array where keys are aliases and values are strings to a fully qualified exception class
* names.
*/
publicstaticfunctionsetExceptionAliases(array$aliases)
{
self::$exceptionAliases = $aliases;
}
privatestaticfunctionhandleBoolCase(bool$allowNulls, array$variables)
{
foreach ($variablesas$i => $variable) {
//using the continue here not negative checks to make use of short cutting optimization.
if ($variable === false || $variable === true || ($allowNulls && $variable === null)) {
continue;
}
thrownewInvalidArgumentException("variable at position '{$i}' was not a boolean");
}
}
privatestaticfunctionhandleNullCase(array$variables)
{
foreach ($variablesas$i => $variable) {
if ($variable !== null) {
thrownewInvalidArgumentException("variable at position '{$i}' was not null");
}
}
}
privatestaticfunctionhandleStringCase(bool$failOnWhitespace, bool$allowNulls, array$variables, string$type)
{
foreach ($variablesas$i => $variable) {
if (is_string($variable)) {
if ($failOnWhitespace && trim($variable) === '') {
thrownewInvalidArgumentException("variable at position '{$i}' was whitespace");
}
continue;
}
if ($allowNulls && $variable === null) {
continue;
}
thrownewInvalidArgumentException("variable at position '{$i}' was not a '{$type}'");
}
}
privatestaticfunctionhandleDefaultCase(bool$allowNulls, string$type, array$variables)
{
$isFunction = "is_{$type}";
foreach ($variablesas$i => $variable) {
if ($isFunction($variable) || ($allowNulls && $variable === null)) {
continue;
}
thrownewInvalidArgumentException("variable at position '{$i}' was not a '{$type}'");
}
}
privatestaticfunctionhandleTypesToVariables(
bool$failOnWhitespace,
bool$allowNulls,
$variablesOrVariable,
$type
) {
$variables = [$variablesOrVariable];
if (is_array($variablesOrVariable)) {
$variables = $variablesOrVariable;
}
//cast ok since an integer won't match any of the cases.
//the similar code in the cases is an optimization for those type where faster checks can be made.
$typeString = (string)$type;
if ($typeString === 'bool') {
self::handleBoolCase($allowNulls, $variables);
return;
}
if ($typeString === 'null') {
self::handleNullCase($variables);
return;
}
if ($typeString === 'string') {
self::handleStringCase($failOnWhitespace, $allowNulls, $variables, $type);
return;
}
$defaults = [
'array',
'callable',
'double',
'float',
'int',
'integer',
'long',
'numeric',
'object',
'real',
'resource',
'scalar',
];
if (in_array($typeString, $defaults)) {
self::handleDefaultCase($allowNulls, $type, $variables);
return;
}
thrownewInvalidArgumentException('a type was not one of the is_ functions');
}
}