1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace InvoiceNinjaModule\Strategy; |
4
|
|
|
|
5
|
|
|
use InvoiceNinjaModule\Model\Interfaces\ContactInterface; |
6
|
|
|
use InvoiceNinjaModule\Model\Interfaces\InvoiceItemInterface; |
7
|
|
|
use InvoiceNinjaModule\Model\InvoiceItem; |
8
|
|
|
use Zend\Hydrator\ClassMethods; |
9
|
|
|
use Zend\Hydrator\Exception\BadMethodCallException; |
10
|
|
|
use Zend\Hydrator\HydratorInterface; |
11
|
|
|
use Zend\Hydrator\Strategy\StrategyInterface; |
12
|
|
|
|
13
|
|
|
class InvoiceItemsStrategy implements StrategyInterface |
14
|
|
|
{ |
15
|
|
|
/** @var ClassMethods */ |
16
|
|
|
private $hydrator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* InvoiceItemsStrategy constructor. |
20
|
|
|
* |
21
|
|
|
* @param HydratorInterface $hydrator |
22
|
|
|
*/ |
23
|
|
|
public function __construct(HydratorInterface $hydrator) |
24
|
|
|
{ |
25
|
|
|
$this->hydrator = $hydrator; |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Converts the given value so that it can be extracted by the hydrator. |
30
|
|
|
* |
31
|
|
|
* @param ContactInterface[] $value The original value. |
32
|
|
|
* |
33
|
|
|
* @return array Returns the value that should be extracted. |
34
|
|
|
* @throws BadMethodCallException for a non-object $contactObj |
35
|
|
|
*/ |
36
|
|
|
public function extract($value) |
37
|
|
|
{ |
38
|
|
|
$result = []; |
39
|
|
|
/** @var InvoiceItemInterface $invoiceItem */ |
40
|
|
|
foreach ($value as $invoiceItem) { |
41
|
|
|
$result[] = $this->hydrator->extract($invoiceItem); |
42
|
|
|
} |
43
|
|
|
return $result; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Converts the given value so that it can be hydrated by the hydrator. |
48
|
|
|
* @param array $value |
49
|
|
|
* |
50
|
|
|
* @return ContactInterface[] |
51
|
|
|
* @throws BadMethodCallException for a non-object $contactObj |
52
|
|
|
*/ |
53
|
|
|
public function hydrate($value) |
54
|
|
|
{ |
55
|
|
|
$result = []; |
56
|
|
|
if (is_array($value)) { |
57
|
|
|
foreach ($value as $invoiceItemArr) { |
58
|
|
|
$invoiceItem = new InvoiceItem(); |
59
|
|
|
$this->hydrator->hydrate($invoiceItemArr, $invoiceItem); |
60
|
|
|
$result[] = $invoiceItem; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
return $result; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.