|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Kreta package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
|
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
declare(strict_types = 1); |
|
14
|
|
|
|
|
15
|
|
|
namespace Kreta\IdentityAccess\Application\DataTransformer; |
|
16
|
|
|
|
|
17
|
|
|
use BenGorUser\User\Application\DataTransformer\UserDTODataTransformer as BaseUserDTODataTransformer; |
|
18
|
|
|
use Kreta\IdentityAccess\Domain\Model\User\FullName; |
|
19
|
|
|
use Kreta\IdentityAccess\Domain\Model\User\Image; |
|
20
|
|
|
|
|
21
|
|
|
class UserDTODataTransformer extends BaseUserDTODataTransformer |
|
22
|
|
|
{ |
|
23
|
|
|
public function read() : array |
|
24
|
|
|
{ |
|
25
|
|
|
if (null === $this->user) { |
|
26
|
|
|
return []; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return array_merge(parent::read(), [ |
|
30
|
|
|
'user_name' => $this->user->username()->username(), |
|
|
|
|
|
|
31
|
|
|
'first_name' => $this->firstName($this->user->fullName()), |
|
|
|
|
|
|
32
|
|
|
'last_name' => $this->lastName($this->user->fullName()), |
|
|
|
|
|
|
33
|
|
|
'full_name' => $this->fullName($this->user->fullName()), |
|
|
|
|
|
|
34
|
|
|
'image_name' => $this->image($this->user->image()), |
|
|
|
|
|
|
35
|
|
|
]); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function firstName(FullName $fullName = null) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($fullName instanceof FullName) { |
|
41
|
|
|
return $fullName->firstName(); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function lastName(FullName $fullName = null) |
|
46
|
|
|
{ |
|
47
|
|
|
if ($fullName instanceof FullName) { |
|
48
|
|
|
return $fullName->lastName(); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function fullName(FullName $fullName = null) |
|
53
|
|
|
{ |
|
54
|
|
|
if ($fullName instanceof FullName) { |
|
55
|
|
|
return $fullName->fullName(); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function image(Image $image = null) |
|
60
|
|
|
{ |
|
61
|
|
|
if ($image instanceof Image) { |
|
62
|
|
|
return $image->name()->filename(); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: