Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Zur aktuellen Revision | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 148 Revision 150
Zeile 17... Zeile 17...
17
 */
17
 */
18
class Validators
18
class Validators
19
{
19
{
20
	use Nette\StaticClass;
20
	use Nette\StaticClass;
Zeile -... Zeile 21...
-
 
21
 
-
 
22
	private const BuiltinTypes = [
-
 
23
		'string' => 1, 'int' => 1, 'float' => 1, 'bool' => 1, 'array' => 1, 'object' => 1,
-
 
24
		'callable' => 1, 'iterable' => 1, 'void' => 1, 'null' => 1, 'mixed' => 1, 'false' => 1,
-
 
25
		'never' => 1, 'true' => 1,
-
 
26
	];
21
 
27
 
22
	/** @var array<string,?callable> */
28
	/** @var array<string,?callable> */
23
	protected static $validators = [
29
	protected static $validators = [
24
		// PHP types
30
		// PHP types
25
		'array' => 'is_array',
31
		'array' => 'is_array',
Zeile 116... Zeile 122...
116
	public static function assertField(
122
	public static function assertField(
117
		array $array,
123
		array $array,
118
		$key,
124
		$key,
119
		?string $expected = null,
125
		?string $expected = null,
120
		string $label = "item '%' in array"
126
		string $label = "item '%' in array"
121
	): void {
127
	): void
-
 
128
	{
122
		if (!array_key_exists($key, $array)) {
129
		if (!array_key_exists($key, $array)) {
123
			throw new AssertionException('Missing ' . str_replace('%', $key, $label) . '.');
130
			throw new AssertionException('Missing ' . str_replace('%', $key, $label) . '.');
Zeile 124... Zeile 131...
124
 
131
 
125
		} elseif ($expected) {
132
		} elseif ($expected) {
Zeile 325... Zeile 332...
325
			@
332
			@
326
			([0-9$alpha]([-0-9$alpha]{0,61}[0-9$alpha])?\\.)+  # domain - RFC 1034
333
			([0-9$alpha]([-0-9$alpha]{0,61}[0-9$alpha])?\\.)+  # domain - RFC 1034
327
			[$alpha]([-0-9$alpha]{0,17}[$alpha])?              # top domain
334
			[$alpha]([-0-9$alpha]{0,17}[$alpha])?              # top domain
328
		$)Dix
335
		$)Dix
329
XX
336
XX
330
, $value);
337
			, $value);
331
	}
338
	}
Zeile 332... Zeile 339...
332
 
339
 
333
 
340
 
Zeile 349... Zeile 356...
349
			(/\\S*)?                                        # path
356
			(/\\S*)?                                        # path
350
			(\\?\\S*)?                                      # query
357
			(\\?\\S*)?                                      # query
351
			(\\#\\S*)?                                      # fragment
358
			(\\#\\S*)?                                      # fragment
352
		$)Dix
359
		$)Dix
353
XX
360
XX
354
, $value);
361
			, $value);
355
	}
362
	}
Zeile 356... Zeile 363...
356
 
363
 
357
 
364
 
Zeile 378... Zeile 385...
378
	 */
385
	 */
379
	public static function isPhpIdentifier(string $value): bool
386
	public static function isPhpIdentifier(string $value): bool
380
	{
387
	{
381
		return preg_match('#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#D', $value) === 1;
388
		return preg_match('#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#D', $value) === 1;
382
	}
389
	}
-
 
390
 
-
 
391
 
-
 
392
	/**
-
 
393
	 * Determines if type is PHP built-in type. Otherwise, it is the class name.
-
 
394
	 */
-
 
395
	public static function isBuiltinType(string $type): bool
-
 
396
	{
-
 
397
		return isset(self::BuiltinTypes[strtolower($type)]);
-
 
398
	}
-
 
399
 
-
 
400
 
-
 
401
	/**
-
 
402
	 * Determines if type is special class name self/parent/static.
-
 
403
	 */
-
 
404
	public static function isClassKeyword(string $name): bool
-
 
405
	{
-
 
406
		return (bool) preg_match('#^(self|parent|static)$#Di', $name);
-
 
407
	}
-
 
408
 
-
 
409
 
-
 
410
	/**
-
 
411
	 * Checks whether the given type declaration is syntactically valid.
-
 
412
	 */
-
 
413
	public static function isTypeDeclaration(string $type): bool
-
 
414
	{
-
 
415
		return (bool) preg_match(<<<'XX'
-
 
416
		~(
-
 
417
			\?? (?<type> \\? (?<name> [a-zA-Z_\x7f-\xff][\w\x7f-\xff]*) (\\ (?&name))* ) |
-
 
418
			(?<intersection> (?&type) (& (?&type))+ ) |
-
 
419
			(?<upart> (?&type) | \( (?&intersection) \) )  (\| (?&upart))+
-
 
420
		)$~xAD
-
 
421
XX
-
 
422
			, $type);
-
 
423
	}
383
}
424
}