1 | <?php |
||
9 | class ResourceFactory |
||
10 | { |
||
11 | /** |
||
12 | * @param Explorer $explorer |
||
13 | * @param array $data |
||
14 | * @return HalResource|ResourceCollection |
||
15 | * @throws InvalidResourceException |
||
16 | */ |
||
17 | 5 | public static function create(Explorer $explorer, $data) |
|
18 | { |
||
19 | 5 | if (!is_array($data)) { |
|
20 | throw new InvalidResourceException('Given resource seems to be not valid. Resource is empty.', 1474016173); |
||
21 | } |
||
22 | 5 | if (ArrayUtils::isNumericArray($data)) { |
|
23 | 1 | return self::createCollection($explorer, $data); |
|
24 | } |
||
25 | 5 | return self::createResource($explorer, $data); |
|
26 | } |
||
27 | |||
28 | /** |
||
29 | * @param Explorer $explorer |
||
30 | * @param array $data |
||
31 | * @return ResourceCollection |
||
32 | */ |
||
33 | 1 | private static function createCollection(Explorer $explorer, array $data) |
|
34 | { |
||
35 | 1 | $collection = new ResourceCollection($explorer); |
|
36 | 1 | foreach ($data as $itemData) { |
|
37 | $collection->addResource(self::create($explorer, $itemData)); |
||
38 | 1 | } |
|
39 | 1 | return $collection; |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * @param Explorer $explorer |
||
44 | * @param array $data |
||
45 | * @return HalResource |
||
46 | */ |
||
47 | 5 | private static function createResource(Explorer $explorer, array $data) |
|
61 | |||
62 | /** |
||
63 | * @param Explorer $explorer |
||
64 | * @param EmbeddableInterface $resource |
||
65 | * @param array $embedded |
||
66 | */ |
||
67 | 5 | private static function parseEmbedded(Explorer $explorer, EmbeddableInterface $resource, array $embedded) |
|
68 | { |
||
69 | 5 | foreach ($embedded as $name => $embed) { |
|
70 | 1 | $resource->addEmbedded($name, self::create($explorer, $embed)); |
|
71 | 5 | } |
|
72 | 5 | } |
|
73 | |||
74 | /** |
||
75 | * @param Explorer $explorer |
||
76 | * @param HalResource $resource |
||
77 | * @param array $links |
||
78 | */ |
||
79 | 5 | private static function parseLinks(Explorer $explorer, HalResource $resource, array $links) |
|
85 | } |
||
86 |