1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonSchema\Structure; |
4
|
|
|
|
5
|
|
|
use Swaggest\JsonSchema\Constraint\Properties; |
6
|
|
|
use Swaggest\JsonSchema\NameMirror; |
7
|
|
|
use Swaggest\JsonSchema\ProcessingOptions; |
8
|
|
|
use Swaggest\JsonSchema\Schema; |
9
|
|
|
|
10
|
|
|
abstract class SchemaStructure extends Schema 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
|
|
|
$properties = new Properties(); |
24
|
|
|
$schema->properties = $properties; |
25
|
|
|
$schema->objectItemClass = get_called_class(); |
26
|
|
|
static::setUpProperties($properties, $schema); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return $schema; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param $data |
34
|
|
|
* @param ProcessingOptions $options |
35
|
|
|
* @return static |
36
|
|
|
*/ |
37
|
|
|
public static function importToSchema($data, ProcessingOptions $options = null) |
38
|
|
|
{ |
39
|
|
|
return static::schema()->import($data, $options); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $data |
44
|
|
|
* @param ProcessingOptions $options |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
|
public static function exportFromSchema($data, ProcessingOptions $options = null) |
48
|
|
|
{ |
49
|
|
|
return static::schema()->export($data, $options); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return static |
55
|
|
|
*/ |
56
|
|
|
public static function names() |
57
|
|
|
{ |
58
|
|
|
static $nameflector = null; |
59
|
|
|
if (null === $nameflector) { |
60
|
|
|
$nameflector = new NameMirror(); |
61
|
|
|
} |
62
|
|
|
return $nameflector; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function jsonSerialize() |
66
|
|
|
{ |
67
|
|
|
$result = new \stdClass(); |
68
|
|
|
$properties = static::schema()->properties; |
69
|
|
|
foreach ($properties->toArray() as $name => $schema) { |
70
|
|
|
$value = $this->$name; |
71
|
|
|
if ((null !== $value) || array_key_exists($name, $this->__arrayOfData)) { |
72
|
|
|
$result->$name = $value; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
foreach ($properties->nestedPropertyNames as $name) { |
76
|
|
|
/** @var ObjectItem $nested */ |
77
|
|
|
$nested = $this->$name; |
78
|
|
|
if (null !== $nested) { |
79
|
|
|
foreach ((array)$nested->jsonSerialize() as $key => $value) { |
80
|
|
|
$result->$key = $value; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |