1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of AppBundle the package. |
4
|
|
|
* |
5
|
|
|
* (c) Ruslan Muriev <[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
|
|
|
namespace RonteLtd\JsonApiBundle\Serializer\Mapping; |
12
|
|
|
|
13
|
|
|
use RonteLtd\JsonApiBundle\Annotation\Attribute; |
14
|
|
|
use RonteLtd\JsonApiBundle\Annotation\Relationship; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AttributeMetadata |
18
|
|
|
* |
19
|
|
|
* @package RonteLtd\Bundle\Serializer\Mapping |
20
|
|
|
* @author Ruslan Muriev <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class AttributeMetadata implements AttributeMetadataInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
* |
27
|
|
|
* @internal This property is public in order to reduce the size of the |
28
|
|
|
* class' serialized representation. Do not access it. Use |
29
|
|
|
* {@link getName()} instead. |
30
|
|
|
*/ |
31
|
|
|
public $name; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Attribute |
35
|
|
|
*/ |
36
|
|
|
public $attribute; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Relationship |
40
|
|
|
*/ |
41
|
|
|
public $relationship; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructs a metadata for the given attribute. |
45
|
|
|
* |
46
|
|
|
* @param string $name |
47
|
|
|
*/ |
48
|
|
|
public function __construct($name) |
49
|
|
|
{ |
50
|
|
|
$this->name = $name; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function getName() |
57
|
|
|
{ |
58
|
|
|
return $this->name; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return Attribute |
63
|
|
|
*/ |
64
|
|
|
public function getAttribute() |
65
|
|
|
{ |
66
|
|
|
return $this->attribute; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Attribute $attribute |
71
|
|
|
*/ |
72
|
|
|
public function setAttribute($attribute) |
73
|
|
|
{ |
74
|
|
|
$this->attribute = $attribute; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return Relationship |
79
|
|
|
*/ |
80
|
|
|
public function getRelationship() |
81
|
|
|
{ |
82
|
|
|
return $this->relationship; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param Relationship $relationship |
87
|
|
|
*/ |
88
|
|
|
public function setRelationship($relationship) |
89
|
|
|
{ |
90
|
|
|
$this->relationship = $relationship; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function merge(AttributeMetadataInterface $attributeMetadata) |
97
|
|
|
{ |
98
|
|
|
// Overwrite only if not defined |
99
|
|
|
if (null === $this->attribute) { |
100
|
|
|
$this->attribute = $attributeMetadata->getAttribute(); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Overwrite only if not defined |
104
|
|
|
if (null === $this->relationship) { |
105
|
|
|
$this->relationship = $attributeMetadata->getRelationship(); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Returns the names of the properties that should be serialized. |
111
|
|
|
* |
112
|
|
|
* @return string[] |
113
|
|
|
*/ |
114
|
|
|
public function __sleep() |
115
|
|
|
{ |
116
|
|
|
return array('name', 'attribute', 'relationship'); |
117
|
|
|
} |
118
|
|
|
} |
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.