Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7
 
8
declare(strict_types=1);
9
 
10
namespace Nette\Schema;
11
 
12
use Nette;
13
use Nette\Schema\Elements\AnyOf;
14
use Nette\Schema\Elements\Structure;
15
use Nette\Schema\Elements\Type;
16
 
17
 
18
/**
19
 * Schema generator.
20
 *
21
 * @method static Type scalar($default = null)
22
 * @method static Type string($default = null)
23
 * @method static Type int($default = null)
24
 * @method static Type float($default = null)
25
 * @method static Type bool($default = null)
26
 * @method static Type null()
27
 * @method static Type array($default = [])
28
 * @method static Type list($default = [])
29
 * @method static Type mixed($default = null)
30
 * @method static Type email($default = null)
31
 * @method static Type unicode($default = null)
32
 */
33
final class Expect
34
{
35
	use Nette\SmartObject;
36
 
37
	public static function __callStatic(string $name, array $args): Type
38
	{
39
		$type = new Type($name);
40
		if ($args) {
41
			$type->default($args[0]);
42
		}
43
 
44
		return $type;
45
	}
46
 
47
 
48
	public static function type(string $type): Type
49
	{
50
		return new Type($type);
51
	}
52
 
53
 
54
	/**
55
	 * @param  mixed|Schema  ...$set
56
	 */
57
	public static function anyOf(...$set): AnyOf
58
	{
59
		return new AnyOf(...$set);
60
	}
61
 
62
 
63
	/**
64
	 * @param  Schema[]  $items
65
	 */
66
	public static function structure(array $items): Structure
67
	{
68
		return new Structure($items);
69
	}
70
 
71
 
72
	/**
73
	 * @param  object  $object
74
	 */
75
	public static function from($object, array $items = []): Structure
76
	{
77
		$ro = new \ReflectionObject($object);
78
		foreach ($ro->getProperties() as $prop) {
79
			$type = Helpers::getPropertyType($prop) ?? 'mixed';
80
			$item = &$items[$prop->getName()];
81
			if (!$item) {
82
				$item = new Type($type);
83
				if (PHP_VERSION_ID >= 70400 && !$prop->isInitialized($object)) {
84
					$item->required();
85
				} else {
86
					$def = $prop->getValue($object);
87
					if (is_object($def)) {
88
						$item = static::from($def);
89
					} elseif ($def === null && !Nette\Utils\Validators::is(null, $type)) {
90
						$item->required();
91
					} else {
92
						$item->default($def);
93
					}
94
				}
95
			}
96
		}
97
 
98
		return (new Structure($items))->castTo($ro->getName());
99
	}
100
 
101
 
102
	/**
103
	 * @param  string|Schema  $valueType
104
	 * @param  string|Schema|null  $keyType
105
	 */
106
	public static function arrayOf($valueType, $keyType = null): Type
107
	{
108
		return (new Type('array'))->items($valueType, $keyType);
109
	}
110
 
111
 
112
	/**
113
	 * @param  string|Schema  $type
114
	 */
115
	public static function listOf($type): Type
116
	{
117
		return (new Type('list'))->items($type);
118
	}
119
}