|
1
|
|
|
<?php |
|
2
|
|
|
namespace Aeq\Hal\Explorer; |
|
3
|
|
|
|
|
4
|
|
|
use Aeq\Hal\Explorer; |
|
5
|
|
|
use Aeq\Hal\Explorer\Resource as HalResource; |
|
6
|
|
|
use Aeq\Hal\Exception\AlreadyEmbeddedException; |
|
7
|
|
|
use QL\UriTemplate\UriTemplate; |
|
8
|
|
|
|
|
9
|
|
|
class Link implements LinkInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var Explorer |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $explorer; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $data; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var EmbeddableInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $parent; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $name; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Explorer $explorer |
|
33
|
|
|
* @param string $name |
|
34
|
|
|
* @param array $data |
|
35
|
|
|
* @param EmbeddableInterface $parent |
|
36
|
|
|
*/ |
|
37
|
5 |
|
public function __construct(Explorer $explorer, $name, array $data, EmbeddableInterface $parent) |
|
38
|
1 |
|
{ |
|
39
|
5 |
|
$this->explorer = $explorer; |
|
40
|
5 |
|
$this->data = $data; |
|
41
|
5 |
|
$this->name = $name; |
|
42
|
5 |
|
$this->parent = $parent; |
|
43
|
5 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param array $variables |
|
47
|
|
|
* @param array $options |
|
48
|
|
|
* @return HalResource|ResourceCollection |
|
49
|
|
|
* @throws AlreadyEmbeddedException |
|
50
|
|
|
*/ |
|
51
|
4 |
|
public function follow(array $variables = [], array $options = []) |
|
52
|
|
|
{ |
|
53
|
4 |
|
if ($this->parent->hasEmbedded($this->name)) { |
|
54
|
|
|
throw new AlreadyEmbeddedException( |
|
55
|
|
|
sprintf('A link "%s" is already embedded. Try getting the embedded resource and reload it.'), |
|
56
|
|
|
1460663670 |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
$resource = $this->request($variables, $options); |
|
61
|
4 |
|
$this->parent->addEmbedded($this->name, $resource); |
|
62
|
4 |
|
return $resource; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
5 |
|
public function getData() |
|
69
|
|
|
{ |
|
70
|
5 |
|
return $this->data; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param array $variables |
|
75
|
|
|
* @param array $options |
|
76
|
|
|
* @return HalResource|ResourceCollection |
|
77
|
|
|
*/ |
|
78
|
4 |
|
private function request(array $variables = [], array $options = []) |
|
79
|
|
|
{ |
|
80
|
4 |
|
$response = $this->explorer->request('GET', $this->getUri($variables), $options); |
|
81
|
4 |
|
$resource = $this->explorer->explore($response); |
|
82
|
4 |
|
return $resource; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param array $variables |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
4 |
|
private function getUri(array $variables) |
|
90
|
|
|
{ |
|
91
|
4 |
|
if (isset($this->data['templated']) && true === $this->data['templated']) { |
|
92
|
1 |
|
$uri = new UriTemplate($this->data['href']); |
|
93
|
1 |
|
return $uri->expand($variables); |
|
94
|
|
|
} |
|
95
|
3 |
|
return $this->data['href']; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|