Column.php 8.27 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 11
use const E_USER_DEPRECATED;
use function array_merge;
use function is_numeric;
use function method_exists;
use function sprintf;
use function trigger_error;
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 3.0',
84 85 86
                    $name
                ), E_USER_DEPRECATED);

87
                continue;
88
            }
89
            $this->$method($value);
90
        }
Benjamin Morel's avatar
Benjamin Morel committed
91

92 93 94 95
        return $this;
    }

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

102 103 104 105
        return $this;
    }

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

118 119 120 121
        return $this;
    }

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

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

134 135 136 137
        return $this;
    }

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

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

150 151 152 153
        return $this;
    }

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

162 163 164 165
        return $this;
    }

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

174 175 176 177
        return $this;
    }

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

186 187 188 189
        return $this;
    }

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

198 199 200 201
        return $this;
    }

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

210 211 212 213
        return $this;
    }

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

223 224 225
        return $this;
    }

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

235 236 237
        return $this;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

355 356 357
        return $this;
    }

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

367 368 369
        return $this;
    }

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

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

388 389 390 391
        return $this;
    }

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

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

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

420 421 422 423
        return $this;
    }

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

431
    /**
432
     * @return mixed[]
433
     */
434
    public function toArray()
435
    {
436
        return array_merge([
437 438 439 440 441 442 443 444 445
            '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,
446
            'autoincrement' => $this->_autoincrement,
447
            'columnDefinition' => $this->_columnDefinition,
448
            'comment' => $this->_comment,
449
        ], $this->_platformOptions, $this->_customSchemaOptions);
450
    }
451
}