-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathHalResource.php
181 lines (157 loc) · 3.73 KB
/
HalResource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace ShoppingFeed\Sdk\Hal;
class HalResource
{
/**
* @var array
*/
private $properties = [];
/**
* @var array
*/
private $links = [];
/**
* @var array
*/
private $embedded = [];
/**
* @param HalClient $client
* @param array $data
*
* @return static
*/
public static function fromArray(HalClient $client, array $data)
{
$links = [];
$embedded = [];
if (isset($data['_links'])) {
$links = $data['_links'];
unset($data['_links']);
}
if (isset($data['_embedded'])) {
$embedded = $data['_embedded'];
unset($data['_embedded']);
}
return new static($client, $data, $links, $embedded);
}
/**
* @param HalClient $client
* @param array $properties
* @param array $links
* @param array $embedded
*/
public function __construct(HalClient $client, array $properties = [], array $links = [], array $embedded = [])
{
$this->properties = $properties;
$this->createLinks($client, $links);
$this->createEmbedded($client, $embedded);
}
/**
* @param string $name
*
* @return bool
*/
public function hasProperty($name)
{
return array_key_exists($name, $this->properties);
}
/**
* @param $name
* @param null $default
*
* @return mixed|null
*/
public function getProperty($name, $default = null)
{
if (isset($this->properties[$name])) {
return $this->properties[$name];
}
return $default;
}
/**
* @return array
*/
public function getProperties()
{
return $this->properties;
}
/**
* @param string $rel The resource identifier
*
* @return static[]
*/
public function getResources($rel)
{
if (isset($this->embedded[$rel])) {
return $this->embedded[$rel];
}
return [];
}
/**
* @return static[][]
*/
public function getAllResources()
{
return $this->embedded;
}
/**
* @param string $rel The resource identifier
*
* @return static|null
*/
public function getFirstResource($rel)
{
$resources = $this->getResources($rel);
if ($resources) {
return $resources[0];
}
return null;
}
/**
* @param string $rel The resource identifier
*
* @return HalLink|null
*/
public function getLink($rel)
{
if (isset($this->links[$rel])) {
return $this->links[$rel];
}
return null;
}
/**
* @param array $options
*
* @return null|HalResource
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function get(array $options = [])
{
return $this->getLink('self')->get([], $options);
}
/**
* @param array $links
* @param HalClient $client
*/
private function createLinks(HalClient $client, array $links)
{
foreach ($links as $rel => $link) {
$this->links[$rel] = new HalLink($client, $link['href'], $link);
}
}
/**
* @param HalClient $client
* @param array $relations
*/
private function createEmbedded(HalClient $client, array $relations)
{
foreach ($relations as $name => $resources) {
if (array_values($resources) !== $resources) {
$resources = [$resources];
}
foreach ($resources as $index => $resource) {
$this->embedded[$name][$index] = static::fromArray($client, $resource);
}
}
}
}