PluginProbe
Packeta / 1.6.1
Packeta v1.6.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / schema / src / Schema / Expect.php

Expect.php in Packeta 1.6.1, at deps/nette/schema/src/Schema/Expect.php

101 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 declare (strict_types=1);
8 namespace Packetery\Nette\Schema;
9
10 use Packetery\Nette;
11 use Packetery\Nette\Schema\Elements\AnyOf;
12 use Packetery\Nette\Schema\Elements\Structure;
13 use Packetery\Nette\Schema\Elements\Type;
14 /**
15 * Schema generator.
16 *
17 * @method static Type scalar($default = null)
18 * @method static Type string($default = null)
19 * @method static Type int($default = null)
20 * @method static Type float($default = null)
21 * @method static Type bool($default = null)
22 * @method static Type null()
23 * @method static Type array($default = [])
24 * @method static Type list($default = [])
25 * @method static Type mixed($default = null)
26 * @method static Type email($default = null)
27 * @method static Type unicode($default = null)
28 */
29 final class Expect
30 {
31 use \Packetery\Nette\SmartObject;
32 public static function __callStatic(string $name, array $args) : Type
33 {
34 $type = new Type($name);
35 if ($args) {
36 $type->default($args[0]);
37 }
38 return $type;
39 }
40 public static function type(string $type) : Type
41 {
42 return new Type($type);
43 }
44 /**
45 * @param mixed|Schema ...$set
46 */
47 public static function anyOf(...$set) : AnyOf
48 {
49 return new AnyOf(...$set);
50 }
51 /**
52 * @param Schema[] $items
53 */
54 public static function structure(array $items) : Structure
55 {
56 return new Structure($items);
57 }
58 /**
59 * @param object $object
60 */
61 public static function from($object, array $items = []) : Structure
62 {
63 $ro = new \ReflectionObject($object);
64 foreach ($ro->getProperties() as $prop) {
65 $type = Helpers::getPropertyType($prop) ?? 'mixed';
66 $item =& $items[$prop->getName()];
67 if (!$item) {
68 $item = new Type($type);
69 if (\PHP_VERSION_ID >= 70400 && !$prop->isInitialized($object)) {
70 $item->required();
71 } else {
72 $def = $prop->getValue($object);
73 if (\is_object($def)) {
74 $item = static::from($def);
75 } elseif ($def === null && !Nette\Utils\Validators::is(null, $type)) {
76 $item->required();
77 } else {
78 $item->default($def);
79 }
80 }
81 }
82 }
83 return (new Structure($items))->castTo($ro->getName());
84 }
85 /**
86 * @param string|Schema $valueType
87 * @param string|Schema|null $keyType
88 */
89 public static function arrayOf($valueType, $keyType = null) : Type
90 {
91 return (new Type('array'))->items($valueType, $keyType);
92 }
93 /**
94 * @param string|Schema $type
95 */
96 public static function listOf($type) : Type
97 {
98 return (new Type('list'))->items($type);
99 }
100 }
101