|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HelloWordPl\SimpleEntityGeneratorBundle\Tests\Lib\Dummies; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* User dummy class for StructureResolver tests |
|
7
|
|
|
*/ |
|
8
|
|
|
class User implements \HelloWordPl\SimpleEntityGeneratorBundle\Tests\Lib\Dummies\UserInterface |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* 'full_name' property |
|
13
|
|
|
* @\Symfony\Component\Validator\Constraints\NotBlank() |
|
14
|
|
|
* @\JMS\Serializer\Annotation\Type("string") |
|
15
|
|
|
* @\JMS\Serializer\Annotation\SerializedName("full_name") |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $fullName; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* 'email' property |
|
22
|
|
|
* @\Symfony\Component\Validator\Constraints\Email(message = "Invalid email!") |
|
23
|
|
|
* @\JMS\Serializer\Annotation\Type("string") |
|
24
|
|
|
* @\JMS\Serializer\Annotation\SerializedName("email") |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $email; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Wether user active |
|
31
|
|
|
* @\Symfony\Component\Validator\Constraints\Type(type='boolean') |
|
32
|
|
|
* @\Symfony\Component\Validator\Constraints\IsTrue() |
|
33
|
|
|
* @\JMS\Serializer\Annotation\Type("boolean") |
|
34
|
|
|
* @\JMS\Serializer\Annotation\SerializedName("active") |
|
35
|
|
|
* @var boolean |
|
36
|
|
|
*/ |
|
37
|
|
|
private $active; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* User new posts |
|
41
|
|
|
* @\Symfony\Component\Validator\Constraints\NotNull() |
|
42
|
|
|
* @\Symfony\Component\Validator\Constraints\Valid() |
|
43
|
|
|
* @\JMS\Serializer\Annotation\Type("Doctrine\Common\Collections\ArrayCollection<AppBundle\Entity\Post>") |
|
44
|
|
|
* @\JMS\Serializer\Annotation\SerializedName("new_posts") |
|
45
|
|
|
* @var \Doctrine\Common\Collections\ArrayCollection |
|
46
|
|
|
*/ |
|
47
|
|
|
private $newPosts; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Constructor. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->newPosts = new \Doctrine\Common\Collections\ArrayCollection(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* For property "fullName" |
|
59
|
|
|
* @param string $fullName |
|
60
|
|
|
* @return this |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setFullName($fullName) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->fullName = $fullName; |
|
65
|
|
|
return $this; |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* For property "fullName" |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getFullName() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->fullName; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* For property "email" |
|
79
|
|
|
* @param string $email |
|
80
|
|
|
* @return this |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setEmail($email) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->email = $email; |
|
85
|
|
|
return $this; |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* For property "email" |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getEmail() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->email; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* For property "active" |
|
99
|
|
|
* @return boolean |
|
100
|
|
|
*/ |
|
101
|
|
|
public function isActive() |
|
102
|
|
|
{ |
|
103
|
|
|
return (bool) $this->active; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* For property "active" |
|
108
|
|
|
* @param boolean $active |
|
109
|
|
|
* @return this |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setActive($active) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->active = $active; |
|
114
|
|
|
return $this; |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* For property "active" |
|
119
|
|
|
* @return boolean |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getActive() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->active; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* For property "newPosts" |
|
128
|
|
|
* @param \Doctrine\Common\Collections\ArrayCollection $newPosts |
|
129
|
|
|
* @return this |
|
130
|
|
|
*/ |
|
131
|
|
|
public function setNewPosts(\Doctrine\Common\Collections\ArrayCollection $newPosts) |
|
132
|
|
|
{ |
|
133
|
|
|
$this->newPosts = $newPosts; |
|
134
|
|
|
return $this; |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* For property "newPosts" |
|
139
|
|
|
* @return \Doctrine\Common\Collections\ArrayCollection |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getNewPosts() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->newPosts; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
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.