|
1
|
|
|
<?php |
|
2
|
|
|
namespace Aeq\Hal\Explorer; |
|
3
|
|
|
|
|
4
|
|
|
use Aeq\Hal\Exception\InvalidResourceException; |
|
5
|
|
|
use Aeq\Hal\Explorer; |
|
6
|
|
|
use Aeq\Hal\Explorer\Resource as HalResource; |
|
7
|
|
|
use Aeq\Hal\Utils\ArrayUtils; |
|
8
|
|
|
|
|
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) |
|
48
|
|
|
{ |
|
49
|
5 |
|
$links = isset($data['_links']) ? $data['_links'] : []; |
|
50
|
5 |
|
$embedded = isset($data['_embedded']) ? $data['_embedded'] : []; |
|
51
|
|
|
|
|
52
|
5 |
|
unset($data['_links'], $data['_embedded']); |
|
53
|
|
|
|
|
54
|
5 |
|
$resource = new Resource($explorer, $data); |
|
55
|
|
|
|
|
56
|
5 |
|
self::parseEmbedded($explorer, $resource, $embedded); |
|
57
|
5 |
|
self::parseLinks($explorer, $resource, $links); |
|
58
|
|
|
|
|
59
|
5 |
|
return $resource; |
|
60
|
|
|
} |
|
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) |
|
80
|
|
|
{ |
|
81
|
5 |
|
foreach ($links as $name => $link) { |
|
82
|
5 |
|
$resource->addLink($name, LinkFactory::create($explorer, $name, $link, $resource)); |
|
83
|
5 |
|
} |
|
84
|
5 |
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|