|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Document\Behaviour; |
|
5
|
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Document\AbstractRelationship; |
|
7
|
|
|
use Mikemirten\Component\JsonApi\Document\Exception\RelationshipNotFoundException; |
|
8
|
|
|
use Mikemirten\Component\JsonApi\Document\Exception\RelationshipOverrideException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Relationships-container behaviour |
|
12
|
|
|
* |
|
13
|
|
|
* @see http://jsonapi.org/format/#document-resource-object-relationships |
|
14
|
|
|
* |
|
15
|
|
|
* @package Mikemirten\Component\JsonApi\Document\Behaviour |
|
16
|
|
|
*/ |
|
17
|
|
|
trait RelationshipsContainer |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Relationships |
|
21
|
|
|
* |
|
22
|
|
|
* @var AbstractRelationship[] |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $relationships = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Set relationship |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $name |
|
30
|
|
|
* @param AbstractRelationship $relationship |
|
31
|
|
|
*/ |
|
32
|
4 |
|
public function setRelationship(string $name, AbstractRelationship $relationship) |
|
33
|
|
|
{ |
|
34
|
4 |
|
if (isset($this->relationships[$name])) { |
|
35
|
1 |
|
throw new RelationshipOverrideException($this, $name); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
4 |
|
$this->relationships[$name] = $relationship; |
|
39
|
4 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Has relationship |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $name |
|
45
|
|
|
* @return bool |
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function hasRelationship(string $name): bool |
|
48
|
|
|
{ |
|
49
|
2 |
|
return isset($this->relationships[$name]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get relationship |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $name |
|
56
|
|
|
* @return AbstractRelationship |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function getRelationship(string $name): AbstractRelationship |
|
59
|
|
|
{ |
|
60
|
2 |
|
if (isset($this->relationships[$name])) { |
|
61
|
1 |
|
return $this->relationships[$name]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
throw new RelationshipNotFoundException($this, $name); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Contains any relationships ? |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public function hasRelationships(): bool |
|
73
|
|
|
{ |
|
74
|
|
|
return count($this->relationships) > 0; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get relationships |
|
79
|
|
|
* |
|
80
|
|
|
* @return AbstractRelationship[] |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function getRelationships(): array |
|
83
|
|
|
{ |
|
84
|
1 |
|
return $this->relationships; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Remove relationship |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $name |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function removeRelationship(string $name) |
|
93
|
|
|
{ |
|
94
|
1 |
|
unset($this->relationships[$name]); |
|
95
|
1 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Cast relationships to an array |
|
99
|
|
|
* |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
5 |
|
protected function relationshipsToArray(): array |
|
103
|
|
|
{ |
|
104
|
5 |
|
$relationships = []; |
|
105
|
|
|
|
|
106
|
5 |
|
foreach ($this->relationships as $name => $relationship) |
|
107
|
|
|
{ |
|
108
|
1 |
|
$relationships[$name] = $relationship->toArray(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
5 |
|
return $relationships; |
|
112
|
|
|
} |
|
113
|
|
|
} |