|
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\AttributeMetadataInterface; |
|
15
|
|
|
use Reva2\JsonApi\Contracts\Decoders\Mapping\RelationshipMetadataInterface; |
|
16
|
|
|
use Reva2\JsonApi\Contracts\Decoders\Mapping\ResourceMetadataInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* JSON API resource metadata |
|
20
|
|
|
* |
|
21
|
|
|
* @package Reva2\JsonApi\Decoders\Mapping |
|
22
|
|
|
* @author Sergey Revenko <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class ResourceMetadata extends GenericMetadata implements ResourceMetadataInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
* @internal |
|
29
|
|
|
*/ |
|
30
|
|
|
public $name; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var AttributeMetadataInterface[] |
|
34
|
|
|
* @internal |
|
35
|
|
|
*/ |
|
36
|
|
|
public $attributes = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var RelationshipMetadataInterface[] |
|
40
|
|
|
* @internal |
|
41
|
|
|
*/ |
|
42
|
|
|
public $relationships = []; |
|
43
|
|
|
|
|
44
|
|
|
/*** |
|
45
|
|
|
* @var string|null |
|
46
|
|
|
* @internal |
|
47
|
|
|
*/ |
|
48
|
|
|
public $discField; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var array |
|
52
|
|
|
* @internal |
|
53
|
|
|
*/ |
|
54
|
|
|
public $discMap; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Constructor |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $name |
|
60
|
|
|
* @param $className |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct($name, $className) |
|
63
|
|
|
{ |
|
64
|
|
|
parent::__construct($className); |
|
65
|
|
|
|
|
66
|
|
|
$this->name = $name; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @inheritdoc |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getName() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->name; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @inheritdoc |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getAttributes() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->attributes; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Add resource attribute metadata |
|
87
|
|
|
* |
|
88
|
|
|
* @param AttributeMetadataInterface $attribute |
|
89
|
|
|
* @return $this |
|
90
|
|
|
*/ |
|
91
|
|
|
public function addAttribute(AttributeMetadataInterface $attribute) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->attributes[$attribute->getPropertyName()] = $attribute; |
|
94
|
|
|
|
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @inheritdoc |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getRelationships() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->relationships; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Add resource relationship metadata |
|
108
|
|
|
* |
|
109
|
|
|
* @param RelationshipMetadataInterface $relationship |
|
110
|
|
|
* @return $this |
|
111
|
|
|
*/ |
|
112
|
|
|
public function addRelationship(RelationshipMetadataInterface $relationship) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->relationships[$relationship->getPropertyName()] = $relationship; |
|
115
|
|
|
|
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @inheritdoc |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getDiscriminatorField() |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->discField; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Sets discriminator field |
|
129
|
|
|
* |
|
130
|
|
|
* @param string|null $field |
|
131
|
|
|
* @return $this |
|
132
|
|
|
*/ |
|
133
|
|
|
public function setDiscriminatorField($field = null) |
|
134
|
|
|
{ |
|
135
|
|
|
$this->discField = $field; |
|
136
|
|
|
|
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Sets discriminator map |
|
142
|
|
|
* |
|
143
|
|
|
* @param array $map |
|
144
|
|
|
* @return $this |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setDiscriminatorMap(array $map) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->discMap = $map; |
|
149
|
|
|
|
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @inheritdoc |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getDiscriminatorClass($value) |
|
157
|
|
|
{ |
|
158
|
|
|
if (!array_key_exists($value, $this->discMap)) { |
|
159
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
160
|
|
|
"Discriminator class for value '%s' not specified", |
|
161
|
|
|
$value |
|
162
|
|
|
)); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $this->discMap[$value]; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Merge resource metadata |
|
170
|
|
|
* |
|
171
|
|
|
* @param mixed $metadata |
|
172
|
|
|
* @return $this |
|
173
|
|
|
*/ |
|
174
|
|
|
public function mergeMetadata($metadata) |
|
175
|
|
|
{ |
|
176
|
|
|
if (null === $metadata) { |
|
177
|
|
|
return $this; |
|
178
|
|
|
} elseif (!$metadata instanceof ResourceMetadataInterface) { |
|
179
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
180
|
|
|
"Couldn't merge metadata from %s instance", |
|
181
|
|
|
get_class($metadata) |
|
182
|
|
|
)); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$this->attributes = array_merge($this->attributes, $metadata->getAttributes()); |
|
186
|
|
|
$this->relationships = array_merge($this->relationships, $metadata->getRelationships()); |
|
187
|
|
|
|
|
188
|
|
|
return $this; |
|
189
|
|
|
} |
|
190
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.