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