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
|
|
|
* Constructor |
45
|
|
|
* |
46
|
|
|
* @param string $name |
47
|
|
|
* @param $className |
48
|
|
|
*/ |
49
|
|
|
public function __construct($name, $className) |
50
|
|
|
{ |
51
|
|
|
parent::__construct($className); |
52
|
|
|
|
53
|
|
|
$this->name = $name; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function getName() |
60
|
|
|
{ |
61
|
|
|
return $this->name; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
|
|
public function getAttributes() |
68
|
|
|
{ |
69
|
|
|
return $this->attributes; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Add resource attribute metadata |
74
|
|
|
* |
75
|
|
|
* @param PropertyMetadataInterface $attribute |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function addAttribute(PropertyMetadataInterface $attribute) |
79
|
|
|
{ |
80
|
|
|
$this->attributes[$attribute->getPropertyName()] = $attribute; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritdoc |
87
|
|
|
*/ |
88
|
|
|
public function getRelationships() |
89
|
|
|
{ |
90
|
|
|
return $this->relationships; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Add resource relationship metadata |
95
|
|
|
* |
96
|
|
|
* @param PropertyMetadataInterface $relationship |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function addRelationship(PropertyMetadataInterface $relationship) |
100
|
|
|
{ |
101
|
|
|
$this->relationships[$relationship->getPropertyName()] = $relationship; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Merge resource metadata |
108
|
|
|
* |
109
|
|
|
* @param ResourceMetadataInterface $metadata |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function mergeMetadata(ResourceMetadataInterface $metadata = null) |
113
|
|
|
{ |
114
|
|
View Code Duplication |
if (null === $metadata) { |
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} elseif (!$metadata instanceof ResourceMetadataInterface) { |
117
|
|
|
throw new \InvalidArgumentException(sprintf( |
118
|
|
|
"Couldn't merge metadata from %s instance", |
119
|
|
|
get_class($metadata) |
120
|
|
|
)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->attributes = array_merge($this->attributes, $metadata->getAttributes()); |
124
|
|
|
$this->relationships = array_merge($this->relationships, $metadata->getRelationships()); |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.