|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bug14429; |
| 4 | + |
| 5 | +interface IEntity { |
| 6 | + /** |
| 7 | + * @return IRepository<IEntity> |
| 8 | + */ |
| 9 | + public function getRepository(): IRepository; |
| 10 | +} |
| 11 | + |
| 12 | +interface IProperty {} |
| 13 | + |
| 14 | +interface IPropertyContainer extends IProperty {} |
| 15 | + |
| 16 | +/** |
| 17 | + * @template E of IEntity |
| 18 | + */ |
| 19 | +interface IEntityAwareProperty extends IProperty {} |
| 20 | + |
| 21 | +/** |
| 22 | + * @template E of IEntity |
| 23 | + * @extends IEntityAwareProperty<E> |
| 24 | + */ |
| 25 | +interface IRelationshipContainer extends IPropertyContainer, IEntityAwareProperty {} |
| 26 | + |
| 27 | +interface IModel { |
| 28 | + /** |
| 29 | + * @template E of IEntity |
| 30 | + * @template T of IRepository<E> |
| 31 | + * @param class-string<T> $className |
| 32 | + * @return T |
| 33 | + */ |
| 34 | + public function getRepository(string $className): IRepository; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * @template E of IEntity |
| 39 | + */ |
| 40 | +interface IRepository { |
| 41 | + public function getModel(): IModel; |
| 42 | +} |
| 43 | + |
| 44 | +class PropertyRelationshipMetadata { |
| 45 | + /** @var class-string<IRepository<IEntity>> */ |
| 46 | + public string $repository; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * @template E of IEntity |
| 51 | + * @implements IRelationshipContainer<E> |
| 52 | + */ |
| 53 | +class HasOne implements IRelationshipContainer |
| 54 | +{ |
| 55 | + /** @var E|null */ |
| 56 | + protected ?IEntity $parent = null; |
| 57 | + |
| 58 | + /** @var IRepository<E>|null */ |
| 59 | + protected ?IRepository $targetRepository = null; |
| 60 | + |
| 61 | + protected PropertyRelationshipMetadata $metadataRelationship; |
| 62 | + |
| 63 | + /** |
| 64 | + * @return E |
| 65 | + */ |
| 66 | + protected function getParentEntity(): IEntity |
| 67 | + { |
| 68 | + return $this->parent ?? throw new \InvalidArgumentException('Relationship is not attached to a parent entity.'); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return IRepository<E> |
| 73 | + */ |
| 74 | + protected function getTargetRepository(): IRepository |
| 75 | + { |
| 76 | + if ($this->targetRepository === null) { |
| 77 | + /** @var IRepository<E> $targetRepository */ |
| 78 | + $targetRepository = $this->getParentEntity() |
| 79 | + ->getRepository() |
| 80 | + ->getModel() |
| 81 | + ->getRepository($this->metadataRelationship->repository); |
| 82 | + $this->targetRepository = $targetRepository; |
| 83 | + } |
| 84 | + |
| 85 | + return $this->targetRepository; |
| 86 | + } |
| 87 | +} |
0 commit comments