Column.php 9.36 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

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

    /**
39
     * @var int|null
40
     */
41
    protected $_length = null;
42 43

    /**
44
     * @var int
45
     */
46
    protected $_precision = 10;
47 48

    /**
49
     * @var int
50 51 52 53
     */
    protected $_scale = 0;

    /**
54
     * @var bool
55 56 57 58
     */
    protected $_unsigned = false;

    /**
59
     * @var bool
60 61 62 63
     */
    protected $_fixed = false;

    /**
64
     * @var bool
65 66 67 68
     */
    protected $_notnull = true;

    /**
Benjamin Morel's avatar
Benjamin Morel committed
69
     * @var string|null
70
     */
71
    protected $_default = null;
72

73
    /**
74
     * @var bool
75 76 77
     */
    protected $_autoincrement = false;

78 79 80
    /**
     * @var array
     */
81
    protected $_platformOptions = [];
82

83
    /**
Benjamin Morel's avatar
Benjamin Morel committed
84
     * @var string|null
85 86 87
     */
    protected $_columnDefinition = null;

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

93 94 95
    /**
     * @var array
     */
96
    protected $_customSchemaOptions = [];
97

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

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

                return $this;
130
            }
131
            $this->$method($value);
132
        }
Benjamin Morel's avatar
Benjamin Morel committed
133

