1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonSchema\Structure; |
4
|
|
|
|
5
|
|
|
use Swaggest\JsonSchema\Constraint\Properties; |
6
|
|
|
use Swaggest\JsonSchema\Context; |
7
|
|
|
use Swaggest\JsonSchema\NameMirror; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @method static setupProperties($properties, $schema) |
11
|
|
|
*/ |
12
|
|
|
trait ClassStructureTrait |
13
|
|
|
{ |
14
|
|
|
use ObjectItemTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @return ClassSchema |
18
|
|
|
*/ |
19
|
|
|
public static function schema() |
20
|
|
|
{ |
21
|
|
|
static $schemas = array(); |
22
|
|
|
$className = get_called_class(); |
23
|
|
|
$schema = &$schemas[$className]; |
24
|
|
|
|
25
|
|
|
if (null === $schema) { |
26
|
|
|
$schema = new ClassSchema(); |
27
|
|
|
$properties = new Properties(); |
28
|
|
|
$schema->properties = $properties; |
29
|
|
|
$schema->objectItemClass = $className; |
30
|
|
|
static::setUpProperties($properties, $schema); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $schema; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return Properties|static |
38
|
|
|
*/ |
39
|
|
|
public static function properties() |
40
|
|
|
{ |
41
|
|
|
return static::schema()->properties; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param mixed $data |
46
|
|
|
* @param Context $options |
47
|
|
|
* @return static |
48
|
|
|
*/ |
49
|
|
|
public static function import($data, Context $options = null) |
50
|
|
|
{ |
51
|
|
|
return static::schema()->in($data, $options); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param mixed $data |
56
|
|
|
* @param Context $options |
57
|
|
|
* @return mixed |
58
|
|
|
* @throws \Swaggest\JsonSchema\InvalidValue |
59
|
|
|
*/ |
60
|
|
|
public static function export($data, Context $options = null) |
61
|
|
|
{ |
62
|
|
|
return static::schema()->out($data, $options); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param ObjectItemContract $objectItem |
67
|
|
|
* @return static |
68
|
|
|
*/ |
69
|
|
|
public static function pick(ObjectItemContract $objectItem) |
70
|
|
|
{ |
71
|
|
|
$className = get_called_class(); |
72
|
|
|
return $objectItem->getNestedObject($className); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return static |
77
|
|
|
*/ |
78
|
|
|
public static function create() |
79
|
|
|
{ |
80
|
|
|
return new static; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected $__validateOnSet = true; // todo skip validation during import |
84
|
|
|
|
85
|
|
|
public function jsonSerialize() |
86
|
|
|
{ |
87
|
|
|
$result = new \stdClass(); |
88
|
|
|
$properties = static::schema()->properties; |
89
|
|
|
foreach ($properties->toArray() as $name => $schema) { |
90
|
|
|
$value = $this->$name; |
91
|
|
|
if ((null !== $value) || array_key_exists($name, $this->__arrayOfData)) { |
92
|
|
|
$result->$name = $value; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
foreach ($properties->nestedPropertyNames as $name) { |
96
|
|
|
/** @var ObjectItem $nested */ |
97
|
|
|
$nested = $this->$name; |
98
|
|
|
if (null !== $nested) { |
99
|
|
|
foreach ((array)$nested->jsonSerialize() as $key => $value) { |
100
|
|
|
$result->$key = $value; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $result; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return static |
110
|
|
|
*/ |
111
|
|
|
public static function names() |
112
|
|
|
{ |
113
|
|
|
static $nameflector = null; |
114
|
|
|
if (null === $nameflector) { |
115
|
|
|
$nameflector = new NameMirror(); |
116
|
|
|
} |
117
|
|
|
return $nameflector; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function __set($name, $column) // todo nested schemas |
121
|
|
|
{ |
122
|
|
|
if ($this->__validateOnSet) { |
123
|
|
|
if ($property = static::schema()->properties[$name]) { |
124
|
|
|
$property->out($column); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
$this->__arrayOfData[$name] = $column; |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public static function className() |
132
|
|
|
{ |
133
|
|
|
return get_called_class(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function validate() |
137
|
|
|
{ |
138
|
|
|
static::schema()->out($this); |
139
|
|
|
} |
140
|
|
|
} |