1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LmcUser\Mapper; |
4
|
|
|
|
5
|
|
|
use Laminas\Hydrator\HydratorInterface; |
6
|
|
|
use LmcUser\Entity\UserInterface as UserEntityInterface; |
7
|
|
|
use LmcUser\Mapper\Exception\InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class UserHydrator |
11
|
|
|
*/ |
12
|
|
|
class UserHydrator implements HydratorInterface |
13
|
|
|
{ |
14
|
|
|
/** @var HydratorInterface */ |
15
|
|
|
private $hydrator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* UserHydrator constructor. |
19
|
|
|
* |
20
|
|
|
* @param HydratorInterface $hydrator |
21
|
|
|
*/ |
22
|
|
|
public function __construct(HydratorInterface $hydrator) |
23
|
|
|
{ |
24
|
|
|
$this->hydrator = $hydrator; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Extract values from an object |
29
|
|
|
* |
30
|
|
|
* @param UserEntityInterface $object |
31
|
|
|
* @return array |
32
|
|
|
* @throws Exception\InvalidArgumentException |
33
|
|
|
*/ |
34
|
|
|
public function extract($object): array |
35
|
|
|
{ |
36
|
|
|
if (!$object instanceof UserEntityInterface) { |
37
|
|
|
throw new InvalidArgumentException('$object must be an instance of LmcUser\Entity\UserInterface'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$data = $this->hydrator->extract($object); |
|
|
|
|
41
|
|
|
if ($data['id'] !== null) { |
42
|
|
|
$data = $this->mapField('id', 'user_id', $data); |
43
|
|
|
} else { |
44
|
|
|
unset($data['id']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $data; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Hydrate $object with the provided $data. |
52
|
|
|
* |
53
|
|
|
* @param array $data |
54
|
|
|
* @param UserEntityInterface $object |
55
|
|
|
* @return UserInterface |
56
|
|
|
* @throws Exception\InvalidArgumentException |
57
|
|
|
*/ |
58
|
|
|
public function hydrate(array $data, $object) |
59
|
|
|
{ |
60
|
|
|
if (!$object instanceof UserEntityInterface) { |
61
|
|
|
throw new Exception\InvalidArgumentException('$object must be an instance of LmcUser\Entity\UserInterface'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$data = $this->mapField('user_id', 'id', $data); |
65
|
|
|
|
66
|
|
|
return $this->hydrator->hydrate($data, $object); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $keyFrom |
71
|
|
|
* @param string $keyTo |
72
|
|
|
* @param array $array |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
protected function mapField($keyFrom, $keyTo, array $array) |
76
|
|
|
{ |
77
|
|
|
$array[$keyTo] = $array[$keyFrom]; |
78
|
|
|
unset($array[$keyFrom]); |
79
|
|
|
|
80
|
|
|
return $array; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: