alexz707 /
InvoiceNinjaModule
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace InvoiceNinjaModule\Strategy; |
||
| 6 | |||
| 7 | use InvoiceNinjaModule\Model\Interfaces\InvitationInterface; |
||
| 8 | use InvoiceNinjaModule\Model\Invitation; |
||
| 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 InvitationsStrategy |
||
| 17 | */ |
||
| 18 | final class InvitationsStrategy implements StrategyInterface |
||
| 19 | { |
||
| 20 | private HydratorInterface $hydrator; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * InvitationsStrategy constructor. |
||
| 24 | * |
||
| 25 | * @param HydratorInterface $hydrator |
||
| 26 | */ |
||
| 27 | 1 | public function __construct(HydratorInterface $hydrator) |
|
| 28 | { |
||
| 29 | 1 | $this->hydrator = $hydrator; |
|
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Converts the given value so that it can be extracted by the hydrator. |
||
| 34 | * |
||
| 35 | * @param InvitationInterface[] $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 | public function extract($value, ?object $object = null): array |
||
| 41 | { |
||
| 42 | $result = []; |
||
| 43 | foreach ($value as $invitation) { |
||
| 44 | if ($invitation instanceof InvitationInterface) { |
||
| 45 | $result[] = $this->hydrator->extract($invitation); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | 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 InvitationInterface[] |
||
| 56 | * @throws BadMethodCallException for a non-object $contactObj |
||
| 57 | */ |
||
| 58 | public function hydrate($value, ?array $data): array |
||
| 59 | { |
||
| 60 | $result = []; |
||
| 61 | if (is_array($value)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 62 | foreach ($value as $invitationArr) { |
||
| 63 | $invitation = new Invitation(); |
||
| 64 | $this->hydrator->hydrate($invitationArr, $invitation); |
||
| 65 | $result[] = $invitation; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | return $result; |
||
| 69 | } |
||
| 70 | } |
||
| 71 |