summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/Mapping/Builder/ClassMetadataBuilder.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/orm/src/Mapping/Builder/ClassMetadataBuilder.php')
-rw-r--r--vendor/doctrine/orm/src/Mapping/Builder/ClassMetadataBuilder.php426
1 files changed, 426 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Mapping/Builder/ClassMetadataBuilder.php b/vendor/doctrine/orm/src/Mapping/Builder/ClassMetadataBuilder.php
new file mode 100644
index 0000000..b9d3cc8
--- /dev/null
+++ b/vendor/doctrine/orm/src/Mapping/Builder/ClassMetadataBuilder.php
@@ -0,0 +1,426 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\ORM\Mapping\Builder;
6
7use BackedEnum;
8use Doctrine\ORM\Mapping\ClassMetadata;
9
10/**
11 * Builder Object for ClassMetadata
12 *
13 * @link www.doctrine-project.com
14 */
15class ClassMetadataBuilder
16{
17 public function __construct(
18 private readonly ClassMetadata $cm,
19 ) {
20 }
21
22 public function getClassMetadata(): ClassMetadata
23 {
24 return $this->cm;
25 }
26
27 /**
28 * Marks the class as mapped superclass.
29 *
30 * @return $this
31 */
32 public function setMappedSuperClass(): static
33 {
34 $this->cm->isMappedSuperclass = true;
35 $this->cm->isEmbeddedClass = false;
36
37 return $this;
38 }
39
40 /**
41 * Marks the class as embeddable.
42 *
43 * @return $this
44 */
45 public function setEmbeddable(): static
46 {
47 $this->cm->isEmbeddedClass = true;
48 $this->cm->isMappedSuperclass = false;
49
50 return $this;
51 }
52
53 /**
54 * Adds and embedded class
55 *
56 * @param class-string $class
57 *
58 * @return $this
59 */
60 public function addEmbedded(string $fieldName, string $class, string|false|null $columnPrefix = null): static
61 {
62 $this->cm->mapEmbedded(
63 [
64 'fieldName' => $fieldName,
65 'class' => $class,
66 'columnPrefix' => $columnPrefix,
67 ],
68 );
69
70 return $this;
71 }
72
73 /**
74 * Sets custom Repository class name.
75 *
76 * @return $this
77 */
78 public function setCustomRepositoryClass(string $repositoryClassName): static
79 {
80 $this->cm->setCustomRepositoryClass($repositoryClassName);
81
82 return $this;
83 }
84
85 /**
86 * Marks class read only.
87 *
88 * @return $this
89 */
90 public function setReadOnly(): static
91 {
92 $this->cm->markReadOnly();
93
94 return $this;
95 }
96
97 /**
98 * Sets the table name.
99 *
100 * @return $this
101 */
102 public function setTable(string $name): static
103 {
104 $this->cm->setPrimaryTable(['name' => $name]);
105
106 return $this;
107 }
108
109 /**
110 * Adds Index.
111 *
112 * @psalm-param list<string> $columns
113 *
114 * @return $this
115 */
116 public function addIndex(array $columns, string $name): static
117 {
118 if (! isset($this->cm->table['indexes'])) {
119 $this->cm->table['indexes'] = [];
120 }
121
122 $this->cm->table['indexes'][$name] = ['columns' => $columns];
123
124 return $this;
125 }
126
127 /**
128 * Adds Unique Constraint.
129 *
130 * @psalm-param list<string> $columns
131 *
132 * @return $this
133 */
134 public function addUniqueConstraint(array $columns, string $name): static
135 {
136 if (! isset($this->cm->table['uniqueConstraints'])) {
137 $this->cm->table['uniqueConstraints'] = [];
138 }
139
140 $this->cm->table['uniqueConstraints'][$name] = ['columns' => $columns];
141
142 return $this;
143 }
144
145 /**
146 * Sets class as root of a joined table inheritance hierarchy.
147 *
148 * @return $this
149 */
150 public function setJoinedTableInheritance(): static
151 {
152 $this->cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_JOINED);
153
154 return $this;
155 }
156
157 /**
158 * Sets class as root of a single table inheritance hierarchy.
159 *
160 * @return $this
161 */
162 public function setSingleTableInheritance(): static
163 {
164 $this->cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE);
165
166 return $this;
167 }
168
169 /**
170 * Sets the discriminator column details.
171 *
172 * @psalm-param class-string<BackedEnum>|null $enumType
173 * @psalm-param array<string, mixed> $options
174 *
175 * @return $this
176 */
177 public function setDiscriminatorColumn(
178 string $name,
179 string $type = 'string',
180 int $length = 255,
181 string|null $columnDefinition = null,
182 string|null $enumType = null,
183 array $options = [],
184 ): static {
185 $this->cm->setDiscriminatorColumn(
186 [
187 'name' => $name,
188 'type' => $type,
189 'length' => $length,
190 'columnDefinition' => $columnDefinition,
191 'enumType' => $enumType,
192 'options' => $options,
193 ],
194 );
195
196 return $this;
197 }
198
199 /**
200 * Adds a subclass to this inheritance hierarchy.
201 *
202 * @return $this
203 */
204 public function addDiscriminatorMapClass(string $name, string $class): static
205 {
206 $this->cm->addDiscriminatorMapClass($name, $class);
207
208 return $this;
209 }
210
211 /**
212 * Sets deferred explicit change tracking policy.
213 *
214 * @return $this
215 */
216 public function setChangeTrackingPolicyDeferredExplicit(): static
217 {
218 $this->cm->setChangeTrackingPolicy(ClassMetadata::CHANGETRACKING_DEFERRED_EXPLICIT);
219
220 return $this;
221 }
222
223 /**
224 * Adds lifecycle event.
225 *
226 * @return $this
227 */
228 public function addLifecycleEvent(string $methodName, string $event): static
229 {
230 $this->cm->addLifecycleCallback($methodName, $event);
231
232 return $this;
233 }
234
235 /**
236 * Adds Field.
237 *
238 * @psalm-param array<string, mixed> $mapping
239 *
240 * @return $this
241 */
242 public function addField(string $name, string $type, array $mapping = []): static
243 {
244 $mapping['fieldName'] = $name;
245 $mapping['type'] = $type;
246
247 $this->cm->mapField($mapping);
248
249 return $this;
250 }
251
252 /**
253 * Creates a field builder.
254 */
255 public function createField(string $name, string $type): FieldBuilder
256 {
257 return new FieldBuilder(
258 $this,
259 [
260 'fieldName' => $name,
261 'type' => $type,
262 ],
263 );
264 }
265
266 /**
267 * Creates an embedded builder.
268 */
269 public function createEmbedded(string $fieldName, string $class): EmbeddedBuilder
270 {
271 return new EmbeddedBuilder(
272 $this,
273 [
274 'fieldName' => $fieldName,
275 'class' => $class,
276 'columnPrefix' => null,
277 ],
278 );
279 }
280
281 /**
282 * Adds a simple many to one association, optionally with the inversed by field.
283 */
284 public function addManyToOne(
285 string $name,
286 string $targetEntity,
287 string|null $inversedBy = null,
288 ): ClassMetadataBuilder {
289 $builder = $this->createManyToOne($name, $targetEntity);
290
291 if ($inversedBy !== null) {
292 $builder->inversedBy($inversedBy);
293 }
294
295 return $builder->build();
296 }
297
298 /**
299 * Creates a ManyToOne Association Builder.
300 *
301 * Note: This method does not add the association, you have to call build() on the AssociationBuilder.
302 */
303 public function createManyToOne(string $name, string $targetEntity): AssociationBuilder
304 {
305 return new AssociationBuilder(
306 $this,
307 [
308 'fieldName' => $name,
309 'targetEntity' => $targetEntity,
310 ],
311 ClassMetadata::MANY_TO_ONE,
312 );
313 }
314
315 /**
316 * Creates a OneToOne Association Builder.
317 */
318 public function createOneToOne(string $name, string $targetEntity): AssociationBuilder
319 {
320 return new AssociationBuilder(
321 $this,
322 [
323 'fieldName' => $name,
324 'targetEntity' => $targetEntity,
325 ],
326 ClassMetadata::ONE_TO_ONE,
327 );
328 }
329
330 /**
331 * Adds simple inverse one-to-one association.
332 */
333 public function addInverseOneToOne(string $name, string $targetEntity, string $mappedBy): ClassMetadataBuilder
334 {
335 $builder = $this->createOneToOne($name, $targetEntity);
336 $builder->mappedBy($mappedBy);
337
338 return $builder->build();
339 }
340
341 /**
342 * Adds simple owning one-to-one association.
343 */
344 public function addOwningOneToOne(
345 string $name,
346 string $targetEntity,
347 string|null $inversedBy = null,
348 ): ClassMetadataBuilder {
349 $builder = $this->createOneToOne($name, $targetEntity);
350
351 if ($inversedBy !== null) {
352 $builder->inversedBy($inversedBy);
353 }
354
355 return $builder->build();
356 }
357
358 /**
359 * Creates a ManyToMany Association Builder.
360 */
361 public function createManyToMany(string $name, string $targetEntity): ManyToManyAssociationBuilder
362 {
363 return new ManyToManyAssociationBuilder(
364 $this,
365 [
366 'fieldName' => $name,
367 'targetEntity' => $targetEntity,
368 ],
369 ClassMetadata::MANY_TO_MANY,
370 );
371 }
372
373 /**
374 * Adds a simple owning many to many association.
375 */
376 public function addOwningManyToMany(
377 string $name,
378 string $targetEntity,
379 string|null $inversedBy = null,
380 ): ClassMetadataBuilder {
381 $builder = $this->createManyToMany($name, $targetEntity);
382
383 if ($inversedBy !== null) {
384 $builder->inversedBy($inversedBy);
385 }
386
387 return $builder->build();
388 }
389
390 /**
391 * Adds a simple inverse many to many association.
392 */
393 public function addInverseManyToMany(string $name, string $targetEntity, string $mappedBy): ClassMetadataBuilder
394 {
395 $builder = $this->createManyToMany($name, $targetEntity);
396 $builder->mappedBy($mappedBy);
397
398 return $builder->build();
399 }
400
401 /**
402 * Creates a one to many association builder.
403 */
404 public function createOneToMany(string $name, string $targetEntity): OneToManyAssociationBuilder
405 {
406 return new OneToManyAssociationBuilder(
407 $this,
408 [
409 'fieldName' => $name,
410 'targetEntity' => $targetEntity,
411 ],
412 ClassMetadata::ONE_TO_MANY,
413 );
414 }
415
416 /**
417 * Adds simple OneToMany association.
418 */
419 public function addOneToMany(string $name, string $targetEntity, string $mappedBy): ClassMetadataBuilder
420 {
421 $builder = $this->createOneToMany($name, $targetEntity);
422 $builder->mappedBy($mappedBy);
423
424 return $builder->build();
425 }
426}