Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Doctrine\DBAL\Schema;
4
 
5
use Doctrine\DBAL\Schema\Exception\UnknownColumnOption;
6
use Doctrine\DBAL\Types\Type;
7
use Doctrine\Deprecations\Deprecation;
8
 
9
use function array_merge;
10
use function is_numeric;
11
use function method_exists;
12
 
13
/**
14
 * Object representation of a database column.
15
 */
16
class Column extends AbstractAsset
17
{
18
    /** @var Type */
19
    protected $_type;
20
 
21
    /** @var int|null */
22
    protected $_length;
23
 
24
    /** @var int */
25
    protected $_precision = 10;
26
 
27
    /** @var int */
28
    protected $_scale = 0;
29
 
30
    /** @var bool */
31
    protected $_unsigned = false;
32
 
33
    /** @var bool */
34
    protected $_fixed = false;
35
 
36
    /** @var bool */
37
    protected $_notnull = true;
38
 
39
    /** @var string|null */
40
    protected $_default;
41
 
42
    /** @var bool */
43
    protected $_autoincrement = false;
44
 
45
    /** @var mixed[] */
46
    protected $_platformOptions = [];
47
 
48
    /** @var string|null */
49
    protected $_columnDefinition;
50
 
51
    /** @var string|null */
52
    protected $_comment;
53
 
54
    /**
55
     * @deprecated Use {@link $_platformOptions instead}
56
     *
57
     * @var mixed[]
58
     */
59
    protected $_customSchemaOptions = [];
60
 
61
    /**
62
     * Creates a new Column.
63
     *
64
     * @param string  $name
65
     * @param mixed[] $options
66
     *
67
     * @throws SchemaException
68
     */
69
    public function __construct($name, Type $type, array $options = [])
70
    {
71
        $this->_setName($name);
72
        $this->setType($type);
73
        $this->setOptions($options);
74
    }
75
 
76
    /**
77
     * @param mixed[] $options
78
     *
79
     * @return Column
80
     *
81
     * @throws SchemaException
82
     */
83
    public function setOptions(array $options)
84
    {
85
        foreach ($options as $name => $value) {
86
            $method = 'set' . $name;
87
 
88
            if (! method_exists($this, $method)) {
89
                throw UnknownColumnOption::new($name);
90
            }
91
 
92
            $this->$method($value);
93
        }
94
 
95
        return $this;
96
    }
97
 
98
    /** @return Column */
99
    public function setType(Type $type)
100
    {
101
        $this->_type = $type;
102
 
103
        return $this;
104
    }
105
 
106
    /**
107
     * @param int|null $length
108
     *
109
     * @return Column
110
     */
111
    public function setLength($length)
112
    {
113
        if ($length !== null) {
114
            $this->_length = (int) $length;
115
        } else {
116
            $this->_length = null;
117
        }
118
 
119
        return $this;
120
    }
121
 
122
    /**
123
     * @param int $precision
124
     *
125
     * @return Column
126
     */
127
    public function setPrecision($precision)
128
    {
129
        if (! is_numeric($precision)) {
130
            $precision = 10; // defaults to 10 when no valid precision is given.
131
        }
132
 
133
        $this->_precision = (int) $precision;
134
 
135
        return $this;
136
    }
137
 
138
    /**
139
     * @param int $scale
140
     *
141
     * @return Column
142
     */
143
    public function setScale($scale)
144
    {
145
        if (! is_numeric($scale)) {
146
            $scale = 0;
147
        }
148
 
149
        $this->_scale = (int) $scale;
150
 
151
        return $this;
152
    }
153
 
154
    /**
155
     * @param bool $unsigned
156
     *
157
     * @return Column
158
     */
159
    public function setUnsigned($unsigned)
160
    {
161
        $this->_unsigned = (bool) $unsigned;
162
 
163
        return $this;
164
    }
165
 
166
    /**
167
     * @param bool $fixed
168
     *
169
     * @return Column
170
     */
171
    public function setFixed($fixed)
172
    {
173
        $this->_fixed = (bool) $fixed;
174
 
175
        return $this;
176
    }
177
 
178
    /**
179
     * @param bool $notnull
180
     *
181
     * @return Column
182
     */
183
    public function setNotnull($notnull)
184
    {
185
        $this->_notnull = (bool) $notnull;
186
 
187
        return $this;
188
    }
189
 
190
    /**
191
     * @param mixed $default
192
     *
193
     * @return Column
194
     */
195
    public function setDefault($default)
196
    {
197
        $this->_default = $default;
198
 
199
        return $this;
200
    }
201
 
202
    /**
203
     * @param mixed[] $platformOptions
204
     *
205
     * @return Column
206
     */
207
    public function setPlatformOptions(array $platformOptions)
208
    {
209
        $this->_platformOptions = $platformOptions;
210
 
211
        return $this;
212
    }
213
 
214
    /**
215
     * @param string $name
216
     * @param mixed  $value
217
     *
218
     * @return Column
219
     */
220
    public function setPlatformOption($name, $value)
221
    {
222
        $this->_platformOptions[$name] = $value;
223
 
224
        return $this;
225
    }
226
 
227
    /**
228
     * @param string|null $value
229
     *
230
     * @return Column
231
     */
232
    public function setColumnDefinition($value)
233
    {
234
        $this->_columnDefinition = $value;
235
 
236
        return $this;
237
    }
238
 
239
    /** @return Type */
240
    public function getType()
241
    {
242
        return $this->_type;
243
    }
244
 
245
    /** @return int|null */
246
    public function getLength()
247
    {
248
        return $this->_length;
249
    }
250
 
251
    /** @return int */
252
    public function getPrecision()
253
    {
254
        return $this->_precision;
255
    }
256
 
257
    /** @return int */
258
    public function getScale()
259
    {
260
        return $this->_scale;
261
    }
262
 
263
    /** @return bool */
264
    public function getUnsigned()
265
    {
266
        return $this->_unsigned;
267
    }
268
 
269
    /** @return bool */
270
    public function getFixed()
271
    {
272
        return $this->_fixed;
273
    }
274
 
275
    /** @return bool */
276
    public function getNotnull()
277
    {
278
        return $this->_notnull;
279
    }
280
 
281
    /** @return string|null */
282
    public function getDefault()
283
    {
284
        return $this->_default;
285
    }
286
 
287
    /** @return mixed[] */
288
    public function getPlatformOptions()
289
    {
290
        return $this->_platformOptions;
291
    }
292
 
293
    /**
294
     * @param string $name
295
     *
296
     * @return bool
297
     */
298
    public function hasPlatformOption($name)
299
    {
300
        return isset($this->_platformOptions[$name]);
301
    }
302
 
303
    /**
304
     * @param string $name
305
     *
306
     * @return mixed
307
     */
308
    public function getPlatformOption($name)
309
    {
310
        return $this->_platformOptions[$name];
311
    }
312
 
313
    /** @return string|null */
314
    public function getColumnDefinition()
315
    {
316
        return $this->_columnDefinition;
317
    }
318
 
319
    /** @return bool */
320
    public function getAutoincrement()
321
    {
322
        return $this->_autoincrement;
323
    }
324
 
325
    /**
326
     * @param bool $flag
327
     *
328
     * @return Column
329
     */
330
    public function setAutoincrement($flag)
331
    {
332
        $this->_autoincrement = $flag;
333
 
334
        return $this;
335
    }
336
 
337
    /**
338
     * @param string|null $comment
339
     *
340
     * @return Column
341
     */
342
    public function setComment($comment)
343
    {
344
        $this->_comment = $comment;
345
 
346
        return $this;
347
    }
348
 
349
    /** @return string|null */
350
    public function getComment()
351
    {
352
        return $this->_comment;
353
    }
354
 
355
    /**
356
     * @deprecated Use {@link setPlatformOption() instead}
357
     *
358
     * @param string $name
359
     * @param mixed  $value
360
     *
361
     * @return Column
362
     */
363
    public function setCustomSchemaOption($name, $value)
364
    {
365
        Deprecation::trigger(
366
            'doctrine/dbal',
367
            'https://github.com/doctrine/dbal/pull/5476',
368
            'Column::setCustomSchemaOption() is deprecated. Use setPlatformOption() instead.',
369
        );
370
 
371
        $this->_customSchemaOptions[$name] = $value;
372
 
373
        return $this;
374
    }
375
 
376
    /**
377
     * @deprecated Use {@link hasPlatformOption() instead}
378
     *
379
     * @param string $name
380
     *
381
     * @return bool
382
     */
383
    public function hasCustomSchemaOption($name)
384
    {
385
        Deprecation::trigger(
386
            'doctrine/dbal',
387
            'https://github.com/doctrine/dbal/pull/5476',
388
            'Column::hasCustomSchemaOption() is deprecated. Use hasPlatformOption() instead.',
389
        );
390
 
391
        return isset($this->_customSchemaOptions[$name]);
392
    }
393
 
394
    /**
395
     * @deprecated Use {@link getPlatformOption() instead}
396
     *
397
     * @param string $name
398
     *
399
     * @return mixed
400
     */
401
    public function getCustomSchemaOption($name)
402
    {
403
        Deprecation::trigger(
404
            'doctrine/dbal',
405
            'https://github.com/doctrine/dbal/pull/5476',
406
            'Column::getCustomSchemaOption() is deprecated. Use getPlatformOption() instead.',
407
        );
408
 
409
        return $this->_customSchemaOptions[$name];
410
    }
411
 
412
    /**
413
     * @deprecated Use {@link setPlatformOptions() instead}
414
     *
415
     * @param mixed[] $customSchemaOptions
416
     *
417
     * @return Column
418
     */
419
    public function setCustomSchemaOptions(array $customSchemaOptions)
420
    {
421
        Deprecation::trigger(
422
            'doctrine/dbal',
423
            'https://github.com/doctrine/dbal/pull/5476',
424
            'Column::setCustomSchemaOptions() is deprecated. Use setPlatformOptions() instead.',
425
        );
426
 
427
        $this->_customSchemaOptions = $customSchemaOptions;
428
 
429
        return $this;
430
    }
431
 
432
    /**
433
     * @deprecated Use {@link getPlatformOptions() instead}
434
     *
435
     * @return mixed[]
436
     */
437
    public function getCustomSchemaOptions()
438
    {
439
        Deprecation::trigger(
440
            'doctrine/dbal',
441
            'https://github.com/doctrine/dbal/pull/5476',
442
            'Column::getCustomSchemaOptions() is deprecated. Use getPlatformOptions() instead.',
443
        );
444
 
445
        return $this->_customSchemaOptions;
446
    }
447
 
448
    /** @return mixed[] */
449
    public function toArray()
450
    {
451
        return array_merge([
452
            'name'          => $this->_name,
453
            'type'          => $this->_type,
454
            'default'       => $this->_default,
455
            'notnull'       => $this->_notnull,
456
            'length'        => $this->_length,
457
            'precision'     => $this->_precision,
458
            'scale'         => $this->_scale,
459
            'fixed'         => $this->_fixed,
460
            'unsigned'      => $this->_unsigned,
461
            'autoincrement' => $this->_autoincrement,
462
            'columnDefinition' => $this->_columnDefinition,
463
            'comment' => $this->_comment,
464
        ], $this->_platformOptions, $this->_customSchemaOptions);
465
    }
466
}