1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonSchema\Constraint; |
4
|
|
|
|
5
|
|
|
use Swaggest\JsonSchema\Exception; |
6
|
|
|
use Swaggest\JsonSchema\Schema; |
7
|
|
|
use Swaggest\JsonSchema\SchemaContract; |
8
|
|
|
use Swaggest\JsonSchema\Structure\Egg; |
9
|
|
|
use Swaggest\JsonSchema\Structure\Nested; |
10
|
|
|
use Swaggest\JsonSchema\Structure\ObjectItem; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @method SchemaContract __get($key) |
14
|
|
|
* @method Schema[] toArray() |
15
|
|
|
*/ |
16
|
|
|
class Properties extends ObjectItem implements Constraint |
17
|
|
|
{ |
18
|
|
|
private $__isReadOnly = false; |
19
|
|
|
|
20
|
|
|
/** @var Schema[] */ |
21
|
|
|
protected $__arrayOfData = array(); |
22
|
|
|
|
23
|
|
|
/** @var Schema */ |
24
|
|
|
protected $__schema; |
25
|
|
|
|
26
|
7 |
|
public function lock() |
27
|
|
|
{ |
28
|
7 |
|
$this->__isReadOnly = true; |
29
|
7 |
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $name |
34
|
|
|
* @param mixed $column |
35
|
|
|
* @return $this|static |
36
|
|
|
* @throws Exception |
37
|
|
|
*/ |
38
|
15 |
|
public function __set($name, $column) |
39
|
|
|
{ |
40
|
15 |
|
if ($this->__isReadOnly) { |
41
|
|
|
throw new Exception('Trying to modify read-only Properties'); |
42
|
|
|
} |
43
|
15 |
|
if ($column instanceof Nested) { |
44
|
4 |
|
$this->addNested($column->schema, $name); |
45
|
4 |
|
return $this; |
46
|
|
|
} |
47
|
13 |
|
parent::__set($name, $column); |
48
|
13 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
public static function create() |
52
|
|
|
{ |
53
|
3 |
|
return new static; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** @var Schema|null */ |
57
|
|
|
private $additionalProperties; |
|
|
|
|
58
|
|
|
|
59
|
|
|
/** @var Egg[][] */ |
60
|
|
|
public $nestedProperties = array(); |
61
|
|
|
|
62
|
|
|
/** @var string[] */ |
63
|
|
|
public $nestedPropertyNames = array(); |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param SchemaContract $nested |
67
|
|
|
* @param string $name |
68
|
|
|
* @return $this |
69
|
|
|
* @throws Exception |
70
|
|
|
*/ |
71
|
4 |
|
protected function addNested(SchemaContract $nested, $name) |
72
|
|
|
{ |
73
|
4 |
|
if ($this->__isReadOnly) { |
74
|
|
|
throw new Exception('Trying to modify read-only Properties'); |
75
|
|
|
} |
76
|
4 |
|
if (null === $nested->getProperties()) { |
77
|
|
|
throw new Exception('Schema with properties required', Exception::PROPERTIES_REQUIRED); |
78
|
|
|
} |
79
|
4 |
|
$this->nestedPropertyNames[$name] = $name; |
80
|
4 |
|
foreach ($nested->getProperties()->toArray() as $propertyName => $property) { |
81
|
4 |
|
$this->nestedProperties[$propertyName][] = new Egg($nested, $name, $property); |
82
|
|
|
} |
83
|
4 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return Egg[][] |
88
|
|
|
*/ |
89
|
|
|
public function getNestedProperties() |
90
|
|
|
{ |
91
|
|
|
return $this->nestedProperties; |
92
|
|
|
} |
93
|
|
|
|
94
|
7 |
|
public function isEmpty() |
95
|
|
|
{ |
96
|
7 |
|
return (count($this->__arrayOfData) + count($this->nestedProperties)) === 0; |
97
|
|
|
} |
98
|
|
|
} |