Completed
Pull Request — develop (#3348)
by Sergei
63:43
created

Column::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Schema;
6
7
use Doctrine\DBAL\Types\Type;
8
use const E_USER_DEPRECATED;
9
use function array_merge;
10
use function assert;
11
use function is_string;
12
use function method_exists;
13
use function sprintf;
14
use function trigger_error;
15
16
/**
17
 * Object representation of a database column.
18
 */
19
class Column extends AbstractAsset
20
{
21
    /** @var Type */
22
    protected $_type;
23
24
    /** @var int|null */
25
    protected $_length;
26
27
    /** @var int|null */
28
    protected $_precision;
29
30
    /** @var int */
31
    protected $_scale = 0;
32
33
    /** @var bool */
34
    protected $_unsigned = false;
35
36
    /** @var bool */
37
    protected $_fixed = false;
38
39
    /** @var bool */
40
    protected $_notnull = true;
41
42
    /** @var mixed */
43
    protected $_default;
44
45
    /** @var bool */
46
    protected $_autoincrement = false;
47
48
    /** @var mixed[] */
49
    protected $_platformOptions = [];
50
51
    /** @var string|null */
52
    protected $_columnDefinition;
53
54
    /** @var string|null */
55
    protected $_comment;
56
57
    /** @var mixed[] */
58
    protected $_customSchemaOptions = [];
59
60
    /**
61
     * Creates a new Column.
62
     *
63 18853
     * @param mixed[] $options
64
     */
65 18853
    public function __construct(string $name, Type $type, array $options = [])
66 18853
    {
67 18853
        $this->_setName($name);
68 18853
        $this->setType($type);
69
        $this->setOptions($options);
70
    }
71
72
    public function getName() : string
73 18853
    {
74
        $name = parent::getName();
75 18853
        assert(is_string($name));
76 18065
77 18065
        return $name;
78
    }
79 3079
80
    /**
81 4
     * @param mixed[] $options
82 3079
     */
83 3079
    public function setOptions(array $options) : self
84
    {
85 3079
        foreach ($options as $name => $value) {
86
            $method = 'set' . $name;
87 18063
            if (! method_exists($this, $method)) {
88
                // next major: throw an exception
89
                @trigger_error(sprintf(
90 18853
                    'The "%s" column option is not supported,' .
91
                    ' setting it is deprecated and will cause an error in Doctrine 3.0',
92
                    $name
93 18853
                ), E_USER_DEPRECATED);
94
95 18853
                continue;
96
            }
97 18853
            $this->$method($value);
98
        }
99
100 16971
        return $this;
101
    }
102 16971
103
    public function setType(Type $type) : self
104 16971
    {
105
        $this->_type = $type;
106
107 16772
        return $this;
108
    }
109 16772
110 16698
    public function setLength(?int $length) : self
111
    {
112
        $this->_length = $length;
113 16772
114
        return $this;
115 16772
    }
116
117
    public function setPrecision(?int $precision) : self
118 16770
    {
119
        $this->_precision = $precision;
120 16770
121
        return $this;
122 16770
    }
123
124
    public function setScale(int $scale) : self
125 16662
    {
126
        $this->_scale = $scale;
127 16662
128
        return $this;
129 16662
    }
130
131
    public function setUnsigned(bool $unsigned) : self
132 16865
    {
133
        $this->_unsigned = $unsigned;
134 16865
135
        return $this;
136 16865
    }
137
138
    public function setFixed(bool $fixed) : self
139 18319
    {
140
        $this->_fixed = $fixed;
141 18319
142
        return $this;
143 18319
    }
144
145
    public function setNotnull(bool $notnull) : self
146
    {
147
        $this->_notnull = $notnull;
148
149 16804
        return $this;
150
    }
151 16804
152
    /**
153 16804
     * @param mixed $default
154
     */
155
    public function setDefault($default) : self
156
    {
157
        $this->_default = $default;
158
159 13287
        return $this;
160
    }
161 13287
162
    /**
163 13287
     * @param mixed[] $platformOptions
164
     */
165
    public function setPlatformOptions(array $platformOptions) : self
166
    {
167
        $this->_platformOptions = $platformOptions;
168
169 16561
        return $this;
170
    }
171 16561
172
    /**
173 16561
     * @param mixed $value
174
     */
175
    public function setPlatformOption(string $name, $value) : self
176 12670
    {
177
        $this->_platformOptions[$name] = $value;
178 12670
179
        return $this;
180 12670
    }
181
182
    public function setColumnDefinition(string $value) : self
183 18435
    {
184
        $this->_columnDefinition = $value;
185 18435
186
        return $this;
187
    }
188 15723
189
    public function getType() : Type
190 15723
    {
191
        return $this->_type;
192
    }
193 15712
194
    public function getLength() : ?int
195 15712
    {
196
        return $this->_length;
197
    }
198 15712
199
    public function getPrecision() : ?int
200 15712
    {
201
        return $this->_precision;
202
    }
203 15896
204
    public function getScale() : int
205 15896
    {
206
        return $this->_scale;
207
    }
208 15780
209
    public function getUnsigned() : bool
210 15780
    {
211
        return $this->_unsigned;
212
    }
213 15986
214
    public function getFixed() : bool
215 15986
    {
216
        return $this->_fixed;
217
    }
218
219
    public function getNotnull() : bool
220
    {
221 16122
        return $this->_notnull;
222
    }
223 16122
224
    /**
225
     * @return mixed
226
     */
227
    public function getDefault()
228
    {
229 16914
        return $this->_default;
230
    }
231 16914
232
    /**
233
     * @return mixed[]
234 18201
     */
235
    public function getPlatformOptions() : array
236 18201
    {
237
        return $this->_platformOptions;
238
    }
239
240
    public function hasPlatformOption(string $name) : bool
241
    {
242 15550
        return isset($this->_platformOptions[$name]);
243
    }
244 15550
245
    /**
246
     * @return mixed
247
     */
248
    public function getPlatformOption(string $name)
249
    {
250
        return $this->_platformOptions[$name];
251
    }
252 16680
253
    public function getColumnDefinition() : ?string
254 16680
    {
255
        return $this->_columnDefinition;
256
    }
257 17034
258
    public function getAutoincrement() : bool
259 17034
    {
260
        return $this->_autoincrement;
261 17034
    }
262
263
    public function setAutoincrement(bool $flag) : self
264 16906
    {
265
        $this->_autoincrement = $flag;
266 16906
267
        return $this;
268 16906
    }
269
270
    public function setComment(?string $comment) : self
271 18417
    {
272
        $this->_comment = $comment;
273 18417
274
        return $this;
275
    }
276
277
    public function getComment() : ?string
278
    {
279 2677
        return $this->_comment;
280
    }
281 2677
282
    /**
283 2677
     * @param mixed $value
284
     */
285
    public function setCustomSchemaOption(string $name, $value) : self
286 3127
    {
287
        $this->_customSchemaOptions[$name] = $value;
288 3127
289
        return $this;
290
    }
291
292
    public function hasCustomSchemaOption(string $name) : bool
293
    {
294 3127
        return isset($this->_customSchemaOptions[$name]);
295
    }
296 3127
297
    /**
298
     * @return mixed
299
     */
300
    public function getCustomSchemaOption(string $name)
301
    {
302 3131
        return $this->_customSchemaOptions[$name];
303
    }
304 3131
305
    /**
306 3131
     * @param mixed[] $customSchemaOptions
307
     */
308
    public function setCustomSchemaOptions(array $customSchemaOptions) : self
309
    {
310
        $this->_customSchemaOptions = $customSchemaOptions;
311
312 16912
        return $this;
313
    }
314 16912
315
    /**
316
     * @return mixed[]
317
     */
318
    public function getCustomSchemaOptions() : array
319
    {
320 18623
        return $this->_customSchemaOptions;
321
    }
322 18623
323 18623
    /**
324 18623
     * @return mixed[]
325 18623
     */
326 18623
    public function toArray() : array
327 18623
    {
328 18623
        return array_merge([
329 18623
            'name'          => $this->_name,
330 18623
            'type'          => $this->_type,
331 18623
            'default'       => $this->_default,
332 18623
            'notnull'       => $this->_notnull,
333 18623
            'length'        => $this->_length,
334 18623
            'precision'     => $this->_precision,
335 18623
            'scale'         => $this->_scale,
336
            'fixed'         => $this->_fixed,
337
            'unsigned'      => $this->_unsigned,
338
            'autoincrement' => $this->_autoincrement,
339
            'columnDefinition' => $this->_columnDefinition,
340
            'comment' => $this->_comment,
341
        ], $this->_platformOptions, $this->_customSchemaOptions);
342
    }
343
}
344