Column.php 9.13 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;

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

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

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

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

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

    /**
Benjamin Morel's avatar
Benjamin Morel committed
64
     * @var boolean
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
    /**
Benjamin Morel's avatar
Benjamin Morel committed
74
     * @var boolean
75 76 77
     */
    protected $_autoincrement = false;

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

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 96 97
    /**
     * @var array
     */
    protected $_customSchemaOptions = array();

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 106 107 108 109 110 111 112 113
     */
    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
114
     *
jeroendedauw's avatar
jeroendedauw committed
115
     * @return Column
116 117 118
     */
    public function setOptions(array $options)
    {
119
        foreach ($options as $name => $value) {
120 121 122 123 124
            $method = "set".$name;
            if (method_exists($this, $method)) {
                $this->$method($value);
            }
        }
Benjamin Morel's avatar
Benjamin Morel committed
125

126 127 128 129
        return $this;
    }

    /**
jeroendedauw's avatar
jeroendedauw committed
130
     * @param Type $type
Benjamin Morel's avatar
Benjamin Morel committed
131
     *
jeroendedauw's avatar
jeroendedauw committed
132
     * @return Column
133 134 135 136
     */
    public function setType(Type $type)
    {
        $this->_type = $type;
137

138 139 140 141
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
142 143
     * @param integer|null $length
     *
jeroendedauw's avatar
jeroendedauw committed
144
     * @return Column
145 146 147
     */
    public function setLength($length)
    {
Steve Müller's avatar
Steve Müller committed
148
        if ($length !== null) {
149
            $this->_length = (int) $length;
150 151 152
        } 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
     * @param integer $precision
     *
jeroendedauw's avatar
jeroendedauw committed
160
     * @return 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
     * @param integer $scale
     *
jeroendedauw's avatar
jeroendedauw committed
176
     * @return Column
177 178 179
     */
    public function setScale($scale)
    {
180 181 182 183
        if (!is_numeric($scale)) {
            $scale = 0;
        }

184
        $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
     *
jeroendedauw's avatar
jeroendedauw committed
192
     * @return Column
193 194 195
     */
    public function setUnsigned($unsigned)
    {
196
        $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
     *
jeroendedauw's avatar
jeroendedauw committed
204
     * @return Column
205 206 207
     */
    public function setFixed($fixed)
    {
208
        $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
     * @param boolean $notnull
     *
jeroendedauw's avatar
jeroendedauw committed
216
     * @return Column
217 218 219
     */
    public function setNotnull($notnull)
    {
220
        $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
     *
jeroendedauw's avatar
jeroendedauw committed
228
     * @return 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
     *
jeroendedauw's avatar
jeroendedauw committed
240
     * @return 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
     *
jeroendedauw's avatar
jeroendedauw committed
253
     * @return 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
     *
jeroendedauw's avatar
jeroendedauw committed
265
     * @return 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
    /**
jeroendedauw's avatar
jeroendedauw committed
275
     * @return Type
Benjamin Morel's avatar
Benjamin Morel committed
276
     */
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
    /**
     * @param boolean $flag
     *
jeroendedauw's avatar
jeroendedauw committed
385
     * @return Column
Benjamin Morel's avatar
Benjamin Morel committed
386
     */
387 388
    public function setAutoincrement($flag)
    {
389
        $this->_autoincrement = $flag;
390

391 392 393
        return $this;
    }

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

403 404 405
        return $this;
    }

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

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

424 425 426 427
        return $this;
    }

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

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

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

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

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

467
    /**
468
     * @return array
469
     */
470
    public function toArray()
471
    {
472 473 474 475 476 477 478 479 480 481
        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,
482
            'autoincrement' => $this->_autoincrement,
483
            'columnDefinition' => $this->_columnDefinition,
484
            'comment' => $this->_comment,
485
        ), $this->_platformOptions, $this->_customSchemaOptions);
486
    }
487
}