|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Bug6591; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +interface HydratorInterface { |
| 8 | + /** |
| 9 | + * @return array<string, mixed> |
| 10 | + */ |
| 11 | + public function extract(object $object): array; |
| 12 | +} |
| 13 | + |
| 14 | +interface EntityInterface { |
| 15 | + public const IDENTITY = 'identity'; |
| 16 | + public const CREATED = 'created'; |
| 17 | + public function getIdentity(): string; |
| 18 | + public function getCreated(): \DateTimeImmutable; |
| 19 | +} |
| 20 | +interface UpdatableInterface extends EntityInterface { |
| 21 | + public const UPDATED = 'updated'; |
| 22 | + public function getUpdated(): \DateTimeImmutable; |
| 23 | + public function setUpdated(\DateTimeImmutable $updated): void; |
| 24 | +} |
| 25 | +interface EnableableInterface extends UpdatableInterface { |
| 26 | + public const ENABLED = 'enabled'; |
| 27 | + public function isEnabled(): bool; |
| 28 | + public function setEnabled(bool $enabled): void; |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +/** |
| 33 | + * @template T of EntityInterface |
| 34 | + */ |
| 35 | +class DoctrineEntityHydrator implements HydratorInterface |
| 36 | +{ |
| 37 | + /** @param T $object */ |
| 38 | + public function extract(object $object): array |
| 39 | + { |
| 40 | + $data = [ |
| 41 | + EntityInterface::IDENTITY => $object->getIdentity(), |
| 42 | + EntityInterface::CREATED => $object->getCreated()->format('c'), |
| 43 | + ]; |
| 44 | + assertType('T of Bug6591\EntityInterface (class Bug6591\DoctrineEntityHydrator, argument)', $object); |
| 45 | + if ($object instanceof UpdatableInterface) { |
| 46 | + assertType('Bug6591\UpdatableInterface&T of Bug6591\EntityInterface (class Bug6591\DoctrineEntityHydrator, argument)', $object); |
| 47 | + $data[UpdatableInterface::UPDATED] = $object->getUpdated()->format('c'); |
| 48 | + } else { |
| 49 | + assertType('T of Bug6591\EntityInterface~Bug6591\UpdatableInterface (class Bug6591\DoctrineEntityHydrator, argument)', $object); |
| 50 | + } |
| 51 | + |
| 52 | + assertType('T of Bug6591\EntityInterface (class Bug6591\DoctrineEntityHydrator, argument)', $object); |
| 53 | + |
| 54 | + if ($object instanceof EnableableInterface) { |
| 55 | + assertType('Bug6591\EnableableInterface&T of Bug6591\EntityInterface (class Bug6591\DoctrineEntityHydrator, argument)', $object); |
| 56 | + $data[EnableableInterface::ENABLED] = $object->isEnabled(); |
| 57 | + } else { |
| 58 | + assertType('T of Bug6591\EntityInterface~Bug6591\EnableableInterface (class Bug6591\DoctrineEntityHydrator, argument)', $object); |
| 59 | + } |
| 60 | + |
| 61 | + assertType('T of Bug6591\EntityInterface (class Bug6591\DoctrineEntityHydrator, argument)', $object); |
| 62 | + |
| 63 | + return [...$data, ...$this->performExtraction($object)]; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param T $entity |
| 68 | + * @return array<string, mixed> |
| 69 | + */ |
| 70 | + public function performExtraction(EntityInterface $entity): array |
| 71 | + { |
| 72 | + return []; |
| 73 | + } |
| 74 | +} |
0 commit comments