Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 150 | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 150 Revision 399
Zeile 91... Zeile 91...
91
	];
91
	];
Zeile 92... Zeile 92...
92
 
92
 
93
 
93
 
94
	/**
-
 
95
	 * Verifies that the value is of expected types separated by pipe.
94
	/**
96
	 * @param  mixed  $value
95
	 * Verifies that the value is of expected types separated by pipe.
97
	 * @throws AssertionException
96
	 * @throws AssertionException
98
	 */
97
	 */
99
	public static function assert($value, string $expected, string $label = 'variable'): void
98
	public static function assert(mixed $value, string $expected, string $label = 'variable'): void
100
	{
99
	{
101
		if (!static::is($value, $expected)) {
100
		if (!static::is($value, $expected)) {
102
			$expected = str_replace(['|', ':'], [' or ', ' in range '], $expected);
101
			$expected = str_replace(['|', ':'], [' or ', ' in range '], $expected);
103
			$translate = ['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'NULL' => 'null'];
102
			$translate = ['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'NULL' => 'null'];
104
			$type = $translate[gettype($value)] ?? gettype($value);
103
			$type = $translate[gettype($value)] ?? gettype($value);
105
			if (is_int($value) || is_float($value) || (is_string($value) && strlen($value) < 40)) {
104
			if (is_int($value) || is_float($value) || (is_string($value) && strlen($value) < 40)) {
106
				$type .= ' ' . var_export($value, true);
105
				$type .= ' ' . var_export($value, true);
107
			} elseif (is_object($value)) {
106
			} elseif (is_object($value)) {
Zeile 108... Zeile 107...
108
				$type .= ' ' . get_class($value);
107
				$type .= ' ' . $value::class;
109
			}
108
			}
110
 
109
 
Zeile 111... Zeile 110...
111
			throw new AssertionException("The $label expects to be $expected, $type given.");
110
			throw new AssertionException("The $label expects to be $expected, $type given.");
112
		}
111
		}
113
	}
112
	}
114
 
-
 
115
 
113
 
116
	/**
114
 
117
	 * Verifies that element $key in array is of expected types separated by pipe.
115
	/**
118
	 * @param  mixed[]  $array
116
	 * Verifies that element $key in array is of expected types separated by pipe.
119
	 * @param  int|string  $key
117
	 * @param  mixed[]  $array
120
	 * @throws AssertionException
118
	 * @throws AssertionException
121
	 */
119
	 */
122
	public static function assertField(
120
	public static function assertField(
123
		array $array,
121
		array $array,
124
		$key,
122
		$key,
125
		?string $expected = null,
123
		?string $expected = null,
Zeile 135... Zeile 133...
135
	}
133
	}
Zeile 136... Zeile 134...
136
 
134
 
137
 
135
 
138
	/**
-
 
139
	 * Verifies that the value is of expected types separated by pipe.
136
	/**
140
	 * @param  mixed  $value
137
	 * Verifies that the value is of expected types separated by pipe.
141
	 */
138
	 */
142
	public static function is($value, string $expected): bool
139
	public static function is(mixed $value, string $expected): bool
143
	{
140
	{
144
		foreach (explode('|', $expected) as $item) {
141
		foreach (explode('|', $expected) as $item) {
145
			if (substr($item, -2) === '[]') {
142
			if (str_ends_with($item, '[]')) {
146
				if (is_iterable($value) && self::everyIs($value, substr($item, 0, -2))) {
143
				if (is_iterable($value) && self::everyIs($value, substr($item, 0, -2))) {
Zeile 147... Zeile 144...
147
					return true;
144
					return true;
148
				}
145
				}
149
 
146
 
150
				continue;
147
				continue;
151
			} elseif (substr($item, 0, 1) === '?') {
148
			} elseif (str_starts_with($item, '?')) {
152
				$item = substr($item, 1);
149
				$item = substr($item, 1);
153
				if ($value === null) {
150
				if ($value === null) {
Zeile 213... Zeile 210...
213
	}
210
	}
Zeile 214... Zeile 211...
214
 
211
 
215
 
212
 
216
	/**
-
 
217
	 * Checks if the value is an integer or a float.
213
	/**
218
	 * @param  mixed  $value
214
	 * Checks if the value is an integer or a float.
219
	 */
215
	 */
220
	public static function isNumber($value): bool
216
	public static function isNumber(mixed $value): bool
221
	{
217
	{
Zeile 222... Zeile 218...
222
		return is_int($value) || is_float($value);
218
		return is_int($value) || is_float($value);
223
	}
219
	}
224
 
-
 
225
 
220
 
226
	/**
221
 
227
	 * Checks if the value is an integer or a integer written in a string.
222
	/**
228
	 * @param  mixed  $value
223
	 * Checks if the value is an integer or a integer written in a string.
229
	 */
224
	 */
Zeile 230... Zeile 225...
230
	public static function isNumericInt($value): bool
225
	public static function isNumericInt(mixed $value): bool
231
	{
226
	{
232
		return is_int($value) || (is_string($value) && preg_match('#^[+-]?[0-9]+$#D', $value));
-
 
233
	}
227
		return is_int($value) || (is_string($value) && preg_match('#^[+-]?[0-9]+$#D', $value));
234
 
228
	}
235
 
229
 
236
	/**
230
 
237
	 * Checks if the value is a number or a number written in a string.
231
	/**
Zeile 238... Zeile 232...
238
	 * @param  mixed  $value
232
	 * Checks if the value is a number or a number written in a string.
239
	 */
233
	 */
240
	public static function isNumeric($value): bool
-
 
241
	{
234
	public static function isNumeric(mixed $value): bool
242
		return is_float($value) || is_int($value) || (is_string($value) && preg_match('#^[+-]?([0-9]++\.?[0-9]*|\.[0-9]+)$#D', $value));
235
	{
243
	}
236
		return is_float($value) || is_int($value) || (is_string($value) && preg_match('#^[+-]?([0-9]++\.?[0-9]*|\.[0-9]+)$#D', $value));
244
 
237
	}
245
 
238
 
Zeile 246... Zeile 239...
246
	/**
239
 
247
	 * Checks if the value is a syntactically correct callback.
240
	/**
248
	 * @param  mixed  $value
-
 
249
	 */
241
	 * Checks if the value is a syntactically correct callback.
250
	public static function isCallable($value): bool
242
	 */
251
	{
243
	public static function isCallable(mixed $value): bool
252
		return $value && is_callable($value, true);
244
	{
253
	}
245
		return $value && is_callable($value, true);
Zeile 254... Zeile 246...
254
 
246
	}
255
 
247
 
256
	/**
-
 
257
	 * Checks if the value is a valid UTF-8 string.
248
 
258
	 * @param  mixed  $value
249
	/**
259
	 */
250
	 * Checks if the value is a valid UTF-8 string.
260
	public static function isUnicode($value): bool
251
	 */
261
	{
252
	public static function isUnicode(mixed $value): bool
Zeile 280... Zeile 271...
280
	}
271
	}
Zeile 281... Zeile 272...
281
 
272
 
282
 
273
 
283
	/**
-
 
284
	 * Checks if a variable is a zero-based integer indexed array.
274
	/**
285
	 * @param  mixed  $value
275
	 * Checks if a variable is a zero-based integer indexed array.
286
	 * @deprecated  use Nette\Utils\Arrays::isList
276
	 * @deprecated  use Nette\Utils\Arrays::isList
287
	 */
277
	 */
288
	public static function isList($value): bool
278
	public static function isList(mixed $value): bool
289
	{
279
	{
Zeile 290... Zeile 280...
290
		return Arrays::isList($value);
280
		return Arrays::isList($value);
291
	}
281
	}
292
 
282
 
293
 
-
 
294
	/**
283
 
295
	 * Checks if the value is in the given range [min, max], where the upper or lower limit can be omitted (null).
284
	/**
296
	 * Numbers, strings and DateTime objects can be compared.
285
	 * Checks if the value is in the given range [min, max], where the upper or lower limit can be omitted (null).
297
	 * @param  mixed  $value
286
	 * Numbers, strings and DateTime objects can be compared.
298
	 */
287
	 */
299
	public static function isInRange($value, array $range): bool
288
	public static function isInRange(mixed $value, array $range): bool
Zeile 325... Zeile 314...
325
	public static function isEmail(string $value): bool
314
	public static function isEmail(string $value): bool
326
	{
315
	{
327
		$atom = "[-a-z0-9!#$%&'*+/=?^_`{|}~]"; // RFC 5322 unquoted characters in local-part
316
		$atom = "[-a-z0-9!#$%&'*+/=?^_`{|}~]"; // RFC 5322 unquoted characters in local-part
328
		$alpha = "a-z\x80-\xFF"; // superset of IDN
317
		$alpha = "a-z\x80-\xFF"; // superset of IDN
329
		return (bool) preg_match(<<<XX
318
		return (bool) preg_match(<<<XX
330
		(^
319
			(^(?n)
331
			("([ !#-[\\]-~]*|\\\\[ -~])+"|$atom+(\\.$atom+)*)  # quoted or unquoted
320
				("([ !#-[\\]-~]*|\\\\[ -~])+"|$atom+(\\.$atom+)*)  # quoted or unquoted
332
			@
321
				@
333
			([0-9$alpha]([-0-9$alpha]{0,61}[0-9$alpha])?\\.)+  # domain - RFC 1034
322
				([0-9$alpha]([-0-9$alpha]{0,61}[0-9$alpha])?\\.)+  # domain - RFC 1034
334
			[$alpha]([-0-9$alpha]{0,17}[$alpha])?              # top domain
323
				[$alpha]([-0-9$alpha]{0,17}[$alpha])?              # top domain
335
		$)Dix
324
			$)Dix
336
XX
-
 
337
			, $value);
325
			XX, $value);
338
	}
326
	}
Zeile 339... Zeile 327...
339
 
327
 
340
 
328
 
341
	/**
329
	/**
342
	 * Checks if the value is a valid URL address.
330
	 * Checks if the value is a valid URL address.
343
	 */
331
	 */
344
	public static function isUrl(string $value): bool
332
	public static function isUrl(string $value): bool
345
	{
333
	{
346
		$alpha = "a-z\x80-\xFF";
334
		$alpha = "a-z\x80-\xFF";
347
		return (bool) preg_match(<<<XX
335
		return (bool) preg_match(<<<XX
348
		(^
336
			(^(?n)
349
			https?://(
337
				https?://(
350
				(([-_0-9$alpha]+\\.)*                       # subdomain
338
					(([-_0-9$alpha]+\\.)*                       # subdomain
351
					[0-9$alpha]([-0-9$alpha]{0,61}[0-9$alpha])?\\.)?  # domain
339
						[0-9$alpha]([-0-9$alpha]{0,61}[0-9$alpha])?\\.)?  # domain
352
					[$alpha]([-0-9$alpha]{0,17}[$alpha])?   # top domain
340
						[$alpha]([-0-9$alpha]{0,17}[$alpha])?   # top domain
353
				|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}  # IPv4
341
					|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}  # IPv4
354
				|\\[[0-9a-f:]{3,39}\\]                      # IPv6
342
					|\\[[0-9a-f:]{3,39}\\]                      # IPv6
355
			)(:\\d{1,5})?                                   # port
343
				)(:\\d{1,5})?                                   # port
356
			(/\\S*)?                                        # path
344
				(/\\S*)?                                        # path
357
			(\\?\\S*)?                                      # query
345
				(\\?\\S*)?                                      # query
358
			(\\#\\S*)?                                      # fragment
-
 
359
		$)Dix
346
				(\\#\\S*)?                                      # fragment
360
XX
347
			$)Dix
Zeile 361... Zeile 348...
361
			, $value);
348
			XX, $value);
362
	}
349
	}
Zeile 371... Zeile 358...
371
	}
358
	}
Zeile 372... Zeile 359...
372
 
359
 
373
 
360
 
-
 
361
	/**
374
	/**
362
	 * Checks whether the input is a class, interface or trait.
375
	 * Checks whether the input is a class, interface or trait.
363
	 * @deprecated
376
	 */
364
	 */
377
	public static function isType(string $type): bool
365
	public static function isType(string $type): bool
378
	{
366
	{
Zeile 411... Zeile 399...
411
	 * Checks whether the given type declaration is syntactically valid.
399
	 * Checks whether the given type declaration is syntactically valid.
412
	 */
400
	 */
413
	public static function isTypeDeclaration(string $type): bool
401
	public static function isTypeDeclaration(string $type): bool
414
	{
402
	{
415
		return (bool) preg_match(<<<'XX'
403
		return (bool) preg_match(<<<'XX'
416
		~(
404
			~((?n)
417
			\?? (?<type> \\? (?<name> [a-zA-Z_\x7f-\xff][\w\x7f-\xff]*) (\\ (?&name))* ) |
405
				\?? (?<type> \\? (?<name> [a-zA-Z_\x7f-\xff][\w\x7f-\xff]*) (\\ (?&name))* ) |
418
			(?<intersection> (?&type) (& (?&type))+ ) |
406
				(?<intersection> (?&type) (& (?&type))+ ) |
419
			(?<upart> (?&type) | \( (?&intersection) \) )  (\| (?&upart))+
407
				(?<upart> (?&type) | \( (?&intersection) \) )  (\| (?&upart))+
420
		)$~xAD
408
			)$~xAD
421
XX
-
 
422
			, $type);
409
			XX, $type);
423
	}
410
	}
424
}
411
}