134 135 136 137
        return $this;
    }

    /**
jeroendedauw's avatar
jeroendedauw committed
138
     * @param Type $type
Benjamin Morel's avatar
Benjamin Morel committed
139
     *
jeroendedauw's avatar
jeroendedauw committed
140
     * @return Column
141 142 143 144
     */
    public function setType(Type $type)
    {
        $this->_type = $type;
145

146 147 148 149
        return $this;
    }

    /**
150
     * @param int|null $length
Benjamin Morel's avatar
Benjamin Morel committed
151
     *
jeroendedauw's avatar
jeroendedauw committed
152
     * @return Column
153 154 155
     */
    public function setLength($length)
    {
Steve Müller's avatar
Steve Müller committed
156
        if ($length !== null) {
157
            $this->_length = (int) $length;
158 159 160
        } else {
            $this->_length = null;
        }
Benjamin Morel's avatar
Benjamin Morel committed
161

162 163 164 165
        return $this;
    }

    /**
166
     * @param int $precision
Benjamin Morel's avatar
Benjamin Morel committed
167
     *
jeroendedauw's avatar
jeroendedauw committed
168
     * @return Column
169 170 171
     */
    public function setPrecision($precision)
    {
172 173 174 175
        if (!is_numeric($precision)) {
            $precision = 10; // defaults to 10 when no valid precision is given.
        }

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

178 179 180 181
        return $this;
    }

    /**
182
     * @param int $scale
Benjamin Morel's avatar
Benjamin Morel committed
183
     *
jeroendedauw's avatar
jeroendedauw committed
184
     * @return Column
185 186 187
     */
    public function setScale($scale)
    {
188 189 190 191
        if (!is_numeric($scale)) {
            $scale = 0;
        }

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

194 195 196 197
        return $this;
    }

    /**
198
     * @param bool $unsigned
199
     *
jeroendedauw's avatar
jeroendedauw committed
200
     * @return Column
201 202 203
     */
    public function setUnsigned($unsigned)
    {
204
        $this->_unsigned = (bool) $unsigned;
Benjamin Morel's avatar
Benjamin Morel committed
205

206 207 208 209
        return $this;
    }

    /**
210
     * @param bool $fixed
211
     *
jeroendedauw's avatar
jeroendedauw committed
212
     * @return Column
213 214 215
     */
    public function setFixed($fixed)
    {
216
        $this->_fixed = (bool) $fixed;
Benjamin Morel's avatar
Benjamin Morel committed
217

218 219 220 221
        return $this;
    }

    /**
222
     * @param bool $notnull
Benjamin Morel's avatar
Benjamin Morel committed
223
     *
jeroendedauw's avatar
jeroendedauw committed
224
     * @return Column
225 226 227
     */
    public function setNotnull($notnull)
    {
228
        $this->_notnull = (bool) $notnull;
Benjamin Morel's avatar
Benjamin Morel committed
229

230 231 232 233
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
234
     * @param mixed $default
235
     *
jeroendedauw's avatar
jeroendedauw committed
236
     * @return Column
237 238 239 240
     */
    public function setDefault($default)
    {
        $this->_default = $default;
Benjamin Morel's avatar
Benjamin Morel committed
241

242 243 244 245 246
        return $this;
    }

    /**
     * @param array $platformOptions
Benjamin Morel's avatar
Benjamin Morel committed
247
     *
jeroendedauw's avatar
jeroendedauw committed
248
     * @return Column
249 250 251 252
     */
    public function setPlatformOptions(array $platformOptions)
    {
        $this->_platformOptions = $platformOptions;
Benjamin Morel's avatar
Benjamin Morel committed
253

254 255 256 257
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
258 259
     * @param string $name
     * @param mixed  $value
260
     *
jeroendedauw's avatar
jeroendedauw committed
261
     * @return Column
262 263 264 265
     */
    public function setPlatformOption($name, $value)
    {
        $this->_platformOptions[$name] = $value;
Benjamin Morel's avatar
Benjamin Morel committed
266

267 268 269
        return $this;
    }

270
    /**
Benjamin Morel's avatar
Benjamin Morel committed
271
     * @param string $value
272
     *
jeroendedauw's avatar
jeroendedauw committed
273
     * @return Column
274 275 276 277
     */
    public function setColumnDefinition($value)
    {
        $this->_columnDefinition = $value;
Benjamin Morel's avatar
Benjamin Morel committed
278

279 280 281
        return $this;
    }

Benjamin Morel's avatar
Benjamin Morel committed
282
    /**
jeroendedauw's avatar
jeroendedauw committed
283
     * @return Type
Benjamin Morel's avatar
Benjamin Morel committed
284
     */
285 286 287 288 289
    public function getType()
    {
        return $this->_type;
    }

Benjamin Morel's avatar
Benjamin Morel committed
290
    /**
291
     * @return int|null
Benjamin Morel's avatar
Benjamin Morel committed
292
     */
293 294 295 296 297
    public function getLength()
    {
        return $this->_length;
    }

Benjamin Morel's avatar
Benjamin Morel committed
298
    /**
299
     * @return int
Benjamin Morel's avatar
Benjamin Morel committed
300
     */
301 302 303 304 305
    public function getPrecision()
    {
        return $this->_precision;
    }

Benjamin Morel's avatar
Benjamin Morel committed
306
    /**
307
     * @return int
Benjamin Morel's avatar
Benjamin Morel committed
308
     */
309 310 311 312 313
    public function getScale()
    {
        return $this->_scale;
    }

Benjamin Morel's avatar
Benjamin Morel committed
314
    /**
315
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
316
     */
317 318 319 320 321
    public function getUnsigned()
    {
        return $this->_unsigned;
    }

Benjamin Morel's avatar
Benjamin Morel committed
322
    /**
323
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
324
     */
325 326 327 328 329
    public function getFixed()
    {
        return $this->_fixed;
    }

Benjamin Morel's avatar
Benjamin Morel committed
330
    /**
331
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
332
     */
333 334 335 336 337
    public function getNotnull()
    {
        return $this->_notnull;
    }

Benjamin Morel's avatar
Benjamin Morel committed
338 339 340
    /**
     * @return string|null
     */
341 342 343 344 345
    public function getDefault()
    {
        return $this->_default;
    }

Benjamin Morel's avatar
Benjamin Morel committed
346 347 348
    /**
     * @return array
     */
349 350 351 352 353
    public function getPlatformOptions()
    {
        return $this->_platformOptions;
    }

Benjamin Morel's avatar
Benjamin Morel committed
354 355 356
    /**
     * @param string $name
     *
357
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
358
     */
359 360 361 362 363
    public function hasPlatformOption($name)
    {
        return isset($this->_platformOptions[$name]);
    }

Benjamin Morel's avatar
Benjamin Morel committed
364 365 366 367 368
    /**
     * @param string $name
     *
     * @return mixed
     */
369 370 371 372 373
    public function getPlatformOption($name)
    {
        return $this->_platformOptions[$name];
    }

Benjamin Morel's avatar
Benjamin Morel committed
374 375 376
    /**
     * @return string|null
     */
377 378 379 380 381
    public function getColumnDefinition()
    {
        return $this->_columnDefinition;
    }

Benjamin Morel's avatar
Benjamin Morel committed
382
    /**
383
     * @return bool
Benjamin Morel's avatar
Benjamin Morel committed
384
     */
385 386 387 388 389
    public function getAutoincrement()
    {
        return $this->_autoincrement;
    }

Benjamin Morel's avatar
Benjamin Morel committed
390
    /**
391
     * @param bool $flag
Benjamin Morel's avatar
Benjamin Morel committed
392
     *
jeroendedauw's avatar
jeroendedauw committed
393
     * @return Column
Benjamin Morel's avatar
Benjamin Morel committed
394
     */
395 396
    public function setAutoincrement($flag)
    {
397
        $this->_autoincrement = $flag;
398

399 400 401
        return $this;
    }

Benjamin Morel's avatar
Benjamin Morel committed
402 403 404
    /**
     * @param string $comment
     *
jeroendedauw's avatar
jeroendedauw committed
405
     * @return Column
Benjamin Morel's avatar
Benjamin Morel committed
406
     */
407 408 409
    public function setComment($comment)
    {
        $this->_comment = $comment;
Benjamin Morel's avatar
Benjamin Morel committed
410

411 412 413
        return $this;
    }

Benjamin Morel's avatar
Benjamin Morel committed
414 415 416
    /**
     * @return string|null
     */
417 418 419 420 421
    public function getComment()
    {
        return $this->_comment;
    }

422
    /**
Benjamin Morel's avatar
Benjamin Morel committed
423 424 425
     * @param string $name
     * @param mixed  $value
     *
jeroendedauw's avatar
jeroendedauw committed
426
     * @return Column
427 428 429 430
     */
    public function setCustomSchemaOption($name, $value)
    {
        $this->_customSchemaOptions[$name] = $value;
Benjamin Morel's avatar
Benjamin Morel committed
431

432 433 434 435
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
436 437
     * @param string $name
     *
438
     * @return bool
439 440 441 442 443 444 445
     */
    public function hasCustomSchemaOption($name)
    {
        return isset($this->_customSchemaOptions[$name]);
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
446 447
     * @param string $name
     *
448 449 450 451 452 453 454 455 456
     * @return mixed
     */
    public function getCustomSchemaOption($name)
    {
        return $this->_customSchemaOptions[$name];
    }

    /**
     * @param array $customSchemaOptions
Benjamin Morel's avatar
Benjamin Morel committed
457
     *
jeroendedauw's avatar
jeroendedauw committed
458
     * @return Column
459 460 461 462
     */
    public function setCustomSchemaOptions(array $customSchemaOptions)
    {
        $this->_customSchemaOptions = $customSchemaOptions;
Benjamin Morel's avatar
Benjamin Morel committed
463

464 465 466 467 468 469 470 471 472 473 474
        return $this;
    }

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

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