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 Zend\Hydrator\Exception\BadMethodCallException; |
10
|
|
|
use Zend\Hydrator\HydratorInterface; |
11
|
|
|
use Zend\Hydrator\Strategy\StrategyInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class InvoiceItemsStrategy |
15
|
|
|
*/ |
16
|
|
|
class InvoiceItemsStrategy implements StrategyInterface |
17
|
|
|
{ |
18
|
|
|
/** @var HydratorInterface */ |
19
|
|
|
private $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 ContactInterface[] $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) :array |
40
|
|
|
{ |
41
|
2 |
|
$result = []; |
42
|
|
|
/** @var InvoiceItemInterface $invoiceItem */ |
43
|
2 |
|
foreach ($value as $invoiceItem) { |
44
|
1 |
|
if ($invoiceItem instanceof InvoiceItemInterface) { |
45
|
1 |
|
$result[] = $this->hydrator->extract($invoiceItem); |
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 |
59
|
|
|
{ |
60
|
2 |
|
$result = []; |
61
|
2 |
|
if (is_array($value)) { |
62
|
2 |
|
foreach ($value as $invoiceItemArr) { |
63
|
1 |
|
$invoiceItem = new InvoiceItem(); |
64
|
1 |
|
$this->hydrator->hydrate($invoiceItemArr, $invoiceItem); |
65
|
1 |
|
$result[] = $invoiceItem; |
66
|
|
|
} |
67
|
|
|
} |
68
|
2 |
|
return $result; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|