1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Mapper\Definition; |
5
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Behaviour\LinksAwareInterface; |
7
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Behaviour\LinksContainer; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Mapping Definition |
11
|
|
|
* |
12
|
|
|
* @package Mikemirten\Component\JsonApi\Mapper\Definition |
13
|
|
|
*/ |
14
|
|
|
class Definition implements LinksAwareInterface |
15
|
|
|
{ |
16
|
|
|
use LinksContainer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class covered by definition |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $class; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Resource type |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $type; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Attributes |
34
|
|
|
* |
35
|
|
|
* @var Attribute[] |
36
|
|
|
*/ |
37
|
|
|
protected $attributes = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Relationships |
41
|
|
|
* |
42
|
|
|
* @var Relationship[] |
43
|
|
|
*/ |
44
|
|
|
protected $relationships = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Definition constructor. |
48
|
|
|
* |
49
|
|
|
* @param string $class |
50
|
|
|
*/ |
51
|
25 |
|
public function __construct(string $class) |
52
|
|
|
{ |
53
|
25 |
|
$this->class = $class; |
54
|
25 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get class covered by definition |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
3 |
|
public function getClass(): string |
62
|
|
|
{ |
63
|
3 |
|
return $this->class; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set type of resource |
68
|
|
|
* |
69
|
|
|
* @param string $type |
70
|
|
|
*/ |
71
|
8 |
|
public function setType(string $type) |
72
|
|
|
{ |
73
|
8 |
|
$this->type = $type; |
74
|
8 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Has type of resource defined ? |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
5 |
|
public function hasType(): bool |
82
|
|
|
{ |
83
|
5 |
|
return $this->type !== null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get type of resource |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
5 |
|
public function getType(): string |
92
|
|
|
{ |
93
|
5 |
|
return $this->type; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Add attribute |
98
|
|
|
* |
99
|
|
|
* @param Attribute $attribute |
100
|
|
|
* @throws \LogicException |
101
|
|
|
*/ |
102
|
1 |
|
public function addAttribute(Attribute $attribute) |
103
|
|
|
{ |
104
|
1 |
|
$name = $attribute->getName(); |
105
|
|
|
|
106
|
1 |
|
if (isset($this->attributes[$name])) { |
107
|
|
|
throw new \LogicException(sprintf('Attribute "%s" already defined.', $name)); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$this->attributes[$name] = $attribute; |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get attributes |
115
|
|
|
* [name => attribute] |
116
|
|
|
* |
117
|
|
|
* @return Attribute[] |
118
|
|
|
*/ |
119
|
3 |
|
public function getAttributes(): array |
120
|
|
|
{ |
121
|
3 |
|
return $this->attributes; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Add relationship |
126
|
|
|
* |
127
|
|
|
* @param Relationship $relationship |
128
|
|
|
* @throws \LogicException |
129
|
|
|
*/ |
130
|
12 |
|
public function addRelationship(Relationship $relationship) |
131
|
|
|
{ |
132
|
12 |
|
$name = $relationship->getName(); |
133
|
|
|
|
134
|
12 |
|
if (isset($this->relationships[$name])) { |
135
|
1 |
|
throw new \LogicException(sprintf('Relationship "%s" already defined.', $name)); |
136
|
|
|
} |
137
|
|
|
|
138
|
12 |
|
$this->relationships[$name] = $relationship; |
139
|
12 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get relationships |
143
|
|
|
* [name => relationship] |
144
|
|
|
* |
145
|
|
|
* @return Relationship[] |
146
|
|
|
*/ |
147
|
10 |
|
public function getRelationships(): array |
148
|
|
|
{ |
149
|
10 |
|
return $this->relationships; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Merge a definition into this one |
154
|
|
|
* |
155
|
|
|
* @param self $definition |
156
|
|
|
*/ |
157
|
5 |
|
public function merge(self $definition) |
158
|
|
|
{ |
159
|
5 |
|
if ($this->type === null && $definition->hasType()) { |
160
|
1 |
|
$this->type = $definition->getType(); |
161
|
|
|
} |
162
|
|
|
|
163
|
5 |
|
$this->mergeLinks($definition); |
164
|
5 |
|
$this->mergeAttributes($definition); |
165
|
5 |
|
$this->mergeRelationships($definition); |
166
|
5 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Merge attributes |
170
|
|
|
* |
171
|
|
|
* @param Definition $definition |
172
|
|
|
*/ |
173
|
5 |
|
protected function mergeAttributes(self $definition) |
174
|
|
|
{ |
175
|
5 |
|
foreach ($definition->getAttributes() as $name => $attribute) |
176
|
|
|
{ |
177
|
1 |
|
if (isset($this->attributes[$name])) { |
178
|
1 |
|
$this->attributes[$name]->merge($attribute); |
179
|
1 |
|
continue; |
180
|
|
|
} |
181
|
|
|
|
182
|
1 |
|
$this->attributes[$name] = $attribute; |
183
|
|
|
} |
184
|
5 |
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Merge relationships |
188
|
|
|
* |
189
|
|
|
* @param Definition $definition |
190
|
|
|
*/ |
191
|
5 |
|
protected function mergeRelationships(self $definition) |
192
|
|
|
{ |
193
|
5 |
|
foreach ($definition->getRelationships() as $name => $relationship) |
194
|
|
|
{ |
195
|
1 |
|
if (isset($this->relationships[$name])) { |
196
|
1 |
|
$this->relationships[$name]->merge($relationship); |
197
|
1 |
|
continue; |
198
|
|
|
} |
199
|
|
|
|
200
|
1 |
|
$this->relationships[$name] = $relationship; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |