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