Column.php 9.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Schema;

Benjamin Morel's avatar
Benjamin Morel committed
22
use Doctrine\DBAL\Types\Type;
23 24 25 26 27 28
use const E_USER_DEPRECATED;
use function array_merge;
use function is_numeric;
use function method_exists;
use function sprintf;
use function trigger_error;
29 30

/**
Benjamin Morel's avatar
Benjamin Morel committed
31
 * Object representation of a database column.
32
 *
Benjamin Morel's avatar
Benjamin Morel committed
33 34 35
 * @link   www.doctrine-project.org
 * @since  2.0
 * @author Benjamin Eberlei <kontakt@beberlei.de>
36
 */
37
class Column extends AbstractAsset
38 39
{
    /**
jeroendedauw's avatar
jeroendedauw committed
40
     * @var Type
41 42 43 44
     */
    protected $_type;

    /**
45
     * @var int|null
46
     */
47
    protected $_length = null;
48 49

    /**
50
     * @var int
51
     */
52
    protected $_precision = 10;
53 54

    /**
55
     * @var int
56 57 58 59
     */
    protected $_scale = 0;

    /**
60
     * @var bool
61 62 63 64
     */
    protected $_unsigned = false;

    /**
65
     * @var bool
66 67 68 69
     */
    protected $_fixed = false;

    /**
70
     * @var bool
71 72 73 74
     */
    protected $_notnull = true;

    /**
Benjamin Morel's avatar
Benjamin Morel committed
75
     * @var string|null
76
     */
77
    protected $_default = null;
78

79
    /**
80
     * @var bool
81 82 83
     */
    protected $_autoincrement = false;

84 85 86
    /**
     * @var array
     */
87
    protected $_platformOptions = [];
88

89
    /**
Benjamin Morel's avatar
Benjamin Morel committed
90
     * @var string|null
91 92 93
     */
    protected $_columnDefinition = null;

94
    /**
Benjamin Morel's avatar
Benjamin Morel committed
95
     * @var string|null
96 97 98
     */
    protected $_comment = null;

99 100 101
    /**
     * @var array
     */
102
    protected $_customSchemaOptions = [];
103

104
    /**
Benjamin Morel's avatar
Benjamin Morel committed
105
     * Creates a new Column.
106
     *
jeroendedauw's avatar
jeroendedauw committed
107
     * @param string $columnName
108 109
     * @param Type   $type
     * @param array  $options
110
     */
111
    public function __construct($columnName, Type $type, array $options=[])
112 113 114 115 116 117 118 119
    {
        $this->_setName($columnName);
        $this->setType($type);
        $this->setOptions($options);
    }

    /**
     * @param array $options
Benjamin Morel's avatar
Benjamin Morel committed
120
     *
jeroendedauw's avatar
jeroendedauw committed
121
     * @return Column
122 123 124
     */
    public function setOptions(array $options)
    {
125
        foreach ($options as $name => $value) {
126
            $method = "set".$name;
127
            if ( ! method_exists($this, $method)) {
128
                // next major: throw an exception
129
                @trigger_error(sprintf(
130 131
                    'The "%s" column option is not supported,'.
                    ' setting it is deprecated and will cause an error in Doctrine 3.0',
132 133 134
                    $name
                ), E_USER_DEPRECATED);

135
                continue;
136
            }
137
            $this->$method($value);
138
        }
Benjamin Morel's avatar
Benjamin Morel committed
139

140 141 142 143
        return $this;
    }

    /**
jeroendedauw's avatar
jeroendedauw committed
144
     * @param Type $type
Benjamin Morel's avatar
Benjamin Morel committed
145
     *
jeroendedauw's avatar
jeroendedauw committed
146
     * @return Column
147 148 149 150
     */
    public function setType(Type $type)
    {
        $this->_type = $type;
151

152 153 154 155
        return $this;
    }

    /**
156
     * @param int|null $length
Benjamin Morel's avatar
Benjamin Morel committed
157
     *
jeroendedauw's avatar
jeroendedauw committed
158
     * @return Column
159 160 161
     */
    public function setLength($length)
    {
Steve Müller's avatar
Steve Müller committed
162
        if ($length !== null) {
163
            $this->_length = (int) $length;
164 165 166
        } else {
            $this->_length = null;
        }
Benjamin Morel's avatar
Benjamin Morel committed
167

168 169 170 171
        return $this;
    }

    /**
172
     * @param int $precision
Benjamin Morel's avatar
Benjamin Morel committed
173
     *
jeroendedauw's avatar
jeroendedauw committed
174
     * @return Column
175 176 177
     */
    public function setPrecision($precision)
    {
178 179 180 181
        if (!is_numeric($precision)) {
            $precision = 10; // defaults to 10 when no valid precision is given.
        }

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

184 185 186 187
        return $this;
    }

    /**
188
     * @param int $scale
Benjamin Morel's avatar
Benjamin Morel committed
189
     *
jeroendedauw's avatar
jeroendedauw committed
190
     * @return Column
191 192 193
     */
    public function setScale($scale)
    {
194 195 196 197
        if (!is_numeric($scale)) {
            $scale = 0;
        }

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

200 201 202 203
        return $this;
    }

    /**
204
     * @param bool $unsigned
205
     *
jeroendedauw's avatar
jeroendedauw committed
206
     * @return Column
207 208 209
     */
    public function setUnsigned($unsigned)
    {
210
        $this->_unsigned = (bool) $unsigned;
Benjamin Morel's avatar
Benjamin Morel committed
211

212 213 214 215
        return $this;
    }

    /**
216
     * @param bool $fixed
217
     *
jeroendedauw's avatar
jeroendedauw committed
218
     * @return Column
219 220 221
     */
    public function setFixed($fixed)
    {
222
        $this->_fixed = (bool) $fixed;
Benjamin Morel's avatar
Benjamin Morel committed
223

224 225 226 227
        return $this;
    }

    /**
228
     * @param bool $notnull
Benjamin Morel's avatar
Benjamin Morel committed
229
     *
jeroendedauw's avatar
jeroendedauw committed
230
     * @return Column
231 232 233
     */
    public function setNotnull($notnull)
    {
234
        $this->_notnull = (bool) $notnull;
Benjamin Morel's avatar
Benjamin Morel committed
235

236 237 238 239
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
240
     * @param mixed $default
241
     *
jeroendedauw's avatar
jeroendedauw committed
242
     * @return Column
243 244 245 246
     */
    public function setDefault($default)
    {
        $this->_default = $default;
Benjamin Morel's avatar
Benjamin Morel committed
247

248 249 250 251 252
        return $this;
    }

    /**
     * @param array $platformOptions
Benjamin Morel's avatar
Benjamin Morel committed
253
     *
jeroendedauw's avatar
jeroendedauw committed
254
     * @return Column
255 256 257 258
     */
    public function setPlatformOptions(array $platformOptions)
    {
        $this->_platformOptions = $platformOptions;
Benjamin Morel's avatar
Benjamin Morel committed
259

260 261 262 263
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
264 265
     * @param string $name
     * @param mixed  $value
266
     *
jeroendedauw's avatar
jeroendedauw committed
267
     * @return Column
268 269 270 271
     */
    public function setPlatformOption($name, $value)
    {
        $this->_platformOptions[$name] = $value;
Benjamin Morel's avatar
Benjamin Morel committed
272

273 274 275
        return $this;
    }

276
    /**
Benjamin Morel's avatar
Benjamin Morel committed
277
     * @param string $value
278
     *
jeroendedauw's avatar
jeroendedauw committed
279
     * @return Column
280 281 282 283
     */
    public function setColumnDefinition($value)
    {
        $this->_columnDefinition = $value;
Benjamin Morel's avatar
Benjamin Morel committed
284

285 286 287
        return $this;
    }

Benjamin Morel's avatar
Benjamin Morel committed
288
    /**
jeroendedauw's avatar
jeroendedauw committed
289
     * @return Type
Benjamin Morel's avatar
Benjamin Morel committed
290
     */
291 292 293 294 295
    public function getType()
    {
        return $this->_type;
    }

Benjamin Morel's avatar
Benjamin Morel committed
296
    /**
297
     * @return int|null
Benjamin Morel's avatar
Benjamin Morel committed
298
     */
299 300 301 302 303
    public function getLength()
    {
        return $this->_length;
    }

Benjamin Morel's avatar
Benjamin Morel committed
304
    /**
305
     * @return int
Benjamin Morel's avatar
Benjamin Morel committed
306
     */
307 308 309 310 311
    public function getPrecision()
    {
        return $this->_precision;
    }

Benjamin Morel's avatar
Benjamin Morel committed
312
    /**
313
     * @return int
Benjamin Morel's avatar
Benjamin Morel committed
314
     */
315 316 317 318 319
    public function getScale()
    {
        return $this->_scale;
    }

Benjamin Morel's avatar
Benjamin Morel committed
320
    /**
321
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
322
     */
323 324 325 326 327
    public function getUnsigned()
    {
        return $this->_unsigned;
    }

Benjamin Morel's avatar
Benjamin Morel committed
328
    /**
329
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
330
     */
331 332 333 334 335
    public function getFixed()
    {
        return $this->_fixed;
    }

Benjamin Morel's avatar
Benjamin Morel committed
336
    /**
337
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
338
     */
339 340 341 342 343
    public function getNotnull()
    {
        return $this->_notnull;
    }

Benjamin Morel's avatar
Benjamin Morel committed
344 345 346
    /**
     * @return string|null
     */
347 348 349 350 351
    public function getDefault()
    {
        return $this->_default;
    }

Benjamin Morel's avatar
Benjamin Morel committed
352 353 354
    /**
     * @return array
     */
355 356 357 358 359
    public function getPlatformOptions()
    {
        return $this->_platformOptions;
    }

Benjamin Morel's avatar
Benjamin Morel committed
360 361 362
    /**
     * @param string $name
     *
363
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
364
     */
365 366 367 368 369
    public function hasPlatformOption($name)
    {
        return isset($this->_platformOptions[$name]);
    }

Benjamin Morel's avatar
Benjamin Morel committed
370 371 372 373 374
    /**
     * @param string $name
     *
     * @return mixed
     */
375 376 377 378 379
    public function getPlatformOption($name)
    {
        return $this->_platformOptions[$name];
    }

Benjamin Morel's avatar
Benjamin Morel committed
380 381 382
    /**
     * @return string|null
     */
383 384 385 386 387
    public function getColumnDefinition()
    {
        return $this->_columnDefinition;
    }

Benjamin Morel's avatar
Benjamin Morel committed
388
    /**
389
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
390
     */
391 392 393 394 395
    public function getAutoincrement()
    {
        return $this->_autoincrement;
    }

Benjamin Morel's avatar
Benjamin Morel committed
396
    /**
397
     * @param bool $flag
Benjamin Morel's avatar
Benjamin Morel committed
398
     *
jeroendedauw's avatar
jeroendedauw committed
399
     * @return Column
Benjamin Morel's avatar
Benjamin Morel committed
400
     */
401 402
    public function setAutoincrement($flag)
    {
403
        $this->_autoincrement = $flag;
404

405 406 407
        return $this;
    }

Benjamin Morel's avatar
Benjamin Morel committed
408 409 410
    /**
     * @param string $comment
     *
jeroendedauw's avatar
jeroendedauw committed
411
     * @return Column
Benjamin Morel's avatar
Benjamin Morel committed
412
     */
413 414 415
    public function setComment($comment)
    {
        $this->_comment = $comment;
Benjamin Morel's avatar
Benjamin Morel committed
416

417 418 419
        return $this;
    }

Benjamin Morel's avatar
Benjamin Morel committed
420 421 422
    /**
     * @return string|null
     */
423 424 425 426 427
    public function getComment()
    {
        return $this->_comment;
    }

428
    /**
Benjamin Morel's avatar
Benjamin Morel committed
429 430 431
     * @param string $name
     * @param mixed  $value
     *
jeroendedauw's avatar
jeroendedauw committed
432
     * @return Column
433 434 435 436
     */
    public function setCustomSchemaOption($name, $value)
    {
        $this->_customSchemaOptions[$name] = $value;
Benjamin Morel's avatar
Benjamin Morel committed
437

438 439 440 441
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
442 443
     * @param string $name
     *
444
     * @return bool
445 446 447 448 449 450 451
     */
    public function hasCustomSchemaOption($name)
    {
        return isset($this->_customSchemaOptions[$name]);
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
452 453
     * @param string $name
     *
454 455 456 457 458 459 460 461 462
     * @return mixed
     */
    public function getCustomSchemaOption($name)
    {
        return $this->_customSchemaOptions[$name];
    }

    /**
     * @param array $customSchemaOptions
Benjamin Morel's avatar
Benjamin Morel committed
463
     *
jeroendedauw's avatar
jeroendedauw committed
464
     * @return Column
465 466 467 468
     */
    public function setCustomSchemaOptions(array $customSchemaOptions)
    {
        $this->_customSchemaOptions = $customSchemaOptions;
Benjamin Morel's avatar
Benjamin Morel committed
469

470 471 472 473 474 475 476 477 478 479 480
        return $this;
    }

    /**
     * @return array
     */
    public function getCustomSchemaOptions()
    {
        return $this->_customSchemaOptions;
    }

481
    /**
482
     * @return array
483
     */
484
    public function toArray()
485
    {
486
        return array_merge([
487 488 489 490 491 492 493 494 495
            '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,
496
            'autoincrement' => $this->_autoincrement,
497
            'columnDefinition' => $this->_columnDefinition,
498
            'comment' => $this->_comment,
499
        ], $this->_platformOptions, $this->_customSchemaOptions);
500
    }
501
}