|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\User; |
|
4
|
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Security\PasswordResetToken; |
|
6
|
|
|
|
|
7
|
|
|
class UserWithPassword implements User, PasswordReset |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $username; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $password; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $displayName; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var PasswordResetToken |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $passwordResetToken; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $email; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var int |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $id; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $username |
|
41
|
|
|
* @param string $password |
|
42
|
|
|
* @param string $displayName |
|
43
|
|
|
* @param string $email |
|
44
|
|
|
* @param int $id |
|
45
|
|
|
* @param PasswordResetToken $token |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct( |
|
48
|
|
|
$username, |
|
49
|
|
|
$password, |
|
50
|
|
|
$displayName, |
|
51
|
|
|
$email, |
|
52
|
|
|
$id = null, |
|
53
|
|
|
PasswordResetToken $token = null |
|
54
|
|
|
) { |
|
55
|
|
|
$this->username = $username; |
|
56
|
|
|
$this->password = $password; |
|
57
|
|
|
$this->displayName = $displayName; |
|
58
|
|
|
$this->email = $email; |
|
59
|
|
|
$this->id = $id; |
|
60
|
|
|
|
|
61
|
|
|
if ($token) { |
|
62
|
|
|
$this->passwordResetToken = $token; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritDoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getRoles() |
|
70
|
|
|
{ |
|
71
|
|
|
return [ 'ROLE_USER' ]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritDoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getPassword() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->password; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritDoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getSalt() |
|
86
|
|
|
{ |
|
87
|
|
|
return; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritDoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getUsername() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->username; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritDoc} |
|
100
|
|
|
*/ |
|
101
|
|
|
public function eraseCredentials() |
|
102
|
|
|
{ |
|
103
|
|
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritDoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getDisplayName() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->displayName; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritDoc} |
|
116
|
|
|
*/ |
|
117
|
|
|
public function __toString() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->getDisplayName(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
|
|
public function clearPasswordResetToken() |
|
126
|
|
|
{ |
|
127
|
|
|
$this->passwordResetToken = null; |
|
128
|
|
|
|
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return self |
|
134
|
|
|
*/ |
|
135
|
|
|
public function generatePasswordResetToken() |
|
136
|
|
|
{ |
|
137
|
|
|
$this->passwordResetToken = PasswordResetToken::generate(); |
|
138
|
|
|
|
|
139
|
|
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getPasswordResetToken() |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->passwordResetToken; |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getEmail() |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->email; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param $password |
|
160
|
|
|
* |
|
161
|
|
|
* @return self |
|
162
|
|
|
*/ |
|
163
|
|
|
public function setPassword($password) |
|
164
|
|
|
{ |
|
165
|
|
|
$this->password = $password; |
|
166
|
|
|
|
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return int |
|
172
|
|
|
*/ |
|
173
|
|
|
public function getId() |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->id; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
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.