summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/Mapping/Builder/AssociationBuilder.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/orm/src/Mapping/Builder/AssociationBuilder.php')
-rw-r--r--vendor/doctrine/orm/src/Mapping/Builder/AssociationBuilder.php171
1 files changed, 171 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Mapping/Builder/AssociationBuilder.php b/vendor/doctrine/orm/src/Mapping/Builder/AssociationBuilder.php
new file mode 100644
index 0000000..ea9e13c
--- /dev/null
+++ b/vendor/doctrine/orm/src/Mapping/Builder/AssociationBuilder.php
@@ -0,0 +1,171 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\ORM\Mapping\Builder;
6
7use Doctrine\ORM\Mapping\ClassMetadata;
8use InvalidArgumentException;
9
10class AssociationBuilder
11{
12 /** @var mixed[]|null */
13 protected array|null $joinColumns = null;
14
15 /** @param mixed[] $mapping */
16 public function __construct(
17 protected readonly ClassMetadataBuilder $builder,
18 protected array $mapping,
19 protected readonly int $type,
20 ) {
21 }
22
23 /** @return $this */
24 public function mappedBy(string $fieldName): static
25 {
26 $this->mapping['mappedBy'] = $fieldName;
27
28 return $this;
29 }
30
31 /** @return $this */
32 public function inversedBy(string $fieldName): static
33 {
34 $this->mapping['inversedBy'] = $fieldName;
35
36 return $this;
37 }
38
39 /** @return $this */
40 public function cascadeAll(): static
41 {
42 $this->mapping['cascade'] = ['ALL'];
43
44 return $this;
45 }
46
47 /** @return $this */
48 public function cascadePersist(): static
49 {
50 $this->mapping['cascade'][] = 'persist';
51
52 return $this;
53 }
54
55 /** @return $this */
56 public function cascadeRemove(): static
57 {
58 $this->mapping['cascade'][] = 'remove';
59
60 return $this;
61 }
62
63 /** @return $this */
64 public function cascadeDetach(): static
65 {
66 $this->mapping['cascade'][] = 'detach';
67
68 return $this;
69 }
70
71 /** @return $this */
72 public function cascadeRefresh(): static
73 {
74 $this->mapping['cascade'][] = 'refresh';
75
76 return $this;
77 }
78
79 /** @return $this */
80 public function fetchExtraLazy(): static
81 {
82 $this->mapping['fetch'] = ClassMetadata::FETCH_EXTRA_LAZY;
83
84 return $this;
85 }
86
87 /** @return $this */
88 public function fetchEager(): static
89 {
90 $this->mapping['fetch'] = ClassMetadata::FETCH_EAGER;
91
92 return $this;
93 }
94
95 /** @return $this */
96 public function fetchLazy(): static
97 {
98 $this->mapping['fetch'] = ClassMetadata::FETCH_LAZY;
99
100 return $this;
101 }
102
103 /**
104 * Add Join Columns.
105 *
106 * @return $this
107 */
108 public function addJoinColumn(
109 string $columnName,
110 string $referencedColumnName,
111 bool $nullable = true,
112 bool $unique = false,
113 string|null $onDelete = null,
114 string|null $columnDef = null,
115 ): static {
116 $this->joinColumns[] = [
117 'name' => $columnName,
118 'referencedColumnName' => $referencedColumnName,
119 'nullable' => $nullable,
120 'unique' => $unique,
121 'onDelete' => $onDelete,
122 'columnDefinition' => $columnDef,
123 ];
124
125 return $this;
126 }
127
128 /**
129 * Sets field as primary key.
130 *
131 * @return $this
132 */
133 public function makePrimaryKey(): static
134 {
135 $this->mapping['id'] = true;
136
137 return $this;
138 }
139
140 /**
141 * Removes orphan entities when detached from their parent.
142 *
143 * @return $this
144 */
145 public function orphanRemoval(): static
146 {
147 $this->mapping['orphanRemoval'] = true;
148
149 return $this;
150 }
151
152 /** @throws InvalidArgumentException */
153 public function build(): ClassMetadataBuilder
154 {
155 $mapping = $this->mapping;
156 if ($this->joinColumns) {
157 $mapping['joinColumns'] = $this->joinColumns;
158 }
159
160 $cm = $this->builder->getClassMetadata();
161 if ($this->type === ClassMetadata::MANY_TO_ONE) {
162 $cm->mapManyToOne($mapping);
163 } elseif ($this->type === ClassMetadata::ONE_TO_ONE) {
164 $cm->mapOneToOne($mapping);
165 } else {
166 throw new InvalidArgumentException('Type should be a ToOne Association here');
167 }
168
169 return $this->builder;
170 }
171}