summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/Mapping/Builder/AssociationBuilder.php
blob: ea9e13c53b13070600b96c6d3be10a7ba9db1525 (plain)
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
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Mapping\Builder;

use Doctrine\ORM\Mapping\ClassMetadata;
use InvalidArgumentException;

class AssociationBuilder
{
    /** @var mixed[]|null */
    protected array|null $joinColumns = null;

    /** @param mixed[] $mapping */
    public function __construct(
        protected readonly ClassMetadataBuilder $builder,
        protected array $mapping,
        protected readonly int $type,
    ) {
    }

    /** @return $this */
    public function mappedBy(string $fieldName): static
    {
        $this->mapping['mappedBy'] = $fieldName;

        return $this;
    }

    /** @return $this */
    public function inversedBy(string $fieldName): static
    {
        $this->mapping['inversedBy'] = $fieldName;

        return $this;
    }

    /** @return $this */
    public function cascadeAll(): static
    {
        $this->mapping['cascade'] = ['ALL'];

        return $this;
    }

    /** @return $this */
    public function cascadePersist(): static
    {
        $this->mapping['cascade'][] = 'persist';

        return $this;
    }

    /** @return $this */
    public function cascadeRemove(): static
    {
        $this->mapping['cascade'][] = 'remove';

        return $this;
    }

    /** @return $this */
    public function cascadeDetach(): static
    {
        $this->mapping['cascade'][] = 'detach';

        return $this;
    }

    /** @return $this */
    public function cascadeRefresh(): static
    {
        $this->mapping['cascade'][] = 'refresh';

        return $this;
    }

    /** @return $this */
    public function fetchExtraLazy(): static
    {
        $this->mapping['fetch'] = ClassMetadata::FETCH_EXTRA_LAZY;

        return $this;
    }

    /** @return $this */
    public function fetchEager(): static
    {
        $this->mapping['fetch'] = ClassMetadata::FETCH_EAGER;

        return $this;
    }

    /** @return $this */
    public function fetchLazy(): static
    {
        $this->mapping['fetch'] = ClassMetadata::FETCH_LAZY;

        return $this;
    }

    /**
     * Add Join Columns.
     *
     * @return $this
     */
    public function addJoinColumn(
        string $columnName,
        string $referencedColumnName,
        bool $nullable = true,
        bool $unique = false,
        string|null $onDelete = null,
        string|null $columnDef = null,
    ): static {
        $this->joinColumns[] = [
            'name' => $columnName,
            'referencedColumnName' => $referencedColumnName,
            'nullable' => $nullable,
            'unique' => $unique,
            'onDelete' => $onDelete,
            'columnDefinition' => $columnDef,
        ];

        return $this;
    }

    /**
     * Sets field as primary key.
     *
     * @return $this
     */
    public function makePrimaryKey(): static
    {
        $this->mapping['id'] = true;

        return $this;
    }

    /**
     * Removes orphan entities when detached from their parent.
     *
     * @return $this
     */
    public function orphanRemoval(): static
    {
        $this->mapping['orphanRemoval'] = true;

        return $this;
    }

    /** @throws InvalidArgumentException */
    public function build(): ClassMetadataBuilder
    {
        $mapping = $this->mapping;
        if ($this->joinColumns) {
            $mapping['joinColumns'] = $this->joinColumns;
        }

        $cm = $this->builder->getClassMetadata();
        if ($this->type === ClassMetadata::MANY_TO_ONE) {
            $cm->mapManyToOne($mapping);
        } elseif ($this->type === ClassMetadata::ONE_TO_ONE) {
            $cm->mapOneToOne($mapping);
        } else {
            throw new InvalidArgumentException('Type should be a ToOne Association here');
        }

        return $this->builder;
    }
}