|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace InvoiceNinjaModule\Strategy; |
|
5
|
|
|
|
|
6
|
|
|
use InvoiceNinjaModule\Model\Interfaces\InvitationInterface; |
|
7
|
|
|
use InvoiceNinjaModule\Model\Invitation; |
|
8
|
|
|
use Laminas\Hydrator\Exception\BadMethodCallException; |
|
9
|
|
|
use Laminas\Hydrator\HydratorInterface; |
|
10
|
|
|
use Laminas\Hydrator\Strategy\StrategyInterface; |
|
11
|
|
|
use function is_array; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class InvitationsStrategy |
|
15
|
|
|
*/ |
|
16
|
|
|
final class InvitationsStrategy implements StrategyInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private HydratorInterface $hydrator; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* InvitationsStrategy constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param HydratorInterface $hydrator |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(HydratorInterface $hydrator) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->hydrator = $hydrator; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Converts the given value so that it can be extracted by the hydrator. |
|
32
|
|
|
* |
|
33
|
|
|
* @param InvitationInterface[] $value The original value. |
|
34
|
|
|
* |
|
35
|
|
|
* @return array Returns the value that should be extracted. |
|
36
|
|
|
* @throws BadMethodCallException for a non-object $contactObj |
|
37
|
|
|
*/ |
|
38
|
|
|
public function extract($value, ?object $object = null): array |
|
39
|
|
|
{ |
|
40
|
|
|
$result = []; |
|
41
|
|
|
foreach ($value as $invitation) { |
|
42
|
|
|
if ($invitation instanceof InvitationInterface) { |
|
43
|
|
|
$result[] = $this->hydrator->extract($invitation); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
return $result; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Converts the given value so that it can be hydrated by the hydrator. |
|
51
|
|
|
* @param array $value |
|
52
|
|
|
* |
|
53
|
|
|
* @return InvitationInterface[] |
|
54
|
|
|
* @throws BadMethodCallException for a non-object $contactObj |
|
55
|
|
|
*/ |
|
56
|
|
|
public function hydrate($value, ?array $data) :array |
|
57
|
|
|
{ |
|
58
|
|
|
$result = []; |
|
59
|
|
|
if (is_array($value)) { |
|
|
|
|
|
|
60
|
|
|
foreach ($value as $invitationArr) { |
|
61
|
|
|
$invitation = new Invitation(); |
|
62
|
|
|
$this->hydrator->hydrate($invitationArr, $invitation); |
|
63
|
|
|
$result[] = $invitation; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
return $result; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|