Completed
Push — master ( a92ef9...4dec9e )
by
unknown
01:32
created

DoctrineCollector::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\EasyExtendsBundle\Mapper;
15
16
class DoctrineCollector
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $associations;
22
23
    /**
24
     * @var array
25
     */
26
    protected $indexes;
27
28
    /**
29
     * @var array
30
     */
31
    protected $uniques;
32
33
    /**
34
     * @var array
35
     */
36
    protected $discriminators;
37
38
    /**
39
     * @var array
40
     */
41
    protected $discriminatorColumns;
42
43
    /**
44
     * @var array
45
     */
46
    protected $inheritanceTypes;
47
48
    /**
49
     * @var array
50
     */
51
    protected $overrides;
52
53
    /**
54
     * @var DoctrineCollector
55
     */
56
    private static $instance;
57
58
    public function __construct()
59
    {
60
        $this->initialize();
61
    }
62
63
    /**
64
     * @return DoctrineCollector
65
     */
66
    public static function getInstance(): self
67
    {
68
        if (!self::$instance) {
69
            self::$instance = new self();
70
        }
71
72
        return self::$instance;
73
    }
74
75
    /**
76
     * Add a discriminator to a class.
77
     *
78
     * @param string $class              The Class
79
     * @param string $key                Key is the database value and values are the classes
80
     * @param string $discriminatorClass The mapped class
81
     */
82
    public function addDiscriminator(string $class, string $key, string $discriminatorClass): void
83
    {
84
        if (!isset($this->discriminators[$class])) {
85
            $this->discriminators[$class] = [];
86
        }
87
88
        if (!isset($this->discriminators[$class][$key])) {
89
            $this->discriminators[$class][$key] = $discriminatorClass;
90
        }
91
    }
92
93
    /**
94
     * Add the Discriminator Column.
95
     *
96
     * @param string $class
97
     * @param array  $columnDef
98
     */
99
    public function addDiscriminatorColumn(string $class, array $columnDef): void
100
    {
101
        if (!isset($this->discriminatorColumns[$class])) {
102
            $this->discriminatorColumns[$class] = $columnDef;
103
        }
104
    }
105
106
    /**
107
     * @param string $class
108
     * @param string $type
109
     */
110
    public function addInheritanceType(string $class, string $type): void
111
    {
112
        if (!isset($this->inheritanceTypes[$class])) {
113
            $this->inheritanceTypes[$class] = $type;
114
        }
115
    }
116
117
    /**
118
     * @param string $class
119
     * @param string $type
120
     * @param array  $options
121
     */
122
    public function addAssociation(string $class, string $type, array $options): void
123
    {
124
        if (!isset($this->associations[$class])) {
125
            $this->associations[$class] = [];
126
        }
127
128
        if (!isset($this->associations[$class][$type])) {
129
            $this->associations[$class][$type] = [];
130
        }
131
132
        $this->associations[$class][$type][] = $options;
133
    }
134
135
    /**
136
     * @param string $class
137
     * @param string $name
138
     * @param array  $columns
139
     */
140
    public function addIndex(string $class, string $name, array $columns): void
141
    {
142
        if (!isset($this->indexes[$class])) {
143
            $this->indexes[$class] = [];
144
        }
145
146
        if (isset($this->indexes[$class][$name])) {
147
            return;
148
        }
149
150
        $this->indexes[$class][$name] = $columns;
151
    }
152
153
    /**
154
     * @param string $class
155
     * @param string $name
156
     * @param array  $columns
157
     */
158
    public function addUnique(string $class, string $name, array $columns): void
159
    {
160
        if (!isset($this->indexes[$class])) {
161
            $this->uniques[$class] = [];
162
        }
163
164
        if (isset($this->uniques[$class][$name])) {
165
            return;
166
        }
167
168
        $this->uniques[$class][$name] = $columns;
169
    }
170
171
    /**
172
     * Adds new override.
173
     *
174
     * @param string $class
175
     * @param string $type
176
     * @param array  $options
177
     */
178
    final public function addOverride(string $class, string $type, array $options): void
179
    {
180
        if (!isset($this->overrides[$class])) {
181
            $this->overrides[$class] = [];
182
        }
183
184
        if (!isset($this->overrides[$class][$type])) {
185
            $this->overrides[$class][$type] = [];
186
        }
187
188
        $this->overrides[$class][$type][] = $options;
189
    }
190
191
    /**
192
     * @return array
193
     */
194
    public function getAssociations(): array
195
    {
196
        return $this->associations;
197
    }
198
199
    /**
200
     * @return array
201
     */
202
    public function getDiscriminators(): array
203
    {
204
        return $this->discriminators;
205
    }
206
207
    /**
208
     * @return array
209
     */
210
    public function getDiscriminatorColumns(): array
211
    {
212
        return $this->discriminatorColumns;
213
    }
214
215
    /**
216
     * @return array
217
     */
218
    public function getInheritanceTypes(): array
219
    {
220
        return $this->inheritanceTypes;
221
    }
222
223
    /**
224
     * @return array
225
     */
226
    public function getIndexes(): array
227
    {
228
        return $this->indexes;
229
    }
230
231
    /**
232
     * @return array
233
     */
234
    public function getUniques(): array
235
    {
236
        return $this->uniques;
237
    }
238
239
    /**
240
     * Get all overrides.
241
     *
242
     * @return array
243
     */
244
    final public function getOverrides(): array
245
    {
246
        return $this->overrides;
247
    }
248
249
    public function clear()
250
    {
251
        $this->initialize();
252
    }
253
254
    private function initialize()
255
    {
256
        $this->associations = [];
257
        $this->indexes = [];
258
        $this->uniques = [];
259
        $this->discriminatorColumns = [];
260
        $this->inheritanceTypes = [];
261
        $this->discriminators = [];
262
        $this->overrides = [];
263
    }
264
}
265