Failed Conditions
Pull Request — master (#2846)
by Grégoire
10:21
created

Column::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Schema;
21
22
use Doctrine\DBAL\Exception\InvalidArgumentException;
23
use Doctrine\DBAL\Types\Type;
24
25
/**
26
 * Object representation of a database column.
27
 *
28
 * @link   www.doctrine-project.org
29
 * @since  2.0
30
 * @author Benjamin Eberlei <[email protected]>
31
 */
32
class Column extends AbstractAsset
33
{
34
    /**
35
     * @var Type
36
     */
37
    protected $_type;
38
39
    /**
40
     * @var integer|null
41
     */
42
    protected $_length = null;
43
44
    /**
45
     * @var integer
46
     */
47
    protected $_precision = 10;
48
49
    /**
50
     * @var integer
51
     */
52
    protected $_scale = 0;
53
54
    /**
55
     * @var boolean
56
     */
57
    protected $_unsigned = false;
58
59
    /**
60
     * @var boolean
61
     */
62
    protected $_fixed = false;
63
64
    /**
65
     * @var boolean
66
     */
67
    protected $_notnull = true;
68
69
    /**
70
     * @var string|null
71
     */
72
    protected $_default = null;
73
74
    /**
75
     * @var boolean
76
     */
77
    protected $_autoincrement = false;
78
79
    /**
80
     * @var array
81
     */
82
    protected $_platformOptions = [];
83
84
    /**
85
     * @var string|null
86
     */
87
    protected $_columnDefinition = null;
88
89
    /**
90
     * @var string|null
91
     */
92
    protected $_comment = null;
93
94
    /**
95
     * @var array
96
     */
97
    protected $_customSchemaOptions = [];
98
99
    /**
100
     * Creates a new Column.
101
     *
102
     * @param string $columnName
103
     * @param Type   $type
104
     * @param array  $options
105
     */
106 703
    public function __construct($columnName, Type $type, array $options=[])
107
    {
108 703
        $this->_setName($columnName);
109 703
        $this->setType($type);
110 703
        $this->setOptions($options);
111 703
    }
112
113
    /**
114
     * @param array $options
115
     *
116
     * @return Column
117
     */
118 703
    public function setOptions(array $options)
119
    {
120 703
        foreach ($options as $name => $value) {
121 372
            $method = "set".$name;
122 372
            if ( ! method_exists($this, $method)) {
123
                // next major: use InvalidArgumentException::fromUnsupportedOption()
124 1
                @trigger_error(sprintf(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
125
                    'The "%s" option is not supported,'.
126 1
                    ' setting it is deprecated and will cause an exception in 3.0',
127 1
                    $name
128 1
                ), E_USER_DEPRECATED);
129
130 1
                return;
131
            }
132 371
            $this->$method($value);
133
        }
134
135 702
        return $this;
136
    }
137
138
    /**
139
     * @param Type $type
140
     *
141
     * @return Column
142
     */
143 703
    public function setType(Type $type)
144
    {
145 703
        $this->_type = $type;
146
147 703
        return $this;
148
    }
149
150
    /**
151
     * @param integer|null $length
152
     *
153
     * @return Column
154
     */
155 140
    public function setLength($length)
156
    {
157 140
        if ($length !== null) {
158 113
            $this->_length = (int) $length;
159
        } else {
160 37
            $this->_length = null;
161
        }
162
163 140
        return $this;
164
    }
165
166
    /**
167
     * @param integer $precision
168
     *
169
     * @return Column
170
     */
171 61
    public function setPrecision($precision)
172
    {
173 61
        if (!is_numeric($precision)) {
174 39
            $precision = 10; // defaults to 10 when no valid precision is given.
175
        }
176
177 61
        $this->_precision = (int) $precision;
178
179 61
        return $this;
180
    }
181
182
    /**
183
     * @param integer $scale
184
     *
185
     * @return Column
186
     */
187 60
    public function setScale($scale)
188
    {
189 60
        if (!is_numeric($scale)) {
190 38
            $scale = 0;
191
        }
192
193 60
        $this->_scale = (int) $scale;
194
195 60
        return $this;
196
    }
197
198
    /**
199
     * @param boolean $unsigned
200
     *
201
     * @return Column
202
     */
203 41
    public function setUnsigned($unsigned)
204
    {
205 41
        $this->_unsigned = (bool) $unsigned;
206
207 41
        return $this;
208
    }
209
210
    /**
211
     * @param boolean $fixed
212
     *
213
     * @return Column
214
     */
215 72
    public function setFixed($fixed)
216
    {
217 72
        $this->_fixed = (bool) $fixed;
218
219 72
        return $this;
220
    }
221
222
    /**
223
     * @param boolean $notnull
224
     *
225
     * @return Column
226
     */
227 419
    public function setNotnull($notnull)
228
    {
229 419
        $this->_notnull = (bool) $notnull;
230
231 419
        return $this;
232
    }
233
234
    /**
235
     * @param mixed $default
236
     *
237
     * @return Column
238
     */
239 83
    public function setDefault($default)
240
    {
241 83
        $this->_default = $default;
242
243 83
        return $this;
244
    }
245
246
    /**
247
     * @param array $platformOptions
248
     *
249
     * @return Column
250
     */
251 9
    public function setPlatformOptions(array $platformOptions)
252
    {
253 9
        $this->_platformOptions = $platformOptions;
254
255 9
        return $this;
256
    }
257
258
    /**
259
     * @param string $name
260
     * @param mixed  $value
261
     *
262
     * @return Column
263
     */
264 15
    public function setPlatformOption($name, $value)
265
    {
266 15
        $this->_platformOptions[$name] = $value;
267
268 15
        return $this;
269
    }
270
271
    /**
272
     * @param string $value
273
     *
274
     * @return Column
275
     */
276 1
    public function setColumnDefinition($value)
277
    {
278 1
        $this->_columnDefinition = $value;
279
280 1
        return $this;
281
    }
282
283
    /**
284
     * @return Type
285
     */
286 474
    public function getType()
287
    {
288 474
        return $this->_type;
289
    }
290
291
    /**
292
     * @return integer|null
293
     */
294 4
    public function getLength()
295
    {
296 4
        return $this->_length;
297
    }
298
299
    /**
300
     * @return integer
301
     */
302 2
    public function getPrecision()
303
    {
304 2
        return $this->_precision;
305
    }
306
307
    /**
308
     * @return integer
309
     */
310 2
    public function getScale()
311
    {
312 2
        return $this->_scale;
313
    }
314
315
    /**
316
     * @return boolean
317
     */
318 3
    public function getUnsigned()
319
    {
320 3
        return $this->_unsigned;
321
    }
322
323
    /**
324
     * @return boolean
325
     */
326 4
    public function getFixed()
327
    {
328 4
        return $this->_fixed;
329
    }
330
331
    /**
332
     * @return boolean
333
     */
334 11
    public function getNotnull()
335
    {
336 11
        return $this->_notnull;
337
    }
338
339
    /**
340
     * @return string|null
341
     */
342 46
    public function getDefault()
343
    {
344 46
        return $this->_default;
345
    }
346
347
    /**
348
     * @return array
349
     */
350 130
    public function getPlatformOptions()
351
    {
352 130
        return $this->_platformOptions;
353
    }
354
355
    /**
356
     * @param string $name
357
     *
358
     * @return boolean
359
     */
360 335
    public function hasPlatformOption($name)
361
    {
362 335
        return isset($this->_platformOptions[$name]);
363
    }
364
365
    /**
366
     * @param string $name
367
     *
368
     * @return mixed
369
     */
370 2
    public function getPlatformOption($name)
371
    {
372 2
        return $this->_platformOptions[$name];
373
    }
374
375
    /**
376
     * @return string|null
377
     */
378
    public function getColumnDefinition()
379
    {
380
        return $this->_columnDefinition;
381
    }
382
383
    /**
384
     * @return boolean
385
     */
386 36
    public function getAutoincrement()
387
    {
388 36
        return $this->_autoincrement;
389
    }
390
391
    /**
392
     * @param boolean $flag
393
     *
394
     * @return Column
395
     */
396 118
    public function setAutoincrement($flag)
397
    {
398 118
        $this->_autoincrement = $flag;
399
400 118
        return $this;
401
    }
402
403
    /**
404
     * @param string $comment
405
     *
406
     * @return Column
407
     */
408 121
    public function setComment($comment)
409
    {
410 121
        $this->_comment = $comment;
411
412 121
        return $this;
413
    }
414
415
    /**
416
     * @return string|null
417
     */
418 446
    public function getComment()
419
    {
420 446
        return $this->_comment;
421
    }
422
423
    /**
424
     * @param string $name
425
     * @param mixed  $value
426
     *
427
     * @return Column
428
     */
429 1
    public function setCustomSchemaOption($name, $value)
430
    {
431 1
        $this->_customSchemaOptions[$name] = $value;
432
433 1
        return $this;
434
    }
435
436
    /**
437
     * @param string $name
438
     *
439
     * @return boolean
440
     */
441 1
    public function hasCustomSchemaOption($name)
442
    {
443 1
        return isset($this->_customSchemaOptions[$name]);
444
    }
445
446
    /**
447
     * @param string $name
448
     *
449
     * @return mixed
450
     */
451 1
    public function getCustomSchemaOption($name)
452
    {
453 1
        return $this->_customSchemaOptions[$name];
454
    }
455
456
    /**
457
     * @param array $customSchemaOptions
458
     *
459
     * @return Column
460
     */
461 3
    public function setCustomSchemaOptions(array $customSchemaOptions)
462
    {
463 3
        $this->_customSchemaOptions = $customSchemaOptions;
464
465 3
        return $this;
466
    }
467
468
    /**
469
     * @return array
470
     */
471 128
    public function getCustomSchemaOptions()
472
    {
473 128
        return $this->_customSchemaOptions;
474
    }
475
476
    /**
477
     * @return array
478
     */
479 574
    public function toArray()
480
    {
481 574
        return array_merge([
482 574
            'name'          => $this->_name,
483 574
            'type'          => $this->_type,
484 574
            'default'       => $this->_default,
485 574
            'notnull'       => $this->_notnull,
486 574
            'length'        => $this->_length,
487 574
            'precision'     => $this->_precision,
488 574
            'scale'         => $this->_scale,
489 574
            'fixed'         => $this->_fixed,
490 574
            'unsigned'      => $this->_unsigned,
491 574
            'autoincrement' => $this->_autoincrement,
492 574
            'columnDefinition' => $this->_columnDefinition,
493 574
            'comment' => $this->_comment,
494 574
        ], $this->_platformOptions, $this->_customSchemaOptions);
495
    }
496
}
497