1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Cycle DataMapper ORM |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\ORM\Relation; |
13
|
|
|
|
14
|
|
|
use Cycle\ORM\Exception\RelationException; |
15
|
|
|
use Cycle\ORM\Heap\Node; |
16
|
|
|
use Cycle\ORM\MapperInterface; |
17
|
|
|
use Cycle\ORM\ORMInterface; |
18
|
|
|
use Cycle\ORM\Promise\PromiseInterface; |
19
|
|
|
use Cycle\ORM\Promise\ReferenceInterface; |
20
|
|
|
use Cycle\ORM\Relation; |
21
|
|
|
use Cycle\ORM\Schema; |
22
|
|
|
use Cycle\ORM\Select\SourceInterface; |
23
|
|
|
use Cycle\ORM\Select\SourceProviderInterface; |
24
|
|
|
|
25
|
|
|
abstract class AbstractRelation implements RelationInterface |
26
|
|
|
{ |
27
|
|
|
use Traits\ContextTrait; |
28
|
|
|
use Relation\Traits\NodeTrait; |
29
|
|
|
|
30
|
|
|
/** @var ORMInterface|SourceProviderInterface @internal */ |
31
|
|
|
protected $orm; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
protected $name; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
protected $target; |
38
|
|
|
|
39
|
|
|
/** @var array */ |
40
|
|
|
protected $schema; |
41
|
|
|
|
42
|
|
|
/** @var string */ |
43
|
|
|
protected $innerKey; |
44
|
|
|
|
45
|
|
|
/** @var string */ |
46
|
|
|
protected $outerKey; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param ORMInterface $orm |
50
|
|
|
* @param string $name |
51
|
|
|
* @param string $target |
52
|
|
|
* @param array $schema |
53
|
|
|
*/ |
54
|
|
|
public function __construct(ORMInterface $orm, string $name, string $target, array $schema) |
55
|
|
|
{ |
56
|
|
|
$this->orm = $orm; |
57
|
|
|
$this->name = $name; |
58
|
|
|
$this->target = $target; |
59
|
|
|
$this->schema = $schema; |
60
|
|
|
$this->innerKey = $schema[Relation::INNER_KEY]; |
61
|
|
|
$this->outerKey = $schema[Relation::OUTER_KEY]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function __toString() |
68
|
|
|
{ |
69
|
|
|
// this is incorrect class |
70
|
|
|
return sprintf('%s(%s)->%s', $this->name, get_class($this), $this->target); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
|
|
public function getName(): string |
77
|
|
|
{ |
78
|
|
|
return $this->name; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getTarget(): string |
85
|
|
|
{ |
86
|
|
|
return $this->target; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
|
|
public function isCascade(): bool |
93
|
|
|
{ |
94
|
|
|
return $this->schema[Relation::CASCADE] ?? false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
|
|
public function init(Node $node, array $data): array |
101
|
|
|
{ |
102
|
|
|
$item = $this->orm->make($this->target, $data, Node::MANAGED); |
|
|
|
|
103
|
|
|
|
104
|
|
|
return [$item, $item]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritdoc |
109
|
|
|
*/ |
110
|
|
|
public function extract($data) |
111
|
|
|
{ |
112
|
|
|
return $data; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritDoc |
117
|
|
|
*/ |
118
|
|
|
protected function isNullable(): bool |
119
|
|
|
{ |
120
|
|
|
return !empty($this->schema[Relation::NULLABLE]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the source associated with the role. |
125
|
|
|
* |
126
|
|
|
* @param string|null $role |
127
|
|
|
* @return SourceInterface |
128
|
|
|
*/ |
129
|
|
|
protected function getSource(string $role = null): SourceInterface |
130
|
|
|
{ |
131
|
|
|
return $this->orm->getSource($role ?? $this->target); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the mapper associated with a role. |
136
|
|
|
* |
137
|
|
|
* @param string|null $role |
138
|
|
|
* @return MapperInterface |
139
|
|
|
*/ |
140
|
|
|
protected function getMapper(string $role = null): MapperInterface |
141
|
|
|
{ |
142
|
|
|
return $this->orm->getMapper($role ?? $this->target); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param Node $node |
147
|
|
|
* @param string $field |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
protected function columnName(Node $node, string $field): string |
151
|
|
|
{ |
152
|
|
|
return $this->orm->getSchema()->define($node->getRole(), Schema::COLUMNS)[$field] ?? $field; |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Assert that given entity is allowed for the relation. |
157
|
|
|
* |
158
|
|
|
* @param Node $related |
159
|
|
|
* |
160
|
|
|
* @throws RelationException |
161
|
|
|
*/ |
162
|
|
|
protected function assertValid(Node $related): void |
163
|
|
|
{ |
164
|
|
|
if ($related->getRole() != $this->target) { |
165
|
|
|
throw new RelationException(sprintf('Unable to link %s, given `%s`', $this, $related->getRole())); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Resolve the reference to the object. |
171
|
|
|
* |
172
|
|
|
* @param ReferenceInterface $reference |
173
|
|
|
* @return mixed|null |
174
|
|
|
*/ |
175
|
|
|
protected function resolve(ReferenceInterface $reference) |
176
|
|
|
{ |
177
|
|
|
if ($reference instanceof PromiseInterface) { |
178
|
|
|
return $reference->__resolve(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this->orm->get($reference->__role(), $reference->__scope(), true); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|