Passed
Pull Request — master (#2846)
by Grégoire
10:44
created

Column::getPlatformOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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 757
    public function __construct($columnName, Type $type, array $options=[])
107
    {
108 757
        $this->_setName($columnName);
109 757
        $this->setType($type);
110 757
        $this->setOptions($options);
111 757
    }
112
113
    /**
114
     * @param array $options
115
     *
116
     * @return Column
117
     */
118 757
    public function setOptions(array $options)
119
    {
120 757
        foreach ($options as $name => $value) {
121 390
            $method = "set".$name;
122 390
            if ( ! method_exists($this, $method)) {
123
                // next major: throw an exception
124 1
                @trigger_error(sprintf(
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 $this;
131
            }
132 389
            $this->$method($value);
133
        }
134
135 756
        return $this;
136
    }
137
138
    /**
139
     * @param Type $type
140
     *
141
     * @return Column
142
     */
143 757
    public function setType(Type $type)
144
    {
145 757
        $this->_type = $type;
146
147 757
        return $this;
148
    }
149
150
    /**
151
     * @param integer|null $length
152
     *
153
     * @return Column
154
     */
155 147
    public function setLength($length)
156
    {
157 147
        if ($length !== null) {
158 118
            $this->_length = (int) $length;
159
        } else {
160 39
            $this->_length = null;
161
        }
162
163 147
        return $this;
164
    }
165
166
    /**
167
     * @param integer $precision
168
     *
169
     * @return Column
170
     */
171 63
    public function setPrecision($precision)
172
    {
173 63
        if (!is_numeric($precision)) {
174 41
            $precision = 10; // defaults to 10 when no valid precision is given.
175
        }
176
177 63
        $this->_precision = (int) $precision;
178
179 63
        return $this;
180
    }
181
182
    /**
183
     * @param integer $scale
184
     *
185
     * @return Column
186
     */
187 62
    public function setScale($scale)
188
    {
189 62
        if (!is_numeric($scale)) {
190 40
            $scale = 0;
191
        }
192
193 62
        $this->_scale = (int) $scale;
194
195 62
        return $this;
196
    }
197
198
    /**
199
     * @param boolean $unsigned
200
     *
201
     * @return Column
202
     */
203 43
    public function setUnsigned($unsigned)
204
    {
205 43
        $this->_unsigned = (bool) $unsigned;
206
207 43
        return $this;
208
    }
209
210
    /**
211
     * @param boolean $fixed
212
     *
213
     * @return Column
214
     */
215 75
    public function setFixed($fixed)
216
    {
217 75
        $this->_fixed = (bool) $fixed;
218
219 75
        return $this;
220
    }
221
222
    /**
223
     * @param boolean $notnull
224
     *
225
     * @return Column
226
     */
227 461
    public function setNotnull($notnull)
228
    {
229 461
        $this->_notnull = (bool) $notnull;
230
231 461
        return $this;
232
    }
233
234
    /**
235
     * @param mixed $default
236
     *
237
     * @return Column
238
     */
239 87
    public function setDefault($default)
240
    {
241 87
        $this->_default = $default;
242
243 87
        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 17
    public function setPlatformOption($name, $value)
265
    {
266 17
        $this->_platformOptions[$name] = $value;
267
268 17
        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 516
    public function getType()
287
    {
288 516
        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 142
    public function getPlatformOptions()
351
    {
352 142
        return $this->_platformOptions;
353
    }
354
355
    /**
356
     * @param string $name
357
     *
358
     * @return boolean
359
     */
360 366
    public function hasPlatformOption($name)
361
    {
362 366
        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 41
    public function getAutoincrement()
387
    {
388 41
        return $this->_autoincrement;
389
    }
390
391
    /**
392
     * @param boolean $flag
393
     *
394
     * @return Column
395
     */
396 143
    public function setAutoincrement($flag)
397
    {
398 143
        $this->_autoincrement = $flag;
399
400 143
        return $this;
401
    }
402
403
    /**
404
     * @param string $comment
405
     *
406
     * @return Column
407
     */
408 128
    public function setComment($comment)
409
    {
410 128
        $this->_comment = $comment;
411
412 128
        return $this;
413
    }
414
415
    /**
416
     * @return string|null
417
     */
418 488
    public function getComment()
419
    {
420 488
        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 140
    public function getCustomSchemaOptions()
472
    {
473 140
        return $this->_customSchemaOptions;
474
    }
475
476
    /**
477
     * @return array
478
     */
479 622
    public function toArray()
480
    {
481 622
        return array_merge([
482 622
            'name'          => $this->_name,
483 622
            'type'          => $this->_type,
484 622
            'default'       => $this->_default,
485 622
            'notnull'       => $this->_notnull,
486 622
            'length'        => $this->_length,
487 622
            'precision'     => $this->_precision,
488 622
            'scale'         => $this->_scale,
489 622
            'fixed'         => $this->_fixed,
490 622
            'unsigned'      => $this->_unsigned,
491 622
            'autoincrement' => $this->_autoincrement,
492 622
            'columnDefinition' => $this->_columnDefinition,
493 622
            'comment' => $this->_comment,
494 622
        ], $this->_platformOptions, $this->_customSchemaOptions);
495
    }
496
}
497