1
|
|
|
<?php |
2
|
|
|
namespace Aeq\Hal\Explorer; |
3
|
|
|
|
4
|
|
|
use Aeq\Hal\Exception\NotFoundException; |
5
|
|
|
use Aeq\Hal\Explorer; |
6
|
|
|
|
7
|
|
|
class Resource implements ResourceInterface, EmbeddableInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Explorer |
11
|
|
|
*/ |
12
|
|
|
private $explorer; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $properties; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var LinkInterface[] |
21
|
|
|
*/ |
22
|
|
|
private $links = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ResourceInterface[] |
26
|
|
|
*/ |
27
|
|
|
private $embedded = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Explorer $explorer |
31
|
|
|
* @param array $properties |
32
|
|
|
*/ |
33
|
5 |
|
public function __construct(Explorer $explorer, array $properties) |
34
|
|
|
{ |
35
|
5 |
|
$this->explorer = $explorer; |
36
|
5 |
|
$this->properties = $properties; |
37
|
5 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $name |
41
|
|
|
* @return LinkInterface |
42
|
|
|
* @throws NotFoundException |
43
|
|
|
*/ |
44
|
4 |
|
public function getLink($name) |
45
|
|
|
{ |
46
|
4 |
|
if (!$this->hasLink($name)) { |
47
|
|
|
throw new NotFoundException(sprintf('link "%s" not found', $name), 1460663568); |
48
|
|
|
} |
49
|
4 |
|
return $this->links[$name]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $name |
54
|
|
|
* @param LinkInterface $link |
55
|
|
|
*/ |
56
|
5 |
|
public function addLink($name, LinkInterface $link) |
57
|
|
|
{ |
58
|
5 |
|
$this->links[$name] = $link; |
59
|
5 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $name |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
4 |
|
public function hasLink($name) |
66
|
|
|
{ |
67
|
4 |
|
return isset($this->links[$name]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $name |
72
|
|
|
* @param ResourceInterface $resource |
73
|
|
|
*/ |
74
|
5 |
|
public function addEmbedded($name, ResourceInterface $resource) |
75
|
|
|
{ |
76
|
5 |
|
$this->embedded[$name] = $resource; |
77
|
5 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $name |
81
|
|
|
* @return ResourceInterface |
82
|
|
|
* @throws NotFoundException |
83
|
|
|
*/ |
84
|
2 |
|
public function getEmbedded($name) |
85
|
|
|
{ |
86
|
2 |
|
if (!$this->hasEmbedded($name)) { |
87
|
|
|
throw new NotFoundException(sprintf('embedded resource "%s" not found', $name), 1460663416); |
88
|
|
|
} |
89
|
2 |
|
return $this->embedded[$name]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $name |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
5 |
|
public function hasEmbedded($name) |
97
|
|
|
{ |
98
|
5 |
|
return isset($this->embedded[$name]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
5 |
|
public function getData() |
105
|
|
|
{ |
106
|
5 |
|
$data = []; |
107
|
5 |
|
foreach ($this->embedded as $name => $resource) { |
108
|
2 |
|
$data['_embedded'][$name] = $resource->getData(); |
109
|
5 |
|
} |
110
|
5 |
|
foreach ($this->links as $name => $link) { |
111
|
5 |
|
$data['_links'][$name] = $link->getData(); |
112
|
5 |
|
} |
113
|
5 |
|
return array_merge($this->properties, $data); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|