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