-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathAssociationMapping.php
365 lines (309 loc) · 11.6 KB
/
AssociationMapping.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
declare(strict_types=1);
namespace Doctrine\ORM\Mapping;
use ArrayAccess;
use Exception;
use OutOfRangeException;
use function assert;
use function count;
use function in_array;
use function property_exists;
use function sprintf;
/** @template-implements ArrayAccess<string, mixed> */
abstract class AssociationMapping implements ArrayAccess
{
/**
* The names of persistence operations to cascade on the association.
*
* @var list<'persist'|'remove'|'detach'|'refresh'|'all'>
*/
public array $cascade = [];
/**
* The fetching strategy to use for the association, usually defaults to FETCH_LAZY.
*
* @var ClassMetadata::FETCH_*|null
*/
public int|null $fetch = null;
/**
* This is set when the association is inherited by this class from another
* (inheritance) parent <em>entity</em> class. The value is the FQCN of the
* topmost entity class that contains this association. (If there are
* transient classes in the class hierarchy, these are ignored, so the
* class property may in fact come from a class further up in the PHP class
* hierarchy.) To-many associations initially declared in mapped
* superclasses are <em>not</em> considered 'inherited' in the nearest
* entity subclasses.
*
* @var class-string|null
*/
public string|null $inherited = null;
/**
* This is set when the association does not appear in the current class
* for the first time, but is initially declared in another parent
* <em>entity or mapped superclass</em>. The value is the FQCN of the
* topmost non-transient class that contains association information for
* this relationship.
*
* @var class-string|null
*/
public string|null $declared = null;
public array|null $cache = null;
public bool|null $id = null;
public bool|null $isOnDeleteCascade = null;
/** @var class-string|null */
public string|null $originalClass = null;
public string|null $originalField = null;
public bool $orphanRemoval = false;
public bool|null $unique = null;
/**
* @param string $fieldName The name of the field in the entity
* the association is mapped to.
* @param class-string $sourceEntity The class name of the source entity.
* In the case of to-many associations
* initially present in mapped
* superclasses, the nearest
* <em>entity</em> subclasses will be
* considered the respective source
* entities.
* @param class-string $targetEntity The class name of the target entity.
* If it is fully-qualified it is used as
* is. If it is a simple, unqualified
* class name the namespace is assumed to
* be the same as the namespace of the
* source entity.
*/
final public function __construct(
public readonly string $fieldName,
public string $sourceEntity,
public readonly string $targetEntity,
) {
}
/**
* @param mixed[] $mappingArray
* @phpstan-param array{
* fieldName: string,
* sourceEntity: class-string,
* targetEntity: class-string,
* cascade?: list<'persist'|'remove'|'detach'|'refresh'|'all'>,
* fetch?: ClassMetadata::FETCH_*|null,
* inherited?: class-string|null,
* declared?: class-string|null,
* cache?: array<mixed>|null,
* id?: bool|null,
* isOnDeleteCascade?: bool|null,
* originalClass?: class-string|null,
* originalField?: string|null,
* orphanRemoval?: bool,
* unique?: bool|null,
* joinTable?: mixed[]|null,
* type?: int,
* isOwningSide: bool,
* } $mappingArray
*/
public static function fromMappingArray(array $mappingArray): static
{
unset($mappingArray['isOwningSide'], $mappingArray['type']);
$mapping = new static(
$mappingArray['fieldName'],
$mappingArray['sourceEntity'],
$mappingArray['targetEntity'],
);
unset($mappingArray['fieldName'], $mappingArray['sourceEntity'], $mappingArray['targetEntity']);
foreach ($mappingArray as $key => $value) {
if ($key === 'joinTable') {
assert($mapping instanceof ManyToManyAssociationMapping);
if ($value === [] || $value === null) {
continue;
}
if (! $mapping instanceof ManyToManyOwningSideMapping) {
throw new MappingException(
"Mapping error on field '" .
$mapping->fieldName . "' in " . $mapping->sourceEntity .
" : 'joinTable' can only be set on many-to-many owning side.",
);
}
$mapping->joinTable = JoinTableMapping::fromMappingArray($value);
continue;
}
if (property_exists($mapping, $key)) {
$mapping->$key = $value;
} else {
throw new OutOfRangeException('Unknown property ' . $key . ' on class ' . static::class);
}
}
return $mapping;
}
/**
* @phpstan-assert-if-true OwningSideMapping $this
* @phpstan-assert-if-false InverseSideMapping $this
*/
final public function isOwningSide(): bool
{
return $this instanceof OwningSideMapping;
}
/** @phpstan-assert-if-true ToOneAssociationMapping $this */
final public function isToOne(): bool
{
return $this instanceof ToOneAssociationMapping;
}
/** @phpstan-assert-if-true ToManyAssociationMapping $this */
final public function isToMany(): bool
{
return $this instanceof ToManyAssociationMapping;
}
/** @phpstan-assert-if-true OneToOneOwningSideMapping $this */
final public function isOneToOneOwningSide(): bool
{
return $this->isOneToOne() && $this->isOwningSide();
}
/** @phpstan-assert-if-true OneToOneOwningSideMapping|ManyToOneAssociationMapping $this */
final public function isToOneOwningSide(): bool
{
return $this->isToOne() && $this->isOwningSide();
}
/** @phpstan-assert-if-true ManyToManyOwningSideMapping $this */
final public function isManyToManyOwningSide(): bool
{
return $this instanceof ManyToManyOwningSideMapping;
}
/** @phpstan-assert-if-true OneToOneAssociationMapping $this */
final public function isOneToOne(): bool
{
return $this instanceof OneToOneAssociationMapping;
}
/** @phpstan-assert-if-true OneToManyAssociationMapping $this */
final public function isOneToMany(): bool
{
return $this instanceof OneToManyAssociationMapping;
}
/** @phpstan-assert-if-true ManyToOneAssociationMapping $this */
final public function isManyToOne(): bool
{
return $this instanceof ManyToOneAssociationMapping;
}
/** @phpstan-assert-if-true ManyToManyAssociationMapping $this */
final public function isManyToMany(): bool
{
return $this instanceof ManyToManyAssociationMapping;
}
/** @phpstan-assert-if-true ToManyAssociationMapping $this */
final public function isOrdered(): bool
{
return $this->isToMany() && $this->orderBy() !== [];
}
/** @phpstan-assert-if-true ToManyAssociationMapping $this */
public function isIndexed(): bool
{
return false;
}
final public function type(): int
{
return match (true) {
$this instanceof OneToOneAssociationMapping => ClassMetadata::ONE_TO_ONE,
$this instanceof OneToManyAssociationMapping => ClassMetadata::ONE_TO_MANY,
$this instanceof ManyToOneAssociationMapping => ClassMetadata::MANY_TO_ONE,
$this instanceof ManyToManyAssociationMapping => ClassMetadata::MANY_TO_MANY,
default => throw new Exception('Cannot determine type for ' . static::class),
};
}
/** @param string $offset */
public function offsetExists(mixed $offset): bool
{
return isset($this->$offset) || in_array($offset, ['isOwningSide', 'type'], true);
}
final public function offsetGet(mixed $offset): mixed
{
return match ($offset) {
'isOwningSide' => $this->isOwningSide(),
'type' => $this->type(),
'isCascadeRemove' => $this->isCascadeRemove(),
'isCascadePersist' => $this->isCascadePersist(),
'isCascadeRefresh' => $this->isCascadeRefresh(),
'isCascadeDetach' => $this->isCascadeDetach(),
default => property_exists($this, $offset) ? $this->$offset : throw new OutOfRangeException(sprintf(
'Unknown property "%s" on class %s',
$offset,
static::class,
)),
};
}
public function offsetSet(mixed $offset, mixed $value): void
{
assert($offset !== null);
if (! property_exists($this, $offset)) {
throw new OutOfRangeException(sprintf(
'Unknown property "%s" on class %s',
$offset,
static::class,
));
}
if ($offset === 'joinTable') {
$value = JoinTableMapping::fromMappingArray($value);
}
$this->$offset = $value;
}
/** @param string $offset */
public function offsetUnset(mixed $offset): void
{
if (! property_exists($this, $offset)) {
throw new OutOfRangeException(sprintf(
'Unknown property "%s" on class %s',
$offset,
static::class,
));
}
$this->$offset = null;
}
final public function isCascadeRemove(): bool
{
return in_array('remove', $this->cascade, true);
}
final public function isCascadePersist(): bool
{
return in_array('persist', $this->cascade, true);
}
final public function isCascadeRefresh(): bool
{
return in_array('refresh', $this->cascade, true);
}
final public function isCascadeDetach(): bool
{
return in_array('detach', $this->cascade, true);
}
/** @return array<string, mixed> */
public function toArray(): array
{
$array = (array) $this;
$array['isOwningSide'] = $this->isOwningSide();
$array['type'] = $this->type();
return $array;
}
/** @return list<string> */
public function __sleep(): array
{
$serialized = ['fieldName', 'sourceEntity', 'targetEntity'];
if (count($this->cascade) > 0) {
$serialized[] = 'cascade';
}
foreach (
[
'fetch',
'inherited',
'declared',
'cache',
'originalClass',
'originalField',
] as $stringOrArrayProperty
) {
if ($this->$stringOrArrayProperty !== null) {
$serialized[] = $stringOrArrayProperty;
}
}
foreach (['id', 'orphanRemoval', 'isOnDeleteCascade', 'unique'] as $boolProperty) {
if ($this->$boolProperty) {
$serialized[] = $boolProperty;
}
}
return $serialized;
}
}