1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Webcook security bundle. |
5
|
|
|
* |
6
|
|
|
* See LICENSE file in the root of the bundle. Webcook |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Webcook\Cms\SecurityBundle\Entity; |
10
|
|
|
|
11
|
|
|
use Symfony\Component\Security\Core\Role\RoleInterface; |
12
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
13
|
|
|
use Webcook\Cms\CoreBundle\Base\BasicEntity; |
14
|
|
|
use Doctrine\ORM\Mapping as ORM; |
15
|
|
|
use ApiPlatform\Core\Annotation\ApiResource; |
16
|
|
|
use ApiPlatform\Core\Annotation\ApiProperty; |
17
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* System role entity. |
21
|
|
|
* |
22
|
|
|
* @ApiResource |
23
|
|
|
* @ORM\Table(name="SecurityRole") |
24
|
|
|
* @ORM\Entity() |
25
|
|
|
*/ |
26
|
|
|
class Role extends BasicEntity implements RoleInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Role name. |
30
|
|
|
* |
31
|
|
|
* @ORM\Column(name="name", type="string", length=30) |
32
|
|
|
* @Assert\NotNull |
33
|
|
|
* @Assert\NotBlank |
34
|
|
|
*/ |
35
|
|
|
private $name; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Role identification. |
39
|
|
|
* |
40
|
|
|
* @ORM\Column(name="role", type="string", length=20, unique=true) |
41
|
|
|
* @Assert\NotNull |
42
|
|
|
* @Assert\NotBlank |
43
|
|
|
*/ |
44
|
|
|
private $role; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Resources of the role and their permissions. |
48
|
49 |
|
* |
49
|
|
|
* @ORM\OneToMany(targetEntity="RoleResource", mappedBy="role", cascade={"remove"}, fetch="EAGER")) |
50
|
49 |
|
**/ |
51
|
49 |
|
private $resources; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Class constructor. |
55
|
|
|
*/ |
56
|
|
|
public function __construct() |
57
|
|
|
{ |
58
|
6 |
|
$this->resources = new ArrayCollection(); |
59
|
|
|
} |
60
|
6 |
|
|
61
|
|
|
/** |
62
|
|
|
* Get role object. |
63
|
|
|
* |
64
|
|
|
* @see RoleInterface |
65
|
|
|
*/ |
66
|
|
|
public function getRole() |
67
|
|
|
{ |
68
|
6 |
|
return $this->role; |
69
|
|
|
} |
70
|
6 |
|
|
71
|
|
|
/** |
72
|
|
|
* Gets the value of name. |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
public function getName() |
77
|
|
|
{ |
78
|
|
|
return $this->name; |
79
|
|
|
} |
80
|
49 |
|
|
81
|
|
|
/** |
82
|
49 |
|
* Sets the value of name. |
83
|
|
|
* |
84
|
49 |
|
* @param string $name the name |
85
|
|
|
* |
86
|
|
|
* @return self |
87
|
|
|
*/ |
88
|
|
|
public function setName($name) |
89
|
|
|
{ |
90
|
|
|
$this->name = $name; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
49 |
|
|
95
|
|
|
/** |
96
|
49 |
|
* Sets the value of role. |
97
|
|
|
* |
98
|
49 |
|
* @param string $role the role |
99
|
|
|
* |
100
|
|
|
* @return self |
101
|
|
|
*/ |
102
|
|
|
public function setRole($role) |
103
|
|
|
{ |
104
|
|
|
$this->role = $role; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
30 |
|
|
109
|
|
|
/** |
110
|
30 |
|
* Get resource by name. |
111
|
30 |
|
* |
112
|
30 |
|
* @param string $name [description] |
113
|
|
|
* |
114
|
|
|
* @return Resource [description] |
115
|
|
|
*/ |
116
|
1 |
|
public function getResource($name) |
117
|
|
|
{ |
118
|
|
|
foreach ($this->resources as $resource) { |
119
|
|
|
if ($resource->getResource()->getName() == $name) { |
120
|
|
|
return $resource; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
return false; |
|
|
|
|
125
|
|
|
} |
126
|
2 |
|
|
127
|
|
|
/** |
128
|
|
|
* Gets the value of resources. |
129
|
|
|
* |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function getResources() |
133
|
|
|
{ |
134
|
|
|
return $this->resources; |
135
|
|
|
} |
136
|
1 |
|
|
137
|
|
|
/** |
138
|
1 |
|
* Sets the value of resources. |
139
|
|
|
* |
140
|
1 |
|
* @param mixed $resources the resources |
141
|
|
|
* |
142
|
|
|
* @return self |
143
|
|
|
*/ |
144
|
|
|
public function setResources($resources) |
145
|
|
|
{ |
146
|
|
|
$this->resources = $resources; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
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.