Column.php 8.28 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Schema;

Benjamin Morel's avatar
Benjamin Morel committed
5
use Doctrine\DBAL\Types\Type;
6 7 8 9 10
use function array_merge;
use function is_numeric;
use function method_exists;
use function sprintf;
use function trigger_error;
Grégoire Paris's avatar
Grégoire Paris committed
11
use const E_USER_DEPRECATED;
12 13

/**
Benjamin Morel's avatar
Benjamin Morel committed
14
 * Object representation of a database column.
15
 */
16
class Column extends AbstractAsset
17
{
18
    /** @var Type */
19 20
    protected $_type;

21
    /** @var int|null */
22
    protected $_length = null;
23

24
    /** @var int */
25
    protected $_precision = 10;
26

27
    /** @var int */
28 29
    protected $_scale = 0;

30
    /** @var bool */
31 32
    protected $_unsigned = false;

33
    /** @var bool */
34 35
    protected $_fixed = false;

36
    /** @var bool */
37 38
    protected $_notnull = true;

39
    /** @var string|null */
40
    protected $_default = null;
41

42
    /** @var bool */
43 44
    protected $_autoincrement = false;

45
    /** @var mixed[] */
46
    protected $_platformOptions = [];
47

48
    /** @var string|null */
49 50
    protected $_columnDefinition = null;

51
    /** @var string|null */
52 53
    protected $_comment = null;

54
    /** @var mixed[] */
55
    protected $_customSchemaOptions = [];
56

57
    /**
Benjamin Morel's avatar
Benjamin Morel committed
58
     * Creates a new Column.
59
     *
60 61
     * @param string  $columnName
     * @param mixed[] $options
62
     */
63
    public function __construct($columnName, Type $type, array $options = [])
64 65 66 67 68 69 70
    {
        $this->_setName($columnName);
        $this->setType($type);
        $this->setOptions($options);
    }

    /**
71
     * @param mixed[] $options
Benjamin Morel's avatar
Benjamin Morel committed
72
     *
jeroendedauw's avatar
jeroendedauw committed
73
     * @return Column
74 75 76
     */
    public function setOptions(array $options)
    {
77
        foreach ($options as $name => $value) {
78 79
            $method = 'set' . $name;
            if (! method_exists($this, $method)) {
80
                // next major: throw an exception
81
                @trigger_error(sprintf(
82
                    'The "%s" column option is not supported,' .
83
                    ' setting it is deprecated and will cause an error in Doctrine DBAL 3.0',
84 85 86
                    $name
                ), E_USER_DEPRECATED);

87
                continue;
88
            }
Grégoire Paris's avatar
Grégoire Paris committed
89

90
            $this->$method($value);
91
        }
Benjamin Morel's avatar
Benjamin Morel committed
92

93 94 95 96
        return $this;
    }

    /**
jeroendedauw's avatar
jeroendedauw committed
97
     * @return Column
98 99 100 101
     */
    public function setType(Type $type)
    {
        $this->_type = $type;
102

103 104 105 106
        return $this;
    }

    /**
107
     * @param int|null $length
Benjamin Morel's avatar
Benjamin Morel committed
108
     *
jeroendedauw's avatar
jeroendedauw committed
109
     * @return Column
110 111 112
     */
    public function setLength($length)
    {
Steve Müller's avatar
Steve Müller committed
113
        if ($length !== null) {
114
            $this->_length = (int) $length;
115 116 117
        } else {
            $this->_length = null;
        }
Benjamin Morel's avatar
Benjamin Morel committed
118

119 120 121 122
        return $this;
    }

    /**
123
     * @param int $precision
Benjamin Morel's avatar
Benjamin Morel committed
124
     *
jeroendedauw's avatar
jeroendedauw committed
125
     * @return Column
126 127 128
     */
    public function setPrecision($precision)
    {
129
        if (! is_numeric($precision)) {
130 131 132
            $precision = 10; // defaults to 10 when no valid precision is given.
        }

133
        $this->_precision = (int) $precision;
Benjamin Morel's avatar
Benjamin Morel committed
134

135 136 137 138
        return $this;
    }

    /**
139
     * @param int $scale
Benjamin Morel's avatar
Benjamin Morel committed
140
     *
jeroendedauw's avatar
jeroendedauw committed
141
     * @return Column
142 143 144
     */
    public function setScale($scale)
    {
145
        if (! is_numeric($scale)) {
146 147 148
            $scale = 0;
        }

149
        $this->_scale = (int) $scale;
Benjamin Morel's avatar
Benjamin Morel committed
150

151 152 153 154
        return $this;
    }

    /**
155
     * @param bool $unsigned
156
     *
jeroendedauw's avatar
jeroendedauw committed
157
     * @return Column
158 159 160
     */
    public function setUnsigned($unsigned)
    {
161
        $this->_unsigned = (bool) $unsigned;
Benjamin Morel's avatar
Benjamin Morel committed
162

163 164 165 166
        return $this;
    }

    /**
167
     * @param bool $fixed
168
     *
jeroendedauw's avatar
jeroendedauw committed
169
     * @return Column
170 171 172
     */
    public function setFixed($fixed)
    {
173
        $this->_fixed = (bool) $fixed;
Benjamin Morel's avatar
Benjamin Morel committed
174

175 176 177 178
        return $this;
    }

    /**
179
     * @param bool $notnull
Benjamin Morel's avatar
Benjamin Morel committed
180
     *
jeroendedauw's avatar
jeroendedauw committed
181
     * @return Column
182 183 184
     */
    public function setNotnull($notnull)
    {
185
        $this->_notnull = (bool) $notnull;
Benjamin Morel's avatar
Benjamin Morel committed
186

187 188 189 190
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
191
     * @param mixed $default
192
     *
jeroendedauw's avatar
jeroendedauw committed
193
     * @return Column
194 195 196 197
     */
    public function setDefault($default)
    {
        $this->_default = $default;
Benjamin Morel's avatar
Benjamin Morel committed
198

199 200 201 202
        return $this;
    }

    /**
203
     * @param mixed[] $platformOptions
Benjamin Morel's avatar
Benjamin Morel committed
204
     *
jeroendedauw's avatar
jeroendedauw committed
205
     * @return Column
206 207 208 209
     */
    public function setPlatformOptions(array $platformOptions)
    {
        $this->_platformOptions = $platformOptions;
Benjamin Morel's avatar
Benjamin Morel committed
210

211 212 213 214
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
215 216
     * @param string $name
     * @param mixed  $value
217
     *
jeroendedauw's avatar
jeroendedauw committed
218
     * @return Column
219 220 221 222
     */
    public function setPlatformOption($name, $value)
    {
        $this->_platformOptions[$name] = $value;
Benjamin Morel's avatar
Benjamin Morel committed
223

224 225 226
        return $this;
    }

227
    /**
Benjamin Morel's avatar
Benjamin Morel committed
228
     * @param string $value
229
     *
jeroendedauw's avatar
jeroendedauw committed
230
     * @return Column
231 232 233 234
     */
    public function setColumnDefinition($value)
    {
        $this->_columnDefinition = $value;
Benjamin Morel's avatar
Benjamin Morel committed
235

236 237 238
        return $this;
    }

Benjamin Morel's avatar
Benjamin Morel committed
239
    /**
jeroendedauw's avatar
jeroendedauw committed
240
     * @return Type
Benjamin Morel's avatar
Benjamin Morel committed
241
     */
242 243 244 245 246
    public function getType()
    {
        return $this->_type;
    }

Benjamin Morel's avatar
Benjamin Morel committed
247
    /**
248
     * @return int|null
Benjamin Morel's avatar
Benjamin Morel committed
249
     */
250 251 252 253 254
    public function getLength()
    {
        return $this->_length;
    }

Benjamin Morel's avatar
Benjamin Morel committed
255
    /**
256
     * @return int
Benjamin Morel's avatar
Benjamin Morel committed
257
     */
258 259 260 261 262
    public function getPrecision()
    {
        return $this->_precision;
    }

Benjamin Morel's avatar
Benjamin Morel committed
263
    /**
264
     * @return int
Benjamin Morel's avatar
Benjamin Morel committed
265
     */
266 267 268 269 270
    public function getScale()
    {
        return $this->_scale;
    }

Benjamin Morel's avatar
Benjamin Morel committed
271
    /**
272
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
273
     */
274 275 276 277 278
    public function getUnsigned()
    {
        return $this->_unsigned;
    }

Benjamin Morel's avatar
Benjamin Morel committed
279
    /**
280
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
281
     */
282 283 284 285 286
    public function getFixed()
    {
        return $this->_fixed;
    }

Benjamin Morel's avatar
Benjamin Morel committed
287
    /**
288
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
289
     */
290 291 292 293 294
    public function getNotnull()
    {
        return $this->_notnull;
    }

Benjamin Morel's avatar
Benjamin Morel committed
295 296 297
    /**
     * @return string|null
     */
298 299 300 301 302
    public function getDefault()
    {
        return $this->_default;
    }

Benjamin Morel's avatar
Benjamin Morel committed
303
    /**
304
     * @return mixed[]
Benjamin Morel's avatar
Benjamin Morel committed
305
     */
306 307 308 309 310
    public function getPlatformOptions()
    {
        return $this->_platformOptions;
    }

Benjamin Morel's avatar
Benjamin Morel committed
311 312 313
    /**
     * @param string $name
     *
314
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
315
     */
316 317 318 319 320
    public function hasPlatformOption($name)
    {
        return isset($this->_platformOptions[$name]);
    }

Benjamin Morel's avatar
Benjamin Morel committed
321 322 323 324 325
    /**
     * @param string $name
     *
     * @return mixed
     */
326 327 328 329 330
    public function getPlatformOption($name)
    {
        return $this->_platformOptions[$name];
    }

Benjamin Morel's avatar
Benjamin Morel committed
331 332 333
    /**
     * @return string|null
     */
334 335 336 337 338
    public function getColumnDefinition()
    {
        return $this->_columnDefinition;
    }

Benjamin Morel's avatar
Benjamin Morel committed
339
    /**
340
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
341
     */
342 343 344 345 346
    public function getAutoincrement()
    {
        return $this->_autoincrement;
    }

Benjamin Morel's avatar
Benjamin Morel committed
347
    /**
348
     * @param bool $flag
Benjamin Morel's avatar
Benjamin Morel committed
349
     *
jeroendedauw's avatar
jeroendedauw committed
350
     * @return Column
Benjamin Morel's avatar
Benjamin Morel committed
351
     */
352 353
    public function setAutoincrement($flag)
    {
354
        $this->_autoincrement = $flag;
355

356 357 358
        return $this;
    }

Benjamin Morel's avatar
Benjamin Morel committed
359
    /**
Sergei Morozov's avatar
Sergei Morozov committed
360
     * @param string|null $comment
Benjamin Morel's avatar
Benjamin Morel committed
361
     *
jeroendedauw's avatar
jeroendedauw committed
362
     * @return Column
Benjamin Morel's avatar
Benjamin Morel committed
363
     */
364 365 366
    public function setComment($comment)
    {
        $this->_comment = $comment;
Benjamin Morel's avatar
Benjamin Morel committed
367

368 369 370
        return $this;
    }

Benjamin Morel's avatar
Benjamin Morel committed
371 372 373
    /**
     * @return string|null
     */
374 375 376 377 378
    public function getComment()
    {
        return $this->_comment;
    }

379
    /**
Benjamin Morel's avatar
Benjamin Morel committed
380 381 382
     * @param string $name
     * @param mixed  $value
     *
jeroendedauw's avatar
jeroendedauw committed
383
     * @return Column
384 385 386 387
     */
    public function setCustomSchemaOption($name, $value)
    {
        $this->_customSchemaOptions[$name] = $value;
Benjamin Morel's avatar
Benjamin Morel committed
388

389 390 391 392
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
393 394
     * @param string $name
     *
395
     * @return bool
396 397 398 399 400 401 402
     */
    public function hasCustomSchemaOption($name)
    {
        return isset($this->_customSchemaOptions[$name]);
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
403 404
     * @param string $name
     *
405 406 407 408 409 410 411 412
     * @return mixed
     */
    public function getCustomSchemaOption($name)
    {
        return $this->_customSchemaOptions[$name];
    }

    /**
413
     * @param mixed[] $customSchemaOptions
Benjamin Morel's avatar
Benjamin Morel committed
414
     *
jeroendedauw's avatar
jeroendedauw committed
415
     * @return Column
416 417 418 419
     */
    public function setCustomSchemaOptions(array $customSchemaOptions)
    {
        $this->_customSchemaOptions = $customSchemaOptions;
Benjamin Morel's avatar
Benjamin Morel committed
420

421 422 423 424
        return $this;
    }

    /**
425
     * @return mixed[]
426 427 428 429 430 431
     */
    public function getCustomSchemaOptions()
    {
        return $this->_customSchemaOptions;
    }

432
    /**
433
     * @return mixed[]
434
     */
435
    public function toArray()
436
    {
437
        return array_merge([
438 439 440 441 442 443 444 445 446
            'name'          => $this->_name,
            'type'          => $this->_type,
            'default'       => $this->_default,
            'notnull'       => $this->_notnull,
            'length'        => $this->_length,
            'precision'     => $this->_precision,
            'scale'         => $this->_scale,
            'fixed'         => $this->_fixed,
            'unsigned'      => $this->_unsigned,
447
            'autoincrement' => $this->_autoincrement,
448
            'columnDefinition' => $this->_columnDefinition,
449
            'comment' => $this->_comment,
450
        ], $this->_platformOptions, $this->_customSchemaOptions);
451
    }
452
}