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 PropertyMetadataInterface |
33
|
|
|
*/ |
34
|
|
|
public $idMetadata; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var PropertyMetadataInterface[] |
38
|
|
|
* @internal |
39
|
|
|
*/ |
40
|
|
|
public $attributes = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var PropertyMetadataInterface[] |
44
|
|
|
* @internal |
45
|
|
|
*/ |
46
|
1 |
|
public $relationships = []; |
47
|
|
|
|
48
|
1 |
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
|
public function getName() |
52
|
|
|
{ |
53
|
|
|
return $this->name; |
54
|
|
|
} |
55
|
3 |
|
|
56
|
|
|
/** |
57
|
3 |
|
* @param string $name |
58
|
|
|
* @return $this |
59
|
3 |
|
*/ |
60
|
|
|
public function setName($name = null) |
61
|
|
|
{ |
62
|
|
|
$this->name = $name; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
2 |
|
} |
66
|
|
|
|
67
|
2 |
|
/** |
68
|
|
|
* @return PropertyMetadataInterface |
69
|
|
|
*/ |
70
|
|
|
public function getIdMetadata() |
71
|
|
|
{ |
72
|
|
|
return $this->idMetadata; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
3 |
|
* @param PropertyMetadataInterface $idMetadata |
77
|
|
|
* @return $this |
78
|
3 |
|
*/ |
79
|
|
|
public function setIdMetadata($idMetadata) |
80
|
3 |
|
{ |
81
|
|
|
$this->idMetadata = $idMetadata; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
/** |
87
|
|
|
* @inheritdoc |
88
|
2 |
|
*/ |
89
|
|
|
public function getAttributes() |
90
|
|
|
{ |
91
|
|
|
return $this->attributes; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Add resource attribute metadata |
96
|
|
|
* |
97
|
3 |
|
* @param PropertyMetadataInterface $attribute |
98
|
|
|
* @return $this |
99
|
3 |
|
*/ |
100
|
|
|
public function addAttribute(PropertyMetadataInterface $attribute) |
101
|
3 |
|
{ |
102
|
|
|
$this->attributes[$attribute->getPropertyName()] = $attribute; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
/** |
108
|
|
|
* @inheritdoc |
109
|
2 |
|
*/ |
110
|
2 |
|
public function getRelationships() |
111
|
|
|
{ |
112
|
|
|
return $this->relationships; |
113
|
1 |
|
} |
114
|
1 |
|
|
115
|
1 |
|
/** |
116
|
1 |
|
* Add resource relationship metadata |
117
|
|
|
* |
118
|
|
|
* @param PropertyMetadataInterface $relationship |
119
|
1 |
|
* @return $this |
120
|
1 |
|
*/ |
121
|
1 |
|
public function addRelationship(PropertyMetadataInterface $relationship) |
122
|
|
|
{ |
123
|
1 |
|
$this->relationships[$relationship->getPropertyName()] = $relationship; |
124
|
1 |
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
1 |
|
|
128
|
|
|
/** |
129
|
|
|
* @inheritdoc |
130
|
|
|
*/ |
131
|
|
|
public function mergeMetadata($metadata = null) |
132
|
|
|
{ |
133
|
|
|
if (null !== $metadata) { |
134
|
|
|
if (!$metadata instanceof ResourceMetadataInterface) { |
135
|
|
|
/* @var $metadata \Reva2\JsonApi\Contracts\Decoders\Mapping\GenericMetadataInterface */ |
136
|
|
|
|
137
|
|
|
throw new \RuntimeException(sprintf( |
138
|
|
|
"Failed to merge metadata from parent class %s", |
139
|
|
|
$metadata->getClassName() |
140
|
|
|
)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (empty($this->name)) { |
144
|
|
|
$this->name = $metadata->getName(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (null === $this->idMetadata) { |
148
|
|
|
$this->idMetadata = $metadata->getIdMetadata(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$this->attributes = array_merge($this->attributes, $metadata->getAttributes()); |
152
|
|
|
$this->relationships = array_merge($this->relationships, $metadata->getRelationships()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
} |