|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\DataTransferObject; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use Doctrine\Common\Collections\Collection; |
|
7
|
|
|
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\Interfaces\UserDataTransferObject; |
|
8
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User; |
|
9
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Entity\BaseUser; |
|
10
|
|
|
|
|
11
|
|
|
class BaseUserDataTransferObject implements UserDataTransferObject |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var int|null */ |
|
14
|
|
|
public $id; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string|null */ |
|
17
|
|
|
public $userName; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string|null */ |
|
20
|
|
|
public $displayName; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string|null */ |
|
23
|
|
|
public $email; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string|null */ |
|
26
|
|
|
public $plainPassword; |
|
27
|
|
|
|
|
28
|
|
|
/** @var User */ |
|
29
|
|
|
protected $user; |
|
30
|
|
|
|
|
31
|
|
|
/** @var Collection|null */ |
|
32
|
|
|
public $roles; |
|
33
|
|
|
|
|
34
|
|
|
public static function fromUser(User $user): UserDataTransferObject |
|
35
|
|
|
{ |
|
36
|
|
|
$baseUserTransferObject = new static(); |
|
37
|
|
|
$baseUserTransferObject->user = $user; |
|
38
|
|
|
$baseUserTransferObject->id = $user->getId(); |
|
39
|
|
|
$baseUserTransferObject->userName = $user->getUsername(); |
|
40
|
|
|
$baseUserTransferObject->displayName = $user->getDisplayName(); |
|
41
|
|
|
$baseUserTransferObject->email = $user->getEmail(); |
|
42
|
|
|
$baseUserTransferObject->roles = $user->getRolesAsCollection(); |
|
43
|
|
|
if ($user->hasPlainPassword()) { |
|
44
|
|
|
$baseUserTransferObject->plainPassword = $user->getPlainPassword(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $baseUserTransferObject; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getEntity(): User |
|
51
|
|
|
{ |
|
52
|
|
|
if ($this->user) { |
|
53
|
|
|
$this->user->change($this); |
|
54
|
|
|
|
|
55
|
|
|
return $this->user; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return new BaseUser( |
|
59
|
|
|
$this->userName, |
|
60
|
|
|
$this->plainPassword, |
|
61
|
|
|
$this->displayName, |
|
62
|
|
|
$this->email, |
|
63
|
|
|
$this->roles instanceof Collection ? $this->roles : new ArrayCollection() |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getId(): ?int |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->id; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getUserName(): ?string |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->userName; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getDisplayName(): ?string |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->displayName; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getEmail(): ?string |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->email; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getPlainPassword(): ?string |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->plainPassword; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getRoles(): ?Collection |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->roles; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
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.