1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Cycle\ORM\Relation\Morphed; |
11
|
|
|
|
12
|
|
|
use Cycle\ORM\Command\CommandInterface; |
13
|
|
|
use Cycle\ORM\Command\ContextCarrierInterface as CC; |
14
|
|
|
use Cycle\ORM\Exception\RelationException; |
15
|
|
|
use Cycle\ORM\Heap\Node; |
16
|
|
|
use Cycle\ORM\ORMInterface; |
17
|
|
|
use Cycle\ORM\Relation; |
18
|
|
|
use Cycle\ORM\Relation\BelongsTo; |
19
|
|
|
|
20
|
|
|
class BelongsToMorphed extends BelongsTo |
21
|
|
|
{ |
22
|
|
|
/** @var string */ |
23
|
|
|
private $morphKey; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param ORMInterface $orm |
27
|
|
|
* @param string $target |
28
|
|
|
* @param string $name |
29
|
|
|
* @param array $schema |
30
|
|
|
*/ |
31
|
|
|
public function __construct(ORMInterface $orm, string $name, string $target, array $schema) |
32
|
|
|
{ |
33
|
|
|
parent::__construct($orm, $name, $target, $schema); |
34
|
|
|
$this->morphKey = $schema[Relation::MORPH_KEY]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
|
|
public function initPromise(Node $parentNode): array |
41
|
|
|
{ |
42
|
|
|
if (is_null($innerKey = $this->fetchKey($parentNode, $this->innerKey))) { |
43
|
|
|
return [null, null]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** @var string $target */ |
47
|
|
|
$target = $this->fetchKey($parentNode, $this->morphKey); |
48
|
|
|
if (is_null($target)) { |
|
|
|
|
49
|
|
|
return [null, null]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$r = $this->orm->promise($target, [$this->outerKey => $innerKey]); |
|
|
|
|
53
|
|
|
|
54
|
|
|
return [$r, $r]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public function queue(CC $parentStore, $parentEntity, Node $parentNode, $related, $original): CommandInterface |
61
|
|
|
{ |
62
|
|
|
$wrappedStore = parent::queue($parentStore, $parentEntity, $parentNode, $related, $original); |
63
|
|
|
|
64
|
|
|
if (is_null($related)) { |
65
|
|
|
if ($this->fetchKey($parentNode, $this->morphKey) !== null) { |
66
|
|
|
$parentStore->register($this->morphKey, null, true); |
67
|
|
|
$parentNode->register($this->morphKey, null, true); |
68
|
|
|
} |
69
|
|
|
} else { |
70
|
|
|
$relState = $this->getNode($related); |
71
|
|
|
if ($this->fetchKey($parentNode, $this->morphKey) != $relState->getRole()) { |
72
|
|
|
$parentStore->register($this->morphKey, $relState->getRole(), true); |
73
|
|
|
$parentNode->register($this->morphKey, $relState->getRole(), true); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $wrappedStore; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Assert that given entity is allowed for the relation. |
82
|
|
|
* |
83
|
|
|
* @param Node $relNode |
84
|
|
|
* |
85
|
|
|
* @throws RelationException |
86
|
|
|
*/ |
87
|
|
|
protected function assertValid(Node $relNode) |
88
|
|
|
{ |
89
|
|
|
// no need to validate morphed relation yet |
90
|
|
|
} |
91
|
|
|
} |