Passed
Push — dbal ( be5115...7a7bb0 )
by Greg
10:42
created

Column::fixed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.8666
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2023 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\DB;
21
22
use Doctrine\DBAL\Schema\Column as DBALColumn;
23
24
/**
25
 * Simplify constructor arguments for doctrine/dbal.
26
 *
27
 * @internal - use DB::column()
28
 */
29
final readonly class Column implements ComponentInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 29 at column 6
Loading history...
30
{
31
    public function __construct(
32
        private readonly string $name,
33
        private readonly ColumnType $type,
34
        private readonly int $length = 0,
35
        private readonly int $precision = 0,
36
        private readonly int $scale = 0,
37
        private readonly bool $unsigned = false,
38
        private readonly bool $fixed = false,
39
        private readonly bool $nullable = false,
40
        private readonly float|int|string|null $default = null,
41
        private readonly bool $autoincrement = false,
42
        private readonly string|null $collation = null,
43
    ) {
44
    }
45
46
    public function autoincrement(): self
47
    {
48
        return new self(
49
            name: $this->name,
50
            type: $this->type,
51
            length: $this->length,
52
            precision: $this->precision,
53
            scale: $this->scale,
54
            unsigned: $this->unsigned,
55
            fixed: $this->fixed,
56
            nullable: $this->nullable,
57
            default: $this->default,
58
            autoincrement: true,
59
            collation: $this->collation,
60
        );
61
    }
62
63
    public function default(float|int|string $default): self
64
    {
65
        return new self(
66
            name: $this->name,
67
            type: $this->type,
68
            length: $this->length,
69
            precision: $this->precision,
70
            scale: $this->scale,
71
            unsigned: $this->unsigned,
72
            fixed: $this->fixed,
73
            nullable: $this->nullable,
74
            default: $default,
75
            autoincrement: $this->autoincrement,
76
            collation: $this->collation,
77
        );
78
    }
79
80
    public function fixed(): self
81
    {
82
        return new self(
83
            name: $this->name,
84
            type: $this->type,
85
            length: $this->length,
86
            precision: $this->precision,
87
            scale: $this->scale,
88
            unsigned: $this->unsigned,
89
            fixed: true,
90
            nullable: $this->nullable,
91
            default: $this->default,
92
            autoincrement: $this->autoincrement,
93
            collation: $this->collation,
94
        );
95
    }
96
97
    public function nullable(): self
98
    {
99
        return new self(
100
            name: $this->name,
101
            type: $this->type,
102
            length: $this->length,
103
            precision: $this->precision,
104
            scale: $this->scale,
105
            unsigned: $this->unsigned,
106
            fixed: $this->fixed,
107
            nullable: true,
108
            default: $this->default,
109
            autoincrement: $this->autoincrement,
110
            collation: $this->collation,
111
        );
112
    }
113
114
    public function toDBAL(): DBALColumn
115
    {
116
        return new DBALColumn(
117
            name: $this->name,
118
            type: ColumnType::toDBALType($this->type),
119
            options: [
120
                'length'          => $this->length,
121
                'precision'       => $this->precision,
122
                'scale'           => $this->scale,
123
                'unsigned'        => $this->unsigned,
124
                'fixed'           => $this->fixed,
125
                'notnull'         => !$this->nullable,
126
                'default'         => $this->default,
127
                'autoincrement'   => $this->autoincrement,
128
                'platformOptions' => ['collation' => $this->collation],
129
            ],
130
        );
131
    }
132
}
133