1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17
18 namespace OpenCloud\Image\Resource\Schema;
19
20 use OpenCloud\Image\Enum\OperationType;
21 use OpenCloud\Image\Enum\Schema as SchemaEnum;
22
23 24 25 26 27
28 class Schema extends AbstractSchemaItem
29 {
30
31 protected $name;
32
33
34 protected $properties;
35
36 37 38 39 40
41 protected $additionalProperties;
42
43
44 protected $links;
45
46 47 48 49
50 public static function factory(array $data)
51 {
52 $schema = new self();
53
54 $schema->setName(self::stockProperty($data, SchemaEnum::NAME));
55
56 if (isset($data[SchemaEnum::LINKS])) {
57 $schema->setLinks($data[SchemaEnum::LINKS]);
58 }
59
60 if (isset($data[SchemaEnum::PROPERTIES])) {
61 $schema->setProperties($data[SchemaEnum::PROPERTIES]);
62 }
63
64 if (isset($data[SchemaEnum::ADDITIONAL_PROPERTIES])) {
65 $schema->setAdditionalProperties($data[SchemaEnum::ADDITIONAL_PROPERTIES]);
66 }
67
68 return $schema;
69 }
70
71 72 73
74 public function setName($name)
75 {
76 $this->name = $name;
77 }
78
79 80 81
82 public function getName()
83 {
84 return $this->name;
85 }
86
87 88 89
90 public function setProperties(array $properties)
91 {
92 foreach ($properties as $name => $array) {
93 $array[SchemaEnum::NAME] = $name;
94 $this->properties[$name] = Property::factory($array);
95 }
96 }
97
98 99 100
101 public function getProperties()
102 {
103 return $this->properties;
104 }
105
106 107 108
109 public function setAdditionalProperties(array $properties)
110 {
111 $this->additionalProperties = $properties;
112 }
113
114 115 116
117 public function getAdditionalProperties()
118 {
119 if (!empty($this->additionalProperties)) {
120 return Property::factory($this->additionalProperties);
121 }
122
123 return false;
124 }
125
126 127 128
129 public function setLinks(array $links)
130 {
131 $this->links = $links;
132 }
133
134 135 136
137 public function getLinks()
138 {
139 return $this->links;
140 }
141
142 143 144 145 146 147
148 public function propertyExists($property)
149 {
150 return isset($this->properties[$property]);
151 }
152
153 154 155 156 157 158
159 public function getProperty($property)
160 {
161 return $this->propertyExists($property) ? $this->properties[$property] : null;
162 }
163
164 165 166 167 168 169
170 public function decideOperationType(Property $property)
171 {
172 $name = $property->getName();
173
174 return ($this->propertyExists($name)) ? OperationType::REPLACE : OperationType::ADD;
175 }
176
177 178 179 180 181 182
183 public function validateAdditionalProperty($value)
184 {
185 if ($property = $this->getAdditionalProperties()) {
186 $property->setValue($value);
187
188 return ($property->validate() === true) ? $property : false;
189 }
190
191 return false;
192 }
193 }
194