Column.php 9.79 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
use Doctrine\DBAL\Schema\Visitor\Visitor;
24 25

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
40
     * @var integer|null
41
     */
42
    protected $_length = null;
43 44

    /**
Benjamin Morel's avatar
Benjamin Morel committed
45
     * @var integer
46
     */
47
    protected $_precision = 10;
48 49

    /**
Benjamin Morel's avatar
Benjamin Morel committed
50
     * @var integer
51 52 53 54
     */
    protected $_scale = 0;

    /**
Benjamin Morel's avatar
Benjamin Morel committed
55
     * @var boolean
56 57 58 59
     */
    protected $_unsigned = false;

    /**
Benjamin Morel's avatar
Benjamin Morel committed
60
     * @var boolean
61 62 63 64
     */
    protected $_fixed = false;

    /**
Benjamin Morel's avatar
Benjamin Morel committed
65
     * @var boolean
66 67 68 69
     */
    protected $_notnull = true;

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

74
    /**
Benjamin Morel's avatar
Benjamin Morel committed
75
     * @var boolean
76 77 78
     */
    protected $_autoincrement = false;

79 80 81 82 83
    /**
     * @var array
     */
    protected $_platformOptions = array();

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

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

94 95 96 97 98
    /**
     * @var array
     */
    protected $_customSchemaOptions = array();

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

    /**
     * @param array $options
Benjamin Morel's avatar
Benjamin Morel committed
115 116
     *
     * @return \Doctrine\DBAL\Schema\Column
117 118 119
     */
    public function setOptions(array $options)
    {
120
        foreach ($options as $name => $value) {
121 122 123 124 125
            $method = "set".$name;
            if (method_exists($this, $method)) {
                $this->$method($value);
            }
        }
Benjamin Morel's avatar
Benjamin Morel committed
126

127 128 129 130
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
131 132 133
     * @param \Doctrine\DBAL\Types\Type $type
     *
     * @return \Doctrine\DBAL\Schema\Column
134 135 136 137 138 139 140 141
     */
    public function setType(Type $type)
    {
        $this->_type = $type;
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
142 143 144
     * @param integer|null $length
     *
     * @return \Doctrine\DBAL\Schema\Column
145 146 147
     */
    public function setLength($length)
    {
148 149 150 151 152
        if($length !== null) {
            $this->_length = (int)$length;
        } else {
            $this->_length = null;
        }
Benjamin Morel's avatar
Benjamin Morel committed
153

154 155 156 157
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
158 159 160
     * @param integer $precision
     *
     * @return \Doctrine\DBAL\Schema\Column
161 162 163
     */
    public function setPrecision($precision)
    {
164 165 166 167
        if (!is_numeric($precision)) {
            $precision = 10; // defaults to 10 when no valid precision is given.
        }

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

170 171 172 173
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
174 175 176
     * @param integer $scale
     *
     * @return \Doctrine\DBAL\Schema\Column
177 178 179
     */
    public function setScale($scale)
    {
180 181 182 183 184
        if (!is_numeric($scale)) {
            $scale = 0;
        }

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

186 187 188 189
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
190
     * @param boolean $unsigned
191
     *
Benjamin Morel's avatar
Benjamin Morel committed
192
     * @return \Doctrine\DBAL\Schema\Column
193 194 195 196
     */
    public function setUnsigned($unsigned)
    {
        $this->_unsigned = (bool)$unsigned;
Benjamin Morel's avatar
Benjamin Morel committed
197

198 199 200 201
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
202
     * @param boolean $fixed
203
     *
Benjamin Morel's avatar
Benjamin Morel committed
204
     * @return \Doctrine\DBAL\Schema\Column
205 206 207 208
     */
    public function setFixed($fixed)
    {
        $this->_fixed = (bool)$fixed;
Benjamin Morel's avatar
Benjamin Morel committed
209

210 211 212 213
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
214 215 216
     * @param boolean $notnull
     *
     * @return \Doctrine\DBAL\Schema\Column
217 218 219 220
     */
    public function setNotnull($notnull)
    {
        $this->_notnull = (bool)$notnull;
Benjamin Morel's avatar
Benjamin Morel committed
221

222 223 224 225
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
226
     * @param mixed $default
227
     *
Benjamin Morel's avatar
Benjamin Morel committed
228
     * @return \Doctrine\DBAL\Schema\Column
229 230 231 232
     */
    public function setDefault($default)
    {
        $this->_default = $default;
Benjamin Morel's avatar
Benjamin Morel committed
233

234 235 236 237 238
        return $this;
    }

    /**
     * @param array $platformOptions
Benjamin Morel's avatar
Benjamin Morel committed
239 240
     *
     * @return \Doctrine\DBAL\Schema\Column
241 242 243 244
     */
    public function setPlatformOptions(array $platformOptions)
    {
        $this->_platformOptions = $platformOptions;
Benjamin Morel's avatar
Benjamin Morel committed
245

246 247 248 249
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
250 251
     * @param string $name
     * @param mixed  $value
252
     *
Benjamin Morel's avatar
Benjamin Morel committed
253
     * @return \Doctrine\DBAL\Schema\Column
254 255 256 257
     */
    public function setPlatformOption($name, $value)
    {
        $this->_platformOptions[$name] = $value;
Benjamin Morel's avatar
Benjamin Morel committed
258

259 260 261
        return $this;
    }

262
    /**
Benjamin Morel's avatar
Benjamin Morel committed
263
     * @param string $value
264
     *
Benjamin Morel's avatar
Benjamin Morel committed
265
     * @return \Doctrine\DBAL\Schema\Column
266 267 268 269
     */
    public function setColumnDefinition($value)
    {
        $this->_columnDefinition = $value;
Benjamin Morel's avatar
Benjamin Morel committed
270

271 272 273
        return $this;
    }

Benjamin Morel's avatar
Benjamin Morel committed
274 275 276
    /**
     * @return \Doctrine\DBAL\Types\Type
     */
277 278 279 280 281
    public function getType()
    {
        return $this->_type;
    }

Benjamin Morel's avatar
Benjamin Morel committed
282 283 284
    /**
     * @return integer|null
     */
285 286 287 288 289
    public function getLength()
    {
        return $this->_length;
    }

Benjamin Morel's avatar
Benjamin Morel committed
290 291 292
    /**
     * @return integer
     */
293 294 295 296 297
    public function getPrecision()
    {
        return $this->_precision;
    }

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

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

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

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

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

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

Benjamin Morel's avatar
Benjamin Morel committed
346 347 348 349 350
    /**
     * @param string $name
     *
     * @return boolean
     */
351 352 353 354 355
    public function hasPlatformOption($name)
    {
        return isset($this->_platformOptions[$name]);
    }

Benjamin Morel's avatar
Benjamin Morel committed
356 357 358 359 360
    /**
     * @param string $name
     *
     * @return mixed
     */
361 362 363 364 365
    public function getPlatformOption($name)
    {
        return $this->_platformOptions[$name];
    }

Benjamin Morel's avatar
Benjamin Morel committed
366 367 368
    /**
     * @return string|null
     */
369 370 371 372 373
    public function getColumnDefinition()
    {
        return $this->_columnDefinition;
    }

Benjamin Morel's avatar
Benjamin Morel committed
374 375 376
    /**
     * @return boolean
     */
377 378 379 380 381
    public function getAutoincrement()
    {
        return $this->_autoincrement;
    }

Benjamin Morel's avatar
Benjamin Morel committed
382 383 384 385 386
    /**
     * @param boolean $flag
     *
     * @return \Doctrine\DBAL\Schema\Column
     */
387 388
    public function setAutoincrement($flag)
    {
389
        $this->_autoincrement = $flag;
390 391 392
        return $this;
    }

Benjamin Morel's avatar
Benjamin Morel committed
393 394 395 396 397
    /**
     * @param string $comment
     *
     * @return \Doctrine\DBAL\Schema\Column
     */
398 399 400
    public function setComment($comment)
    {
        $this->_comment = $comment;
Benjamin Morel's avatar
Benjamin Morel committed
401

402 403 404
        return $this;
    }

Benjamin Morel's avatar
Benjamin Morel committed
405 406 407
    /**
     * @return string|null
     */
408 409 410 411 412
    public function getComment()
    {
        return $this->_comment;
    }

413
    /**
Benjamin Morel's avatar
Benjamin Morel committed
414 415 416 417
     * @param string $name
     * @param mixed  $value
     *
     * @return \Doctrine\DBAL\Schema\Column
418 419 420 421
     */
    public function setCustomSchemaOption($name, $value)
    {
        $this->_customSchemaOptions[$name] = $value;
Benjamin Morel's avatar
Benjamin Morel committed
422

423 424 425 426
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
427 428
     * @param string $name
     *
429 430 431 432 433 434 435 436
     * @return boolean
     */
    public function hasCustomSchemaOption($name)
    {
        return isset($this->_customSchemaOptions[$name]);
    }

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

    /**
     * @param array $customSchemaOptions
Benjamin Morel's avatar
Benjamin Morel committed
448 449
     *
     * @return \Doctrine\DBAL\Schema\Column
450 451 452 453
     */
    public function setCustomSchemaOptions(array $customSchemaOptions)
    {
        $this->_customSchemaOptions = $customSchemaOptions;
Benjamin Morel's avatar
Benjamin Morel committed
454

455 456 457 458 459 460 461 462 463 464 465
        return $this;
    }

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

466
    /**
Benjamin Morel's avatar
Benjamin Morel committed
467
     * @param \Doctrine\DBAL\Schema\Visitor\Visitor $visitor
468
     */
Benjamin Morel's avatar
Benjamin Morel committed
469
    public function visit(Visitor $visitor)
470 471 472
    {
        $visitor->accept($this);
    }
473 474

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