Column.php 6.93 KB
Newer Older
1 2
<?php

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5 6
namespace Doctrine\DBAL\Schema;

Benjamin Morel's avatar
Benjamin Morel committed
7
use Doctrine\DBAL\Types\Type;
8 9 10 11 12
use const E_USER_DEPRECATED;
use function array_merge;
use function method_exists;
use function sprintf;
use function trigger_error;
13 14

/**
Benjamin Morel's avatar
Benjamin Morel committed
15
 * Object representation of a database column.
16
 */
17
class Column extends AbstractAsset
18
{
19
    /** @var Type */
20 21
    protected $_type;

22
    /** @var int|null */
23
    protected $_length;
24

25
    /** @var int|null */
26
    protected $_precision = 10;
27

28
    /** @var int */
29 30
    protected $_scale = 0;

31
    /** @var bool */
32 33
    protected $_unsigned = false;

34
    /** @var bool */
35 36
    protected $_fixed = false;

37
    /** @var bool */
38 39
    protected $_notnull = true;

40 41
    /** @var mixed */
    protected $_default;
42

43
    /** @var bool */
44 45
    protected $_autoincrement = false;

46
    /** @var array<string, mixed> */
47
    protected $_platformOptions = [];
48

49
    /** @var string|null */
50
    protected $_columnDefinition;
51

52
    /** @var string|null */
53
    protected $_comment;
54

55
    /** @var array<string, mixed> */
56
    protected $_customSchemaOptions = [];
57

58
    /**
Benjamin Morel's avatar
Benjamin Morel committed
59
     * Creates a new Column.
60
     *
61
     * @param array<string, mixed> $options
62
     */
63
    public function __construct(string $name, Type $type, array $options = [])
64
    {
65
        $this->_setName($name);
66 67 68 69 70
        $this->setType($type);
        $this->setOptions($options);
    }

    /**
71
     * @param array<string, mixed> $options
72
     */
73
    public function setOptions(array $options) : self
74
    {
75
        foreach ($options as $name => $value) {
76 77
            $method = 'set' . $name;
            if (! method_exists($this, $method)) {
78
                // next major: throw an exception
79
                @trigger_error(sprintf(
80
                    'The "%s" column option is not supported,' .
81
                    ' setting it is deprecated and will cause an error in Doctrine 3.0',
82 83 84
                    $name
                ), E_USER_DEPRECATED);

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

90 91 92
        return $this;
    }

93
    public function setType(Type $type) : self
94 95
    {
        $this->_type = $type;
96

97 98 99
        return $this;
    }

100
    public function setLength(?int $length) : self
101
    {
102
        $this->_length = $length;
Benjamin Morel's avatar
Benjamin Morel committed
103

104 105 106
        return $this;
    }

107
    public function setPrecision(?int $precision) : self
108
    {
109 110
        if ($precision === null) {
            $precision = 10; // defaults to 10 when no precision is given.
111 112
        }

113
        $this->_precision = $precision;
Benjamin Morel's avatar
Benjamin Morel committed
114

115 116 117
        return $this;
    }

118
    public function setScale(int $scale) : self
119
    {
120
        $this->_scale = $scale;
Benjamin Morel's avatar
Benjamin Morel committed
121

122 123 124
        return $this;
    }

125
    public function setUnsigned(bool $unsigned) : self
126
    {
127
        $this->_unsigned = $unsigned;
Benjamin Morel's avatar
Benjamin Morel committed
128

129 130 131
        return $this;
    }

132
    public function setFixed(bool $fixed) : self
133
    {
134
        $this->_fixed = $fixed;
Benjamin Morel's avatar
Benjamin Morel committed
135

136 137 138
        return $this;
    }

139
    public function setNotnull(bool $notnull) : self
140
    {
141
        $this->_notnull = $notnull;
Benjamin Morel's avatar
Benjamin Morel committed
142

143 144 145 146
        return $this;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
147
     * @param mixed $default
148
     */
149
    public function setDefault($default) : self
150 151
    {
        $this->_default = $default;
Benjamin Morel's avatar
Benjamin Morel committed
152

153 154 155 156
        return $this;
    }

    /**
157
     * @param array<string, mixed> $platformOptions
158
     */
159
    public function setPlatformOptions(array $platformOptions) : self
160 161
    {
        $this->_platformOptions = $platformOptions;
Benjamin Morel's avatar
Benjamin Morel committed
162

163 164 165 166
        return $this;
    }

    /**
167
     * @param mixed $value
168
     */
169
    public function setPlatformOption(string $name, $value) : self
170 171
    {
        $this->_platformOptions[$name] = $value;
Benjamin Morel's avatar
Benjamin Morel committed
172

173 174 175
        return $this;
    }

176
    public function setColumnDefinition(string $value) : self
177 178
    {
        $this->_columnDefinition = $value;
Benjamin Morel's avatar
Benjamin Morel committed
179

180 181 182
        return $this;
    }

183
    public function getType() : Type
184 185 186 187
    {
        return $this->_type;
    }

188
    public function getLength() : ?int
189 190 191 192
    {
        return $this->_length;
    }

193
    public function getPrecision() : ?int
194 195 196 197
    {
        return $this->_precision;
    }

198
    public function getScale() : ?int
199 200 201 202
    {
        return $this->_scale;
    }

203
    public function getUnsigned() : bool
204 205 206 207
    {
        return $this->_unsigned;
    }

208
    public function getFixed() : bool
209 210 211 212
    {
        return $this->_fixed;
    }

213
    public function getNotnull() : bool
214 215 216 217
    {
        return $this->_notnull;
    }

Benjamin Morel's avatar
Benjamin Morel committed
218
    /**
219
     * @return mixed
Benjamin Morel's avatar
Benjamin Morel committed
220
     */
221 222 223 224 225
    public function getDefault()
    {
        return $this->_default;
    }

Benjamin Morel's avatar
Benjamin Morel committed
226
    /**
227
     * @return array<string, mixed>
Benjamin Morel's avatar
Benjamin Morel committed
228
     */
229
    public function getPlatformOptions() : array
230 231 232 233
    {
        return $this->_platformOptions;
    }

234
    public function hasPlatformOption(string $name) : bool
235 236 237 238
    {
        return isset($this->_platformOptions[$name]);
    }

Benjamin Morel's avatar
Benjamin Morel committed
239 240 241
    /**
     * @return mixed
     */
242
    public function getPlatformOption(string $name)
243 244 245 246
    {
        return $this->_platformOptions[$name];
    }

247
    public function getColumnDefinition() : ?string
248 249 250 251
    {
        return $this->_columnDefinition;
    }

252
    public function getAutoincrement() : bool
253 254 255 256
    {
        return $this->_autoincrement;
    }

257
    public function setAutoincrement(bool $flag) : self
258
    {
259
        $this->_autoincrement = $flag;
260

261 262 263
        return $this;
    }

264
    public function setComment(?string $comment) : self
265 266
    {
        $this->_comment = $comment;
Benjamin Morel's avatar
Benjamin Morel committed
267

268 269 270
        return $this;
    }

271
    public function getComment() : ?string
272 273 274 275
    {
        return $this->_comment;
    }

276
    /**
277
     * @param mixed $value
278
     */
279
    public function setCustomSchemaOption(string $name, $value) : self
280 281
    {
        $this->_customSchemaOptions[$name] = $value;
Benjamin Morel's avatar
Benjamin Morel committed
282

283 284 285
        return $this;
    }

286
    public function hasCustomSchemaOption(string $name) : bool
287 288 289 290 291 292 293
    {
        return isset($this->_customSchemaOptions[$name]);
    }

    /**
     * @return mixed
     */
294
    public function getCustomSchemaOption(string $name)
295 296 297 298 299
    {
        return $this->_customSchemaOptions[$name];
    }

    /**
300
     * @param array<string, mixed> $customSchemaOptions
301
     */
302
    public function setCustomSchemaOptions(array $customSchemaOptions) : self
303 304
    {
        $this->_customSchemaOptions = $customSchemaOptions;
Benjamin Morel's avatar
Benjamin Morel committed
305

306 307 308 309
        return $this;
    }

    /**
310
     * @return array<string, mixed>
311
     */
312
    public function getCustomSchemaOptions() : array
313 314 315 316
    {
        return $this->_customSchemaOptions;
    }

317
    /**
318
     * @return array<string, mixed>
319
     */
320
    public function toArray() : array
321
    {
322
        return array_merge([
323 324 325 326 327 328 329 330 331
            '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,
332
            'autoincrement' => $this->_autoincrement,
333
            'columnDefinition' => $this->_columnDefinition,
334
            'comment' => $this->_comment,
335
        ], $this->_platformOptions, $this->_customSchemaOptions);
336
    }
337
}