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