danbelden /
php-siren
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Siren; |
||
| 4 | |||
| 5 | use Siren\Entity; |
||
| 6 | use Siren\Action; |
||
| 7 | use Siren\Link; |
||
| 8 | use LogicException; |
||
| 9 | |||
| 10 | class Document |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var array |
||
| 14 | */ |
||
| 15 | protected $class = array(); |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $properties = array(); |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var Entity[] |
||
| 24 | */ |
||
| 25 | protected $entities = array(); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var Action[] |
||
| 29 | */ |
||
| 30 | protected $actions = array(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var Link[] |
||
| 34 | */ |
||
| 35 | protected $links = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get class |
||
| 39 | * |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | public function getClass() |
||
| 43 | { |
||
| 44 | return $this->class; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Set class |
||
| 49 | * |
||
| 50 | * @param array $class |
||
| 51 | * @return $this |
||
| 52 | * @throws LogicException |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function setClass(array $class) |
|
| 55 | { |
||
| 56 | foreach ($class as $classItem) { |
||
| 57 | if (!is_string($classItem)) { |
||
| 58 | $exMsg = sprintf('Provided class `%s` is not a string', $classItem); |
||
| 59 | throw new LogicException($exMsg); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | $this->class = $class; |
||
| 64 | |||
| 65 | return $this; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Add class |
||
| 70 | * |
||
| 71 | * @param string $class |
||
| 72 | * @return $this |
||
| 73 | * @throws LogicException |
||
| 74 | */ |
||
| 75 | View Code Duplication | public function addClass($class) |
|
| 76 | { |
||
| 77 | if (!is_string($class)) { |
||
| 78 | $exMsg = sprintf('Provided class `%s` is not a string', $class); |
||
| 79 | throw new LogicException($exMsg); |
||
| 80 | } |
||
| 81 | |||
| 82 | $currentClass = $this->getClass(); |
||
| 83 | if (in_array($class, $currentClass)) { |
||
| 84 | $exMsg = sprintf('Provided class `%s` is already registered', $class); |
||
| 85 | throw new LogicException($exMsg); |
||
| 86 | } |
||
| 87 | |||
| 88 | $this->class[] = $class; |
||
| 89 | |||
| 90 | return $this; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get properties |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | public function getProperties() |
||
| 99 | { |
||
| 100 | return $this->properties; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Add property |
||
| 105 | * |
||
| 106 | * @param string $key |
||
| 107 | * @param mixed $value |
||
| 108 | * @return $this |
||
| 109 | */ |
||
| 110 | public function addProperty($key, $value) |
||
| 111 | { |
||
| 112 | $this->properties[$key] = $value; |
||
| 113 | |||
| 114 | return $this; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Set properties |
||
| 119 | * |
||
| 120 | * @param array $properties |
||
| 121 | * @return $this |
||
| 122 | * @throws LogicException |
||
| 123 | */ |
||
| 124 | public function setProperties(array $properties) |
||
| 125 | { |
||
| 126 | $this->properties = $properties; |
||
| 127 | |||
| 128 | return $this; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get entities |
||
| 133 | * |
||
| 134 | * @return Entity[] |
||
| 135 | */ |
||
| 136 | public function getEntities() |
||
| 137 | { |
||
| 138 | return $this->entities; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Set entities |
||
| 143 | * |
||
| 144 | * @param Entity[] $entities |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | public function setEntities(array $entities) |
||
| 148 | { |
||
| 149 | foreach ($entities as $key => $entity) { |
||
| 150 | if (!($entity instanceof Entity)) { |
||
| 151 | $exMsg = sprintf( |
||
| 152 | 'Array key `%s` is not an instance of Entity', |
||
| 153 | $key |
||
| 154 | ); |
||
| 155 | throw new LogicException($exMsg); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | $this->entities = $entities; |
||
| 160 | |||
| 161 | return $this; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Add entity |
||
| 166 | * |
||
| 167 | * @param Entity $entity |
||
| 168 | * @return $this |
||
| 169 | */ |
||
| 170 | public function addEntity(Entity $entity) |
||
| 171 | { |
||
| 172 | $this->entities[] = $entity; |
||
| 173 | |||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get actions |
||
| 179 | * |
||
| 180 | * @return Action[] |
||
| 181 | */ |
||
| 182 | public function getActions() |
||
| 183 | { |
||
| 184 | return $this->actions; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Set actions |
||
| 189 | * |
||
| 190 | * @param Action[] $actions |
||
| 191 | * @return $this |
||
| 192 | * @throws LogicException |
||
| 193 | */ |
||
| 194 | public function setActions(array $actions) |
||
| 195 | { |
||
| 196 | foreach ($actions as $key => $action) { |
||
| 197 | if (!($action instanceof Action)) { |
||
| 198 | $exMsg = sprintf( |
||
| 199 | 'Array key `%s` is not an instance of Action', |
||
| 200 | $key |
||
| 201 | ); |
||
| 202 | throw new LogicException($exMsg); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | $this->actions = $actions; |
||
| 207 | |||
| 208 | return $this; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Add action |
||
| 213 | * |
||
| 214 | * @param Action $action |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | public function addAction(Action $action) |
||
| 218 | { |
||
| 219 | $this->actions[] = $action; |
||
| 220 | |||
| 221 | return $this; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get links |
||
| 226 | * |
||
| 227 | * @return Link[] |
||
| 228 | */ |
||
| 229 | public function getLinks() |
||
| 230 | { |
||
| 231 | return $this->links; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Set links |
||
| 236 | * |
||
| 237 | * @param Link[] $links |
||
| 238 | * @return $this |
||
| 239 | * @throws LogicException |
||
| 240 | */ |
||
| 241 | View Code Duplication | public function setLinks(array $links) |
|
| 242 | { |
||
| 243 | foreach ($links as $key => $link) { |
||
| 244 | if (!($link instanceof Link)) { |
||
| 245 | $exMsg = sprintf( |
||
| 246 | 'Array key `%s` is not an instance of Link', |
||
| 247 | $key |
||
| 248 | ); |
||
| 249 | throw new LogicException($exMsg); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | $this->links = $links; |
||
| 254 | |||
| 255 | return $this; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Add link |
||
| 260 | * |
||
| 261 | * @param Link $link |
||
| 262 | * @return $this |
||
| 263 | */ |
||
| 264 | public function addLink(Link $link) |
||
| 265 | { |
||
| 266 | $this->links[] = $link; |
||
| 267 | |||
| 268 | return $this; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Convert object to array |
||
| 273 | * |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | public function toArray() |
||
| 277 | { |
||
| 278 | $data = array(); |
||
| 279 | |||
| 280 | $class = $this->getClass(); |
||
| 281 | if (!empty($class)) { |
||
| 282 | $data['class'] = $class; |
||
| 283 | } |
||
| 284 | |||
| 285 | $properties = $this->getProperties(); |
||
| 286 | if (!empty($properties)) { |
||
| 287 | $data['properties'] = $properties; |
||
| 288 | } |
||
| 289 | |||
| 290 | $entities = $this->getEntities(); |
||
| 291 | if (!empty($entities)) { |
||
| 292 | $data['entities'] = array(); |
||
| 293 | foreach ($entities as $entity) { |
||
| 294 | $data['entities'][] = $entity->toArray(); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | $actions = $this->getActions(); |
||
| 299 | if (!empty($actions)) { |
||
| 300 | $data['actions'] = array(); |
||
| 301 | foreach ($actions as $action) { |
||
| 302 | $data['actions'][] = $action->toArray(); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | $links = $this->getLinks(); |
||
| 307 | View Code Duplication | if (!empty($links)) { |
|
| 308 | $data['links'] = array(); |
||
| 309 | foreach ($links as $link) { |
||
| 310 | $data['links'][] = $link->toArray(); |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | return $data; |
||
| 315 | } |
||
| 316 | } |
||
| 317 |