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