alexz707 /
InvoiceNinjaModule
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace InvoiceNinjaModule\Strategy; |
||
| 6 | |||
| 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 | |||
| 13 | use function is_array; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Class InvoiceItemsStrategy |
||
| 17 | */ |
||
| 18 | final class InvoiceItemsStrategy implements StrategyInterface |
||
| 19 | { |
||
| 20 | private HydratorInterface $hydrator; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * InvoiceItemsStrategy 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 InvoiceItemInterface[] $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 $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 InvoiceItemInterface[] |
||
| 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)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 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 |