|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Cycle\ORM\Relation\Morphed; |
| 6 | + |
| 7 | +use Cycle\ORM\Exception\RelationException; |
| 8 | +use Cycle\ORM\Heap\Node; |
| 9 | +use Cycle\ORM\ORMInterface; |
| 10 | +use Cycle\ORM\Reference\EmptyReference; |
| 11 | +use Cycle\ORM\Reference\Reference; |
| 12 | +use Cycle\ORM\Reference\ReferenceInterface; |
| 13 | +use Cycle\ORM\Relation; |
| 14 | +use Cycle\ORM\Relation\RefersTo; |
| 15 | +use Cycle\ORM\Transaction\Pool; |
| 16 | +use Cycle\ORM\Transaction\Tuple; |
| 17 | + |
| 18 | +/** |
| 19 | + * Morphed variation of the {@see RefersTo} relation. Like {@see BelongsToMorphed} it stores |
| 20 | + * both the outer key and the target role (morph key) on the owner, but inherits the deferred, |
| 21 | + * "soft" dependency resolution of {@see RefersTo}. This allows the relation to be self linked |
| 22 | + * and to participate in cyclic references (A > A, A > B > A) that {@see BelongsToMorphed} can |
| 23 | + * not persist in a single transaction. |
| 24 | + * |
| 25 | + * @internal |
| 26 | + */ |
| 27 | +class RefersToMorphed extends RefersTo |
| 28 | +{ |
| 29 | + private string $morphKey; |
| 30 | + |
| 31 | + public function __construct(ORMInterface $orm, string $role, string $name, string $target, array $schema) |
| 32 | + { |
| 33 | + parent::__construct($orm, $role, $name, $target, $schema); |
| 34 | + $this->morphKey = $schema[Relation::MORPH_KEY]; |
| 35 | + } |
| 36 | + |
| 37 | + public function initReference(Node $node): ReferenceInterface |
| 38 | + { |
| 39 | + $scope = $this->getReferenceScope($node); |
| 40 | + $nodeData = $node->getData(); |
| 41 | + if (!isset($nodeData[$this->morphKey], $scope)) { |
| 42 | + return new EmptyReference('?', null); |
| 43 | + } |
| 44 | + $target = $nodeData[$this->morphKey]; |
| 45 | + |
| 46 | + return $scope === [] ? new EmptyReference($target, null) : new Reference($target, $scope); |
| 47 | + } |
| 48 | + |
| 49 | + public function prepare(Pool $pool, Tuple $tuple, mixed $related, bool $load = true): void |
| 50 | + { |
| 51 | + // The parent RefersTo resets the node relation while handling a null value, so capture |
| 52 | + // whether there was a related entity before delegating. |
| 53 | + $hadRelation = $tuple->node->getRelation($this->getName()) !== null; |
| 54 | + parent::prepare($pool, $tuple, $related, $load); |
| 55 | + $this->syncMorphKey($pool, $tuple, $hadRelation); |
| 56 | + } |
| 57 | + |
| 58 | + public function queue(Pool $pool, Tuple $tuple): void |
| 59 | + { |
| 60 | + $hadRelation = $tuple->node->getRelation($this->getName()) !== null; |
| 61 | + parent::queue($pool, $tuple); |
| 62 | + $this->syncMorphKey($pool, $tuple, $hadRelation); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Assert that given entity is allowed for the relation. |
| 67 | + * |
| 68 | + * @throws RelationException |
| 69 | + */ |
| 70 | + protected function assertValid(Node $related): void |
| 71 | + { |
| 72 | + // no need to validate morphed relation yet |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Keep the morph key in sync with the related entity role. The role is known as soon as the |
| 77 | + * related object is available, so it can be registered eagerly even while the outer key is |
| 78 | + * still deferred by the parent {@see RefersTo} logic. |
| 79 | + */ |
| 80 | + private function syncMorphKey(Pool $pool, Tuple $tuple, bool $hadRelation): void |
| 81 | + { |
| 82 | + $relName = $this->getName(); |
| 83 | + $state = $tuple->state; |
| 84 | + if (!$state->hasRelation($relName)) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + $related = $state->getRelation($relName); |
| 89 | + |
| 90 | + if ($related === null) { |
| 91 | + // Reset the morph key when the relation was changed to null |
| 92 | + if ($hadRelation) { |
| 93 | + $state->register($this->morphKey, null); |
| 94 | + } |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + if ($related instanceof EmptyReference) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + $role = $related instanceof ReferenceInterface |
| 103 | + ? $related->getRole() |
| 104 | + : $pool->offsetGet($related)?->node->getRole(); |
| 105 | + |
| 106 | + $state->register($this->morphKey, $role); |
| 107 | + } |
| 108 | +} |
0 commit comments