Completed
Pull Request — develop (#3533)
by
unknown
16:31 queued 01:26
created

Column::setType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Schema;
6
7
use Doctrine\DBAL\Types\Type;
8
use const E_USER_DEPRECATED;
9
use function array_merge;
10
use function method_exists;
11
use function sprintf;
12
use function trigger_error;
13
14
/**
15
 * Object representation of a database column.
16
 */
17
class Column extends AbstractAsset
18
{
19
    /** @var Type */
20
    protected $_type;
21
22
    /** @var int|null */
23
    protected $_length;
24
25
    /** @var int|null */
26
    protected $_precision = 10;
27
28
    /** @var int */
29
    protected $_scale = 0;
30
31
    /** @var bool */
32
    protected $_unsigned = false;
33
34
    /** @var bool */
35
    protected $_fixed = false;
36
37
    /** @var bool */
38
    protected $_notnull = true;
39
40
    /** @var mixed */
41
    protected $_default;
42
43
    /** @var bool */
44
    protected $_autoincrement = false;
45
46
    /** @var mixed[] */
47
    protected $_platformOptions = [];
48
49
    /** @var string|null */
50
    protected $_columnDefinition;
51
52
    /** @var string|null */
53
    protected $_comment;
54
55
    /** @var mixed[] */
56
    protected $_customSchemaOptions = [];
57
58
    /**
59
     * Creates a new Column.
60
     *
61
     * @param mixed[] $options
62
     */
63 19765
    public function __construct(string $name, Type $type, array $options = [])
64
    {
65 19765
        $this->_setName($name);
66 19765
        $this->setType($type);
67 19765
        $this->setOptions($options);
68 19765
    }
69
70
    /**
71
     * @param mixed[] $options
72
     */
73 19765
    public function setOptions(array $options) : self
74
    {
75 19765
        foreach ($options as $name => $value) {
76 19270
            $method = 'set' . $name;
77 19270
            if (! method_exists($this, $method)) {
78
                // next major: throw an exception
79 3200
                @trigger_error(sprintf(
80
                    'The "%s" column option is not supported,' .
81 2
                    ' setting it is deprecated and will cause an error in Doctrine 3.0',
82 3200
                    $name
83 3200
                ), E_USER_DEPRECATED);
84
85 3200
                continue;
86
            }
87 19269
            $this->$method($value);
88
        }
89
90 19765
        return $this;
91
    }
92
93 19765
    public function setType(Type $type) : self
94
    {
95 19765
        $this->_type = $type;
96
97 19765
        return $this;
98
    }
99
100 18441
    public function setLength(?int $length) : self
101
    {
102 18441
        $this->_length = $length;
103
104 18441
        return $this;
105
    }
106
107 18213
    public function setPrecision(?int $precision) : self
108
    {
109 18213
        if ($precision === null) {
110 16429
            $precision = 10; // defaults to 10 when no precision is given.
111
        }
112
113 18213
        $this->_precision = $precision;
114
115 18213
        return $this;
116
    }
117
118 18212
    public function setScale(int $scale) : self
119
    {
120 18212
        $this->_scale = $scale;
121
122 18212
        return $this;
123
    }
124
125 17061
    public function setUnsigned(bool $unsigned) : self
126
    {
127 17061
        $this->_unsigned = $unsigned;
128
129 17061
        return $this;
130
    }
131
132 18384
    public function setFixed(bool $fixed) : self
133
    {
134 18384
        $this->_fixed = $fixed;
135
136 18384
        return $this;
137
    }
138
139 19485
    public function setNotnull(bool $notnull) : self
140
    {
141 19485
        $this->_notnull = $notnull;
142
143 19485
        return $this;
144
    }
145
146
    /**
147
     * @param mixed $default
148
     */
149 18235
    public function setDefault($default) : self
150
    {
151 18235
        $this->_default = $default;
152
153 18235
        return $this;
154
    }
155
156
    /**
157
     * @param mixed[] $platformOptions
158
     */
159 14873
    public function setPlatformOptions(array $platformOptions) : self
160
    {
161 14873
        $this->_platformOptions = $platformOptions;
162
163 14873
        return $this;
164
    }
165
166
    /**
167
     * @param mixed $value
168
     */
169 17958
    public function setPlatformOption(string $name, $value) : self
170
    {
171 17958
        $this->_platformOptions[$name] = $value;
172
173 17958
        return $this;
174
    }
175
176 10897
    public function setColumnDefinition(string $value) : self
177
    {
178 10897
        $this->_columnDefinition = $value;
179
180 10897
        return $this;
181
    }
182
183 19544
    public function getType() : Type
184
    {
185 19544
        return $this->_type;
186
    }
187
188 17325
    public function getLength() : ?int
189
    {
190 17325
        return $this->_length;
191
    }
192
193 17315
    public function getPrecision() : ?int
194
    {
195 17315
        return $this->_precision;
196
    }
197
198 17315
    public function getScale() : ?int
199
    {
200 17315
        return $this->_scale;
201
    }
202
203 17465
    public function getUnsigned() : bool
204
    {
205 17465
        return $this->_unsigned;
206
    }
207
208 17409
    public function getFixed() : bool
209
    {
210 17409
        return $this->_fixed;
211
    }
212
213 17484
    public function getNotnull() : bool
214
    {
215 17484
        return $this->_notnull;
216
    }
217
218
    /**
219
     * @return mixed
220
     */
221 17651
    public function getDefault()
222
    {
223 17651
        return $this->_default;
224
    }
225
226
    /**
227
     * @return mixed[]
228
     */
229 18298
    public function getPlatformOptions() : array
230
    {
231 18298
        return $this->_platformOptions;
232
    }
233
234 19416
    public function hasPlatformOption(string $name) : bool
235
    {
236 19416
        return isset($this->_platformOptions[$name]);
237
    }
238
239
    /**
240
     * @return mixed
241
     */
242 16551
    public function getPlatformOption(string $name)
243
    {
244 16551
        return $this->_platformOptions[$name];
245
    }
246
247
    public function getColumnDefinition() : ?string
248
    {
249
        return $this->_columnDefinition;
250
    }
251
252 18103
    public function getAutoincrement() : bool
253
    {
254 18103
        return $this->_autoincrement;
255
    }
256
257 18537
    public function setAutoincrement(bool $flag) : self
258
    {
259 18537
        $this->_autoincrement = $flag;
260
261 18537
        return $this;
262
    }
263
264 18289
    public function setComment(?string $comment) : self
265
    {
266 18289
        $this->_comment = $comment;
267
268 18289
        return $this;
269
    }
270
271 19535
    public function getComment() : ?string
272
    {
273 19535
        return $this->_comment;
274
    }
275
276
    /**
277
     * @param mixed $value
278
     */
279 2783
    public function setCustomSchemaOption(string $name, $value) : self
280
    {
281 2783
        $this->_customSchemaOptions[$name] = $value;
282
283 2783
        return $this;
284
    }
285
286 3251
    public function hasCustomSchemaOption(string $name) : bool
287
    {
288 3251
        return isset($this->_customSchemaOptions[$name]);
289
    }
290
291
    /**
292
     * @return mixed
293
     */
294 3251
    public function getCustomSchemaOption(string $name)
295
    {
296 3251
        return $this->_customSchemaOptions[$name];
297
    }
298
299
    /**
300
     * @param mixed[] $customSchemaOptions
301
     */
302 3253
    public function setCustomSchemaOptions(array $customSchemaOptions) : self
303
    {
304 3253
        $this->_customSchemaOptions = $customSchemaOptions;
305
306 3253
        return $this;
307
    }
308
309
    /**
310
     * @return mixed[]
311
     */
312 18297
    public function getCustomSchemaOptions() : array
313
    {
314 18297
        return $this->_customSchemaOptions;
315
    }
316
317
    /**
318
     * @return mixed[]
319
     */
320 19644
    public function toArray() : array
321
    {
322 19644
        return array_merge([
323 19644
            'name'          => $this->_name,
324 19644
            'type'          => $this->_type,
325 19644
            'default'       => $this->_default,
326 19644
            'notnull'       => $this->_notnull,
327 19644
            'length'        => $this->_length,
328 19644
            'precision'     => $this->_precision,
329 19644
            'scale'         => $this->_scale,
330 19644
            'fixed'         => $this->_fixed,
331 19644
            'unsigned'      => $this->_unsigned,
332 19644
            'autoincrement' => $this->_autoincrement,
333 19644
            'columnDefinition' => $this->_columnDefinition,
334 19644
            'comment' => $this->_comment,
335 19644
        ], $this->_platformOptions, $this->_customSchemaOptions);
336
    }
337
}
338