Completed
Pull Request — 2.x (#97)
by Christian
02:46 queued 26s
created

DoctrineCollector::addOverride()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 4
nop 3
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->associations = array();
59
        $this->indexes = array();
60
        $this->uniques = array();
61
        $this->discriminatorColumns = array();
62
        $this->inheritanceTypes = array();
63
        $this->discriminators = array();
64
        $this->overrides = array();
65
    }
66
67
    /**
68
     * @return DoctrineCollector
69
     */
70
    public static function getInstance()
71
    {
72
        if (!self::$instance) {
73
            self::$instance = new self();
74
        }
75
76
        return self::$instance;
77
    }
78
79
    /**
80
     * Add a discriminator to a class.
81
     *
82
     * @param string $class              The Class
83
     * @param string $key                Key is the database value and values are the classes
84
     * @param string $discriminatorClass The mapped class
85
     */
86
    public function addDiscriminator($class, $key, $discriminatorClass)
87
    {
88
        if (!isset($this->discriminators[$class])) {
89
            $this->discriminators[$class] = array();
90
        }
91
92
        if (!isset($this->discriminators[$class][$key])) {
93
            $this->discriminators[$class][$key] = $discriminatorClass;
94
        }
95
    }
96
97
    /**
98
     * Add the Discriminator Column.
99
     *
100
     * @param string $class
101
     * @param array  $columnDef
102
     */
103
    public function addDiscriminatorColumn($class, array $columnDef)
104
    {
105
        if (!isset($this->discriminatorColumns[$class])) {
106
            $this->discriminatorColumns[$class] = $columnDef;
107
        }
108
    }
109
110
    /**
111
     * @param string $class
112
     * @param string $type
113
     */
114
    public function addInheritanceType($class, $type)
115
    {
116
        if (!isset($this->inheritanceTypes[$class])) {
117
            $this->inheritanceTypes[$class] = $type;
118
        }
119
    }
120
121
    /**
122
     * @param string $class
123
     * @param string $type
124
     * @param array  $options
125
     */
126
    public function addAssociation($class, $type, array $options)
127
    {
128
        if (!isset($this->associations[$class])) {
129
            $this->associations[$class] = array();
130
        }
131
132
        if (!isset($this->associations[$class][$type])) {
133
            $this->associations[$class][$type] = array();
134
        }
135
136
        $this->associations[$class][$type][] = $options;
137
    }
138
139
    /**
140
     * @param string $class
141
     * @param string $name
142
     * @param array  $columns
143
     */
144
    public function addIndex($class, $name, array $columns)
145
    {
146
        if (!isset($this->indexes[$class])) {
147
            $this->indexes[$class] = array();
148
        }
149
150
        if (isset($this->indexes[$class][$name])) {
151
            return;
152
        }
153
154
        $this->indexes[$class][$name] = $columns;
155
    }
156
157
    /**
158
     * @param string $class
159
     * @param string $name
160
     * @param array  $columns
161
     */
162
    public function addUnique($class, $name, array $columns)
163
    {
164
        if (!isset($this->indexes[$class])) {
165
            $this->uniques[$class] = array();
166
        }
167
168
        if (isset($this->uniques[$class][$name])) {
169
            return;
170
        }
171
172
        $this->uniques[$class][$name] = $columns;
173
    }
174
175
    /**
176
     * Adds new override.
177
     *
178
     * @param string $class
179
     * @param string $type
180
     * @param array  $options
181
     */
182
    final public function addOverride($class, $type, array $options)
183
    {
184
        if (!isset($this->overrides[$class])) {
185
            $this->overrides[$class] = array();
186
        }
187
188
        if (!isset($this->overrides[$class][$type])) {
189
            $this->overrides[$class][$type] = array();
190
        }
191
192
        $this->overrides[$class][$type][] = $options;
193
    }
194
195
    /**
196
     * @return array
197
     */
198
    public function getAssociations()
199
    {
200
        return $this->associations;
201
    }
202
203
    /**
204
     * @return array
205
     */
206
    public function getDiscriminators()
207
    {
208
        return $this->discriminators;
209
    }
210
211
    /**
212
     * @return array
213
     */
214
    public function getDiscriminatorColumns()
215
    {
216
        return $this->discriminatorColumns;
217
    }
218
219
    /**
220
     * @return array
221
     */
222
    public function getInheritanceTypes()
223
    {
224
        return $this->inheritanceTypes;
225
    }
226
227
    /**
228
     * @return array
229
     */
230
    public function getIndexes()
231
    {
232
        return $this->indexes;
233
    }
234
235
    /**
236
     * @return array
237
     */
238
    public function getUniques()
239
    {
240
        return $this->uniques;
241
    }
242
243
    /**
244
     * Get all overrides.
245
     *
246
     * @return array
247
     */
248
    final public function getOverrides()
249
    {
250
        return $this->overrides;
251
    }
252
}
253