Failed Conditions
Pull Request — master (#3645)
by Matthew
14:32
created

Column::getColumnDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Doctrine\DBAL\Schema;
4
5
use Doctrine\DBAL\Types\Type;
6
use const E_USER_DEPRECATED;
7
use function array_merge;
8
use function is_float;
9
use function is_numeric;
10
use function localeconv;
11
use function method_exists;
12
use function sprintf;
13
use function str_replace;
14
use function strval;
15
use function trigger_error;
16
17
/**
18
 * Object representation of a database column.
19
 */
20
class Column extends AbstractAsset
21
{
22
    /** @var Type */
23
    protected $_type;
24
25
    /** @var int|null */
26
    protected $_length = null;
27
28
    /** @var int */
29
    protected $_precision = 10;
30
31
    /** @var int */
32
    protected $_scale = 0;
33
34
    /** @var bool */
35
    protected $_unsigned = false;
36
37
    /** @var bool */
38
    protected $_fixed = false;
39
40
    /** @var bool */
41
    protected $_notnull = true;
42
43
    /** @var string|null */
44
    protected $_default = null;
45
46
    /** @var bool */
47
    protected $_autoincrement = false;
48
49
    /** @var mixed[] */
50
    protected $_platformOptions = [];
51
52
    /** @var string|null */
53
    protected $_columnDefinition = null;
54
55
    /** @var string|null */
56
    protected $_comment = null;
57
58
    /** @var mixed[] */
59
    protected $_customSchemaOptions = [];
60
61
    /**
62
     * Creates a new Column.
63
     *
64
     * @param string  $columnName
65
     * @param mixed[] $options
66
     */
67 23918
    public function __construct($columnName, Type $type, array $options = [])
68
    {
69 23918
        $this->_setName($columnName);
70 23918
        $this->setType($type);
71 23918
        $this->setOptions($options);
72 23918
    }
73
74
    /**
75
     * @param mixed[] $options
76
     *
77
     * @return Column
78
     */
79 23918
    public function setOptions(array $options)
80
    {
81 23918
        foreach ($options as $name => $value) {
82 22942
            $method = 'set' . $name;
83 22942
            if (! method_exists($this, $method)) {
84
                // next major: throw an exception
85 2860
                @trigger_error(sprintf(
86
                    'The "%s" column option is not supported,' .
87 2
                    ' setting it is deprecated and will cause an error in Doctrine 3.0',
88 2860
                    $name
89 2860
                ), E_USER_DEPRECATED);
90
91 2860
                continue;
92
            }
93 22942
            $this->$method($value);
94
        }
95
96 23918
        return $this;
97
    }
98
99
    /**
100
     * @return Column
101
     */
102 23918
    public function setType(Type $type)
103
    {
104 23918
        $this->_type = $type;
105
106 23918
        return $this;
107
    }
108
109
    /**
110
     * @param int|null $length
111
     *
112
     * @return Column
113
     */
114 21809
    public function setLength($length)
115
    {
116 21809
        if ($length !== null) {
117 21757
            $this->_length = (int) $length;
118
        } else {
119 21410
            $this->_length = null;
120
        }
121
122 21809
        return $this;
123
    }
124
125
    /**
126
     * @param int $precision
127
     *
128
     * @return Column
129
     */
130 21457
    public function setPrecision($precision)
131
    {
132 21457
        if (! is_numeric($precision)) {
0 ignored issues
show
introduced by
The condition is_numeric($precision) is always true.
Loading history...
133 19884
            $precision = 10; // defaults to 10 when no valid precision is given.
134
        }
135
136 21457
        $this->_precision = (int) $precision;
137
138 21457
        return $this;
139
    }
140
141
    /**
142
     * @param int $scale
143
     *
144
     * @return Column
145
     */
146 21455
    public function setScale($scale)
147
    {
148 21455
        if (! is_numeric($scale)) {
0 ignored issues
show
introduced by
The condition is_numeric($scale) is always true.
Loading history...
149 19828
            $scale = 0;
150
        }
151
152 21455
        $this->_scale = (int) $scale;
153
154 21455
        return $this;
155
    }
156
157
    /**
158
     * @param bool $unsigned
159
     *
160
     * @return Column
161
     */
162 21416
    public function setUnsigned($unsigned)
163
    {
164 21416
        $this->_unsigned = (bool) $unsigned;
165
166 21416
        return $this;
167
    }
168
169
    /**
170
     * @param bool $fixed
171
     *
172
     * @return Column
173
     */
174 21660
    public function setFixed($fixed)
175
    {
176 21660
        $this->_fixed = (bool) $fixed;
177
178 21660
        return $this;
179
    }
180
181
    /**
182
     * @param bool $notnull
183
     *
184
     * @return Column
185
     */
186 23234
    public function setNotnull($notnull)
187
    {
188 23234
        $this->_notnull = (bool) $notnull;
189
190 23234
        return $this;
191
    }
192
193
    /**
194
     * @param mixed $default
195
     *
196
     * @return Column
197
     */
198 21738
    public function setDefault($default)
199
    {
200 21738
        if (is_float($default)) {
201 18796
            $localeInfo = localeconv();
202 18796
            $decimal    = $localeInfo['decimal_point'] ?? '.';
203 18796
            if ($decimal !== '.') {
204
                // SQL standard is '.' for all decimal points so convert to string (issue #3631)
205
                // Also see https://stackoverflow.com/questions/6627239/insert-non-english-decimal-points-in-mysql/6627551#6627551
206
                $default = strval($default);
207
                $default = str_replace($decimal, '.', $default);
208
            }
209
        }
210 21738
        $this->_default = $default;
211
212 21738
        return $this;
213
    }
214
215
    /**
216
     * @param mixed[] $platformOptions
217
     *
218
     * @return Column
219
     */
220 17886
    public function setPlatformOptions(array $platformOptions)
221
    {
222 17886
        $this->_platformOptions = $platformOptions;
223
224 17886
        return $this;
225
    }
226
227
    /**
228
     * @param string $name
229
     * @param mixed  $value
230
     *
231
     * @return Column
232
     */
233 20978
    public function setPlatformOption($name, $value)
234
    {
235 20978
        $this->_platformOptions[$name] = $value;
236
237 20978
        return $this;
238
    }
239
240
    /**
241
     * @param string $value
242
     *
243
     * @return Column
244
     */
245 11782
    public function setColumnDefinition($value)
246
    {
247 11782
        $this->_columnDefinition = $value;
248
249 11782
        return $this;
250
    }
251
252
    /**
253
     * @return Type
254
     */
255 23384
    public function getType()
256
    {
257 23384
        return $this->_type;
258
    }
259
260
    /**
261
     * @return int|null
262
     */
263 20298
    public function getLength()
264
    {
265 20298
        return $this->_length;
266
    }
267
268
    /**
269
     * @return int
270
     */
271 20287
    public function getPrecision()
272
    {
273 20287
        return $this->_precision;
274
    }
275
276
    /**
277
     * @return int
278
     */
279 20287
    public function getScale()
280
    {
281 20287
        return $this->_scale;
282
    }
283
284
    /**
285
     * @return bool
286
     */
287 20403
    public function getUnsigned()
288
    {
289 20403
        return $this->_unsigned;
290
    }
291
292
    /**
293
     * @return bool
294
     */
295 20395
    public function getFixed()
296
    {
297 20395
        return $this->_fixed;
298
    }
299
300
    /**
301
     * @return bool
302
     */
303 20463
    public function getNotnull()
304
    {
305 20463
        return $this->_notnull;
306
    }
307
308
    /**
309
     * @return string|null
310
     */
311 21161
    public function getDefault()
312
    {
313 21161
        return $this->_default;
314
    }
315
316
    /**
317
     * @return mixed[]
318
     */
319 21602
    public function getPlatformOptions()
320
    {
321 21602
        return $this->_platformOptions;
322
    }
323
324
    /**
325
     * @param string $name
326
     *
327
     * @return bool
328
     */
329 23040
    public function hasPlatformOption($name)
330
    {
331 23040
        return isset($this->_platformOptions[$name]);
332
    }
333
334
    /**
335
     * @param string $name
336
     *
337
     * @return mixed
338
     */
339 18461
    public function getPlatformOption($name)
340
    {
341 18461
        return $this->_platformOptions[$name];
342
    }
343
344
    /**
345
     * @return string|null
346
     */
347
    public function getColumnDefinition()
348
    {
349
        return $this->_columnDefinition;
350
    }
351
352
    /**
353
     * @return bool
354
     */
355 21185
    public function getAutoincrement()
356
    {
357 21185
        return $this->_autoincrement;
358
    }
359
360
    /**
361
     * @param bool $flag
362
     *
363
     * @return Column
364
     */
365 21868
    public function setAutoincrement($flag)
366
    {
367 21868
        $this->_autoincrement = $flag;
368
369 21868
        return $this;
370
    }
371
372
    /**
373
     * @param string|null $comment
374
     *
375
     * @return Column
376
     */
377 21662
    public function setComment($comment)
378
    {
379 21662
        $this->_comment = $comment;
380
381 21662
        return $this;
382
    }
383
384
    /**
385
     * @return string|null
386
     */
387 23362
    public function getComment()
388
    {
389 23362
        return $this->_comment;
390
    }
391
392
    /**
393
     * @param string $name
394
     * @param mixed  $value
395
     *
396
     * @return Column
397
     */
398 2435
    public function setCustomSchemaOption($name, $value)
399
    {
400 2435
        $this->_customSchemaOptions[$name] = $value;
401
402 2435
        return $this;
403
    }
404
405
    /**
406
     * @param string $name
407
     *
408
     * @return bool
409
     */
410 2910
    public function hasCustomSchemaOption($name)
411
    {
412 2910
        return isset($this->_customSchemaOptions[$name]);
413
    }
414
415
    /**
416
     * @param string $name
417
     *
418
     * @return mixed
419
     */
420 2910
    public function getCustomSchemaOption($name)
421
    {
422 2910
        return $this->_customSchemaOptions[$name];
423
    }
424
425
    /**
426
     * @param mixed[] $customSchemaOptions
427
     *
428
     * @return Column
429
     */
430 2914
    public function setCustomSchemaOptions(array $customSchemaOptions)
431
    {
432 2914
        $this->_customSchemaOptions = $customSchemaOptions;
433
434 2914
        return $this;
435
    }
436
437
    /**
438
     * @return mixed[]
439
     */
440 21600
    public function getCustomSchemaOptions()
441
    {
442 21600
        return $this->_customSchemaOptions;
443
    }
444
445
    /**
446
     * @return mixed[]
447
     */
448 23596
    public function toArray()
449
    {
450 23596
        return array_merge([
451 23596
            'name'          => $this->_name,
452 23596
            'type'          => $this->_type,
453 23596
            'default'       => $this->_default,
454 23596
            'notnull'       => $this->_notnull,
455 23596
            'length'        => $this->_length,
456 23596
            'precision'     => $this->_precision,
457 23596
            'scale'         => $this->_scale,
458 23596
            'fixed'         => $this->_fixed,
459 23596
            'unsigned'      => $this->_unsigned,
460 23596
            'autoincrement' => $this->_autoincrement,
461 23596
            'columnDefinition' => $this->_columnDefinition,
462 23596
            'comment' => $this->_comment,
463 23596
        ], $this->_platformOptions, $this->_customSchemaOptions);
464
    }
465
}
466