1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the reva2/jsonapi. |
4
|
|
|
* |
5
|
|
|
* (c) Sergey Revenko <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Reva2\JsonApi\Decoders\Mapping; |
13
|
|
|
|
14
|
|
|
use Reva2\JsonApi\Contracts\Decoders\Mapping\PropertyMetadataInterface; |
15
|
|
|
use Reva2\JsonApi\Contracts\Decoders\Mapping\ResourceMetadataInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* JSON API resource metadata |
19
|
|
|
* |
20
|
|
|
* @package Reva2\JsonApi\Decoders\Mapping |
21
|
|
|
* @author Sergey Revenko <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ResourceMetadata extends ClassMetadata implements ResourceMetadataInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
* @internal |
28
|
|
|
*/ |
29
|
|
|
public $name; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
* @internal |
34
|
|
|
*/ |
35
|
|
|
public $loader; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var PropertyMetadataInterface |
39
|
|
|
*/ |
40
|
|
|
public $idMetadata; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var PropertyMetadataInterface[] |
44
|
|
|
* @internal |
45
|
|
|
*/ |
46
|
|
|
public $attributes = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var PropertyMetadataInterface[] |
50
|
|
|
* @internal |
51
|
5 |
|
*/ |
52
|
|
|
public $relationships = []; |
53
|
5 |
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
|
|
public function getName() |
58
|
|
|
{ |
59
|
|
|
return $this->name; |
60
|
9 |
|
} |
61
|
|
|
|
62
|
9 |
|
/** |
63
|
|
|
* @param string $name |
64
|
9 |
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function setName($name = null) |
67
|
|
|
{ |
68
|
|
|
$this->name = $name; |
69
|
|
|
|
70
|
5 |
|
return $this; |
71
|
|
|
} |
72
|
5 |
|
|
73
|
|
|
/** |
74
|
|
|
* @return PropertyMetadataInterface |
75
|
|
|
*/ |
76
|
|
|
public function getIdMetadata() |
77
|
|
|
{ |
78
|
|
|
return $this->idMetadata; |
79
|
9 |
|
} |
80
|
|
|
|
81
|
9 |
|
/** |
82
|
|
|
* @param PropertyMetadataInterface $idMetadata |
83
|
9 |
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function setIdMetadata($idMetadata) |
86
|
|
|
{ |
87
|
|
|
$this->idMetadata = $idMetadata; |
88
|
|
|
|
89
|
9 |
|
return $this; |
90
|
|
|
} |
91
|
9 |
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritdoc |
94
|
|
|
*/ |
95
|
|
|
public function getAttributes() |
96
|
|
|
{ |
97
|
|
|
return $this->attributes; |
98
|
|
|
} |
99
|
|
|
|
100
|
9 |
|
/** |
101
|
|
|
* Add resource attribute metadata |
102
|
9 |
|
* |
103
|
|
|
* @param PropertyMetadataInterface $attribute |
104
|
9 |
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function addAttribute(PropertyMetadataInterface $attribute) |
107
|
|
|
{ |
108
|
|
|
$this->attributes[$attribute->getPropertyName()] = $attribute; |
109
|
|
|
|
110
|
6 |
|
return $this; |
111
|
|
|
} |
112
|
6 |
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritdoc |
115
|
|
|
*/ |
116
|
|
|
public function getRelationships() |
117
|
|
|
{ |
118
|
|
|
return $this->relationships; |
119
|
|
|
} |
120
|
|
|
|
121
|
9 |
|
/** |
122
|
|
|
* Add resource relationship metadata |
123
|
9 |
|
* |
124
|
|
|
* @param PropertyMetadataInterface $relationship |
125
|
9 |
|
* @return $this |
126
|
|
|
*/ |
127
|
|
|
public function addRelationship(PropertyMetadataInterface $relationship) |
128
|
|
|
{ |
129
|
|
|
$this->relationships[$relationship->getPropertyName()] = $relationship; |
130
|
|
|
|
131
|
6 |
|
return $this; |
132
|
|
|
} |
133
|
6 |
|
|
134
|
6 |
|
/** |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
1 |
|
public function getLoader() |
138
|
1 |
|
{ |
139
|
1 |
|
return $this->loader; |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
5 |
|
* @param string $loader |
144
|
5 |
|
* @return $this |
145
|
5 |
|
*/ |
146
|
|
|
public function setLoader($loader = null) |
147
|
5 |
|
{ |
148
|
5 |
|
$this->loader = $loader; |
149
|
5 |
|
|
150
|
|
|
return $this; |
151
|
5 |
|
} |
152
|
5 |
|
|
153
|
5 |
|
/** |
154
|
|
|
* @inheritdoc |
155
|
5 |
|
*/ |
156
|
|
|
public function mergeMetadata($metadata = null) |
157
|
|
|
{ |
158
|
|
|
if (null !== $metadata) { |
159
|
|
|
if (!$metadata instanceof ResourceMetadataInterface) { |
160
|
|
|
/* @var $metadata \Reva2\JsonApi\Contracts\Decoders\Mapping\GenericMetadataInterface */ |
161
|
|
|
|
162
|
|
|
throw new \RuntimeException(sprintf( |
163
|
|
|
"Failed to merge metadata from parent class %s", |
164
|
|
|
$metadata->getClassName() |
165
|
|
|
)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (empty($this->name)) { |
169
|
|
|
$this->name = $metadata->getName(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (empty($this->loader)) { |
173
|
|
|
$this->loader = $metadata->getLoader(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if (null === $this->idMetadata) { |
177
|
|
|
$this->idMetadata = $metadata->getIdMetadata(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$this->attributes = array_merge($this->attributes, $metadata->getAttributes()); |
181
|
|
|
$this->relationships = array_merge($this->relationships, $metadata->getRelationships()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|