1 | <?php |
||
18 | class SecurityUser implements UserInterface |
||
19 | { |
||
20 | const ROLE_USER = 'ROLE_GRAVITON_USER'; |
||
21 | const ROLE_ANONYMOUS = 'ROLE_GRAVITON_ANONYMOUS'; |
||
22 | |||
23 | /** |
||
24 | * @var Object |
||
25 | */ |
||
26 | private $user; |
||
27 | |||
28 | /** |
||
29 | * @var Role[] |
||
30 | */ |
||
31 | private $roles; |
||
32 | |||
33 | |||
34 | /** |
||
35 | * Constructor of the class. |
||
36 | * |
||
37 | * @param object $user the user |
||
38 | * @param Role[] $roles roles for the contract |
||
39 | */ |
||
40 | public function __construct($user, array $roles = array()) |
||
45 | |||
46 | /** |
||
47 | * Returns the roles granted to the user. |
||
48 | * |
||
49 | * @return Role[] The user roles |
||
50 | */ |
||
51 | public function getRoles() |
||
55 | |||
56 | /** |
||
57 | * Returns the password used to authenticate the user. |
||
58 | * |
||
59 | * @return string The password |
||
60 | */ |
||
61 | public function getPassword() |
||
65 | |||
66 | /** |
||
67 | * Returns the salt that was originally used to encode the password. |
||
68 | * |
||
69 | * @return null The salt |
||
70 | */ |
||
71 | public function getSalt() |
||
75 | |||
76 | /** |
||
77 | * Returns the username used to authenticate the user. |
||
78 | * |
||
79 | * @return string The username |
||
80 | */ |
||
81 | public function getUsername() |
||
88 | |||
89 | /** |
||
90 | * Removes sensitive data from the user. |
||
91 | * |
||
92 | * This is important if, at any given point, sensitive information like |
||
93 | * the plain-text password is stored on this object. |
||
94 | * |
||
95 | * @return void |
||
96 | */ |
||
97 | public function eraseCredentials() |
||
100 | |||
101 | /** |
||
102 | * Provides the consultant object. |
||
103 | * |
||
104 | * @return Object |
||
105 | */ |
||
106 | public function getUser() |
||
110 | |||
111 | /** |
||
112 | * Check if user has role |
||
113 | * @param string $role User Role |
||
114 | * @return bool |
||
115 | */ |
||
116 | public function hasRole($role) |
||
120 | |||
121 | /** |
||
122 | * @return string |
||
123 | */ |
||
124 | public function __toString() |
||
130 | } |
||
131 |
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.