|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Document\Behaviour; |
|
5
|
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Document\Exception\LinkNotFoundException; |
|
7
|
|
|
use Mikemirten\Component\JsonApi\Document\Exception\LinkOverrideException; |
|
8
|
|
|
use Mikemirten\Component\JsonApi\Document\LinkObject; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Links-container behaviour |
|
12
|
|
|
* |
|
13
|
|
|
* @see http://jsonapi.org/format/#document-links |
|
14
|
|
|
* |
|
15
|
|
|
* @package Mikemirten\Component\JsonApi\Document\Behaviour |
|
16
|
|
|
*/ |
|
17
|
|
|
trait LinksContainer |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Links |
|
21
|
|
|
* |
|
22
|
|
|
* @var LinkObject[] |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $links = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Set link |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $name |
|
30
|
|
|
* @param LinkObject $link |
|
31
|
|
|
* @param LinkOverrideException |
|
32
|
|
|
*/ |
|
33
|
39 |
|
public function setLink(string $name, LinkObject $link) |
|
34
|
|
|
{ |
|
35
|
39 |
|
if (isset($this->links[$name])) { |
|
36
|
9 |
|
throw new LinkOverrideException($this, $name); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
39 |
|
$this->links[$name] = $link; |
|
40
|
39 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Has link |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $name |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
20 |
|
public function hasLink(string $name): bool |
|
49
|
|
|
{ |
|
50
|
20 |
|
return isset($this->links[$name]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get link |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $name |
|
57
|
|
|
* @return LinkObject |
|
58
|
|
|
* @throws LinkNotFoundException |
|
59
|
|
|
*/ |
|
60
|
19 |
|
public function getLink(string $name): LinkObject |
|
61
|
|
|
{ |
|
62
|
19 |
|
if (isset($this->links[$name])) { |
|
63
|
10 |
|
return $this->links[$name]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
9 |
|
throw new LinkNotFoundException($this, $name); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get all links |
|
71
|
|
|
* |
|
72
|
|
|
* @return LinkObject[] |
|
73
|
|
|
*/ |
|
74
|
10 |
|
public function getLinks(): array |
|
75
|
|
|
{ |
|
76
|
10 |
|
return $this->links; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Contains any links ? |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
11 |
|
public function hasLinks(): bool |
|
85
|
|
|
{ |
|
86
|
11 |
|
return count($this->links) > 0; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Cast links to an array |
|
91
|
|
|
* |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
38 |
|
protected function linksToArray(): array |
|
95
|
|
|
{ |
|
96
|
38 |
|
$links = []; |
|
97
|
|
|
|
|
98
|
38 |
|
foreach ($this->links as $name => $link) |
|
99
|
|
|
{ |
|
100
|
10 |
|
if (! $link->hasMetadata()) { |
|
101
|
4 |
|
$links[$name] = $link->getReference(); |
|
102
|
4 |
|
continue; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
6 |
|
$links[$name] = [ |
|
106
|
6 |
|
'href' => $link->getReference(), |
|
107
|
6 |
|
'meta' => $link->getMetadata() |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
38 |
|
return $links; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Remove link |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $name |
|
118
|
|
|
* @return mixed |
|
119
|
|
|
*/ |
|
120
|
10 |
|
public function removeLink(string $name) |
|
121
|
|
|
{ |
|
122
|
10 |
|
unset($this->links[$name]); |
|
123
|
|
|
} |
|
124
|
|
|
} |