Failed Conditions
Push — develop ( 55bd22...e46c09 )
by Sergei
33s queued 23s
created

Column::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 16
ccs 15
cts 15
cp 1
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 18853
    public function __construct(string $name, Type $type, array $options = [])
64
    {
65 18853
        $this->_setName($name);
66 18853
        $this->setType($type);
67 18853
        $this->setOptions($options);
68 18853
    }
69
70
    /**
71
     * @param mixed[] $options
72
     */
73 18853
    public function setOptions(array $options) : self
74
    {
75 18853
        foreach ($options as $name => $value) {
76 18065
            $method = 'set' . $name;
77 18065
            if (! method_exists($this, $method)) {
78
                // next major: throw an exception
79 3079
                @trigger_error(sprintf(
80
                    'The "%s" column option is not supported,' .
81 4
                    ' setting it is deprecated and will cause an error in Doctrine 3.0',
82 3079
                    $name
83 3079
                ), E_USER_DEPRECATED);
84
85 3079
                continue;
86
            }
87 18063
            $this->$method($value);
88
        }
89
90 18853
        return $this;
91
    }
92
93 18853
    public function setType(Type $type) : self
94
    {
95 18853
        $this->_type = $type;
96
97 18853
        return $this;
98
    }
99
100 16971
    public function setLength(?int $length) : self
101
    {
102 16971
        $this->_length = $length;
103
104 16971
        return $this;
105
    }
106
107 16772
    public function setPrecision(?int $precision) : self
108
    {
109 16772
        if ($precision === null) {
110 16698
            $precision = 10; // defaults to 10 when no precision is given.
111
        }
112
113 16772
        $this->_precision = $precision;
114
115 16772
        return $this;
116
    }
117
118 16770
    public function setScale(int $scale) : self
119
    {
120 16770
        $this->_scale = $scale;
121
122 16770
        return $this;
123
    }
124
125 16662
    public function setUnsigned(bool $unsigned) : self
126
    {
127 16662
        $this->_unsigned = $unsigned;
128
129 16662
        return $this;
130
    }
131
132 16865
    public function setFixed(bool $fixed) : self
133
    {
134 16865
        $this->_fixed = $fixed;
135
136 16865
        return $this;
137
    }
138
139 18319
    public function setNotnull(bool $notnull) : self
140
    {
141 18319
        $this->_notnull = $notnull;
142
143 18319
        return $this;
144
    }
145
146
    /**
147
     * @param mixed $default
148
     */
149 16804
    public function setDefault($default) : self
150
    {
151 16804
        $this->_default = $default;
152
153 16804
        return $this;
154
    }
155
156
    /**
157
     * @param mixed[] $platformOptions
158
     */
159 13287
    public function setPlatformOptions(array $platformOptions) : self
160
    {
161 13287
        $this->_platformOptions = $platformOptions;
162
163 13287
        return $this;
164
    }
165
166
    /**
167
     * @param mixed $value
168
     */
169 16561
    public function setPlatformOption(string $name, $value) : self
170
    {
171 16561
        $this->_platformOptions[$name] = $value;
172
173 16561
        return $this;
174
    }
175
176 12670
    public function setColumnDefinition(string $value) : self
177
    {
178 12670
        $this->_columnDefinition = $value;
179
180 12670
        return $this;
181
    }
182
183 18435
    public function getType() : Type
184
    {
185 18435
        return $this->_type;
186
    }
187
188 15723
    public function getLength() : ?int
189
    {
190 15723
        return $this->_length;
191
    }
192
193 15712
    public function getPrecision() : ?int
194
    {
195 15712
        return $this->_precision;
196
    }
197
198 15712
    public function getScale() : ?int
199
    {
200 15712
        return $this->_scale;
201
    }
202
203 15896
    public function getUnsigned() : bool
204
    {
205 15896
        return $this->_unsigned;
206
    }
207
208 15780
    public function getFixed() : bool
209
    {
210 15780
        return $this->_fixed;
211
    }
212
213 15986
    public function getNotnull() : bool
214
    {
215 15986
        return $this->_notnull;
216
    }
217
218
    /**
219
     * @return mixed
220
     */
221 16122
    public function getDefault()
222
    {
223 16122
        return $this->_default;
224
    }
225
226
    /**
227
     * @return mixed[]
228
     */
229 16914
    public function getPlatformOptions() : array
230
    {
231 16914
        return $this->_platformOptions;
232
    }
233
234 18201
    public function hasPlatformOption(string $name) : bool
235
    {
236 18201
        return isset($this->_platformOptions[$name]);
237
    }
238
239
    /**
240
     * @return mixed
241
     */
242 15550
    public function getPlatformOption(string $name)
243
    {
244 15550
        return $this->_platformOptions[$name];
245
    }
246
247
    public function getColumnDefinition() : ?string
248
    {
249
        return $this->_columnDefinition;
250
    }
251
252 16680
    public function getAutoincrement() : bool
253
    {
254 16680
        return $this->_autoincrement;
255
    }
256
257 17034
    public function setAutoincrement(bool $flag) : self
258
    {
259 17034
        $this->_autoincrement = $flag;
260
261 17034
        return $this;
262
    }
263
264 16906
    public function setComment(?string $comment) : self
265
    {
266 16906
        $this->_comment = $comment;
267
268 16906
        return $this;
269
    }
270
271 18417
    public function getComment() : ?string
272
    {
273 18417
        return $this->_comment;
274
    }
275
276
    /**
277
     * @param mixed $value
278
     */
279 2677
    public function setCustomSchemaOption(string $name, $value) : self
280
    {
281 2677
        $this->_customSchemaOptions[$name] = $value;
282
283 2677
        return $this;
284
    }
285
286 3127
    public function hasCustomSchemaOption(string $name) : bool
287
    {
288 3127
        return isset($this->_customSchemaOptions[$name]);
289
    }
290
291
    /**
292
     * @return mixed
293
     */
294 3127
    public function getCustomSchemaOption(string $name)
295
    {
296 3127
        return $this->_customSchemaOptions[$name];
297
    }
298
299
    /**
300
     * @param mixed[] $customSchemaOptions
301
     */
302 3131
    public function setCustomSchemaOptions(array $customSchemaOptions) : self
303
    {
304 3131
        $this->_customSchemaOptions = $customSchemaOptions;
305
306 3131
        return $this;
307
    }
308
309
    /**
310
     * @return mixed[]
311
     */
312 16912
    public function getCustomSchemaOptions() : array
313
    {
314 16912
        return $this->_customSchemaOptions;
315
    }
316
317
    /**
318
     * @return mixed[]
319
     */
320 18623
    public function toArray() : array
321
    {
322 18623
        return array_merge([
323 18623
            'name'          => $this->_name,
324 18623
            'type'          => $this->_type,
325 18623
            'default'       => $this->_default,
326 18623
            'notnull'       => $this->_notnull,
327 18623
            'length'        => $this->_length,
328 18623
            'precision'     => $this->_precision,
329 18623
            'scale'         => $this->_scale,
330 18623
            'fixed'         => $this->_fixed,
331 18623
            'unsigned'      => $this->_unsigned,
332 18623
            'autoincrement' => $this->_autoincrement,
333 18623
            'columnDefinition' => $this->_columnDefinition,
334 18623
            'comment' => $this->_comment,
335 18623
        ], $this->_platformOptions, $this->_customSchemaOptions);
336
    }
337
}
338