|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Mapping\Builder; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\Annotation; |
|
8
|
|
|
use Doctrine\ORM\Mapping; |
|
9
|
|
|
use function array_diff; |
|
10
|
|
|
use function array_intersect; |
|
11
|
|
|
use function array_map; |
|
12
|
|
|
use function class_exists; |
|
13
|
|
|
use function constant; |
|
14
|
|
|
use function count; |
|
15
|
|
|
use function defined; |
|
16
|
|
|
use function in_array; |
|
17
|
|
|
use function interface_exists; |
|
18
|
|
|
use function sprintf; |
|
19
|
|
|
|
|
20
|
|
|
abstract class AssociationMetadataBuilder |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var Mapping\ClassMetadataBuildingContext */ |
|
23
|
|
|
protected $metadataBuildingContext; |
|
24
|
|
|
|
|
25
|
|
|
/** @var CacheMetadataBuilder */ |
|
26
|
|
|
protected $cacheMetadataBuilder; |
|
27
|
|
|
|
|
28
|
|
|
/** @var Mapping\ClassMetadata */ |
|
29
|
|
|
protected $componentMetadata; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
protected $fieldName; |
|
33
|
|
|
|
|
34
|
|
|
/** @var Annotation\Cache|null */ |
|
35
|
|
|
protected $cacheAnnotation; |
|
36
|
|
|
|
|
37
|
268 |
|
public function __construct( |
|
38
|
|
|
Mapping\ClassMetadataBuildingContext $metadataBuildingContext, |
|
39
|
|
|
?CacheMetadataBuilder $cacheMetadataBuilder = null |
|
40
|
|
|
) { |
|
41
|
268 |
|
$this->metadataBuildingContext = $metadataBuildingContext; |
|
42
|
268 |
|
$this->cacheMetadataBuilder = $cacheMetadataBuilder ?: new CacheMetadataBuilder($metadataBuildingContext); |
|
43
|
268 |
|
} |
|
44
|
|
|
|
|
45
|
268 |
|
public function withComponentMetadata(Mapping\ClassMetadata $componentMetadata) : AssociationMetadataBuilder |
|
46
|
|
|
{ |
|
47
|
268 |
|
$this->componentMetadata = $componentMetadata; |
|
48
|
|
|
|
|
49
|
268 |
|
$this->cacheMetadataBuilder->withComponentMetadata($componentMetadata); |
|
50
|
|
|
|
|
51
|
268 |
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
268 |
|
public function withFieldName(string $fieldName) : AssociationMetadataBuilder |
|
55
|
|
|
{ |
|
56
|
268 |
|
$this->fieldName = $fieldName; |
|
57
|
|
|
|
|
58
|
268 |
|
$this->cacheMetadataBuilder->withFieldName($fieldName); |
|
59
|
|
|
|
|
60
|
268 |
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
268 |
|
public function withCacheAnnotation(?Annotation\Cache $cacheAnnotation) : AssociationMetadataBuilder |
|
64
|
|
|
{ |
|
65
|
268 |
|
$this->cacheAnnotation = $cacheAnnotation; |
|
66
|
|
|
|
|
67
|
268 |
|
if ($cacheAnnotation !== null) { |
|
68
|
15 |
|
$this->cacheMetadataBuilder->withCacheAnnotation($cacheAnnotation); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
268 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
268 |
|
protected function buildCache(Mapping\AssociationMetadata $associationMetadata) : void |
|
75
|
|
|
{ |
|
76
|
268 |
|
if ($this->cacheAnnotation !== null) { |
|
77
|
15 |
|
$associationMetadata->setCache($this->cacheMetadataBuilder->build()); |
|
78
|
|
|
} |
|
79
|
268 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Attempts to resolve target entity. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $targetEntity The proposed target entity |
|
85
|
|
|
* |
|
86
|
|
|
* @return string The processed target entity |
|
87
|
|
|
* |
|
88
|
|
|
* @throws Mapping\MappingException If a target entity is not valid. |
|
89
|
|
|
*/ |
|
90
|
268 |
|
protected function getTargetEntity(string $targetEntity) : string |
|
91
|
|
|
{ |
|
92
|
|
|
// Validate if target entity is defined |
|
93
|
268 |
|
if (! $targetEntity) { |
|
94
|
|
|
throw Mapping\MappingException::missingTargetEntity($this->fieldName); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Validate that target entity exists |
|
98
|
268 |
|
if (! (class_exists($targetEntity) || interface_exists($targetEntity))) { |
|
99
|
|
|
throw Mapping\MappingException::invalidTargetEntityClass( |
|
100
|
|
|
$targetEntity, |
|
101
|
|
|
$this->componentMetadata->getClassName(), |
|
102
|
|
|
$this->fieldName |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
268 |
|
return $targetEntity; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Attempts to resolve the cascade modes. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string[] $originalCascades The original unprocessed field cascades. |
|
113
|
|
|
* |
|
114
|
|
|
* @return string[] The processed field cascades. |
|
115
|
|
|
* |
|
116
|
|
|
* @throws Mapping\MappingException If a cascade option is not valid. |
|
117
|
|
|
*/ |
|
118
|
268 |
|
protected function getCascade(array $originalCascades) : array |
|
119
|
|
|
{ |
|
120
|
268 |
|
$cascadeTypes = ['remove', 'persist', 'refresh']; |
|
121
|
268 |
|
$cascades = array_map('strtolower', $originalCascades); |
|
122
|
|
|
|
|
123
|
268 |
|
if (in_array('all', $cascades, true)) { |
|
124
|
29 |
|
$cascades = $cascadeTypes; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
268 |
|
if (count($cascades) !== count(array_intersect($cascades, $cascadeTypes))) { |
|
128
|
|
|
$diffCascades = array_diff($cascades, array_intersect($cascades, $cascadeTypes)); |
|
129
|
|
|
|
|
130
|
|
|
throw Mapping\MappingException::invalidCascadeOption( |
|
131
|
|
|
$diffCascades, |
|
132
|
|
|
$this->componentMetadata->getClassName(), |
|
133
|
|
|
$this->fieldName |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
268 |
|
return $cascades; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Attempts to resolve the fetch mode. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $fetchMode The fetch mode. |
|
144
|
|
|
* |
|
145
|
|
|
* @return string The fetch mode as defined in ClassMetadata. |
|
146
|
|
|
* |
|
147
|
|
|
* @throws Mapping\MappingException If the fetch mode is not valid. |
|
148
|
|
|
*/ |
|
149
|
268 |
|
protected function getFetchMode($fetchMode) : string |
|
150
|
|
|
{ |
|
151
|
268 |
|
$fetchModeConstant = sprintf('%s::%s', Mapping\FetchMode::class, $fetchMode); |
|
152
|
|
|
|
|
153
|
268 |
|
if (! defined($fetchModeConstant)) { |
|
154
|
|
|
throw Mapping\MappingException::invalidFetchMode($this->componentMetadata->getClassName(), $fetchMode); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
268 |
|
return constant($fetchModeConstant); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|