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