1
|
|
|
<?php |
2
|
|
|
namespace Aeq\Hal\Explorer; |
3
|
|
|
|
4
|
|
|
use Aeq\Hal\Exception\AlreadyEmbeddedException; |
5
|
|
|
use Aeq\Hal\Explorer; |
6
|
|
|
|
7
|
|
|
class LinkCollection implements \Iterator, LinkInterface, EmbeddableInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Explorer |
11
|
|
|
*/ |
12
|
|
|
private $explorer; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $name; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Link[] |
21
|
|
|
*/ |
22
|
|
|
private $links = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var EmbeddableInterface |
26
|
|
|
*/ |
27
|
|
|
private $parent; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ResourceCollection |
31
|
|
|
*/ |
32
|
|
|
private $resources; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Explorer $explorer |
36
|
|
|
* @param string $name |
37
|
|
|
* @param EmbeddableInterface $parent |
38
|
|
|
*/ |
39
|
1 |
|
public function __construct(Explorer $explorer, $name, EmbeddableInterface $parent) |
40
|
|
|
{ |
41
|
1 |
|
$this->explorer = $explorer; |
42
|
1 |
|
$this->name = $name; |
43
|
1 |
|
$this->parent = $parent; |
44
|
1 |
|
$this->resources = new ResourceCollection($explorer); |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Link $link |
49
|
|
|
*/ |
50
|
1 |
|
public function addLink(Link $link) |
51
|
|
|
{ |
52
|
1 |
|
$this->links[] = $link; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return Link |
57
|
|
|
*/ |
58
|
|
|
public function current() |
59
|
|
|
{ |
60
|
|
|
return current($this->links); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function next() |
67
|
|
|
{ |
68
|
|
|
next($this->links); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function key() |
75
|
|
|
{ |
76
|
|
|
return key($this->links); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function valid() |
83
|
|
|
{ |
84
|
|
|
return key($this->links) !== null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function rewind() |
91
|
|
|
{ |
92
|
|
|
reset($this->links); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
2 |
|
public function getData() |
99
|
|
|
{ |
100
|
2 |
|
$data = []; |
101
|
2 |
|
foreach ($this->links as $link) { |
102
|
2 |
|
$data[] = $link->getData(); |
103
|
2 |
|
} |
104
|
2 |
|
return $data; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $name |
109
|
|
|
* @param \Aeq\Hal\Explorer\ResourceInterface $resource |
110
|
|
|
*/ |
111
|
1 |
|
public function addEmbedded($name, ResourceInterface $resource) |
112
|
|
|
{ |
113
|
1 |
|
$this->resources->addResource($resource); |
114
|
1 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $name |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
1 |
|
public function hasEmbedded($name) |
121
|
|
|
{ |
122
|
1 |
|
return $this->parent->hasEmbedded($name); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param array $variables |
127
|
|
|
* @param array $options |
128
|
|
|
* @return ResourceCollection |
129
|
|
|
* @throws AlreadyEmbeddedException |
130
|
|
|
*/ |
131
|
1 |
|
public function follow(array $variables = [], array $options = []) |
132
|
|
|
{ |
133
|
1 |
|
foreach ($this->links as $link) { |
134
|
1 |
|
$link->follow($variables, $options); |
135
|
1 |
|
} |
136
|
|
|
|
137
|
1 |
|
$this->parent->addEmbedded($this->name, $this->resources); |
138
|
|
|
|
139
|
1 |
|
return $this->resources; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|