• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thecodingmachine / dbal-fluid-schema-builder / 7686160655

28 Jan 2024 01:42PM UTC coverage: 100.0% (+0.6%) from 99.415%
7686160655

push

github

web-flow
Merge pull request #13 from aszenz/master

ci: Improve tests + fix ci deps

171 of 171 relevant lines covered (100.0%)

14.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/FluidColumn.php
1
<?php
2

3

4
namespace TheCodingMachine\FluidSchema;
5

6
use Doctrine\DBAL\Schema\Column;
7
use Doctrine\DBAL\Schema\Table;
8
use Doctrine\DBAL\Types\Type;
9
use Doctrine\DBAL\Types\Types;
10

11
class FluidColumn
12
{
13
    /**
14
     * @var FluidSchema
15
     */
16
    private $fluidSchema;
17
    /**
18
     * @var FluidTable
19
     */
20
    private $fluidTable;
21
    /**
22
     * @var Column
23
     */
24
    private $column;
25
    /**
26
     * @var Table
27
     */
28
    private $table;
29
    /**
30
     * @var NamingStrategyInterface
31
     */
32
    private $namingStrategy;
33

34
    /**
35
     * FluidColumn constructor.
36
     * @param FluidSchema $fluidSchema
37
     * @param FluidTable $fluidTable
38
     * @param Table $table
39
     * @param Column $column
40
     */
41
    public function __construct(FluidSchema $fluidSchema, FluidTable $fluidTable, Table $table, Column $column, NamingStrategyInterface $namingStrategy)
42
    {
43
        $this->fluidSchema = $fluidSchema;
54✔
44
        $this->fluidTable = $fluidTable;
54✔
45
        $this->column = $column;
54✔
46
        $this->table = $table;
54✔
47
        $this->namingStrategy = $namingStrategy;
54✔
48
    }
49

50
    public function integer(): FluidColumnOptions
51
    {
52
        $this->column->setType(Type::getType(Types::INTEGER));
39✔
53
        return $this->getOptions();
39✔
54
    }
55

56
    public function smallInt(): FluidColumnOptions
57
    {
58
        $this->column->setType(Type::getType(Types::SMALLINT));
3✔
59
        return $this->getOptions();
3✔
60
    }
61

62
    public function bigInt(): FluidColumnOptions
63
    {
64
        $this->column->setType(Type::getType(Types::BIGINT));
3✔
65
        return $this->getOptions();
3✔
66
    }
67

68
    public function decimal(int $precision = 10, int $scale = 0): FluidColumnOptions
69
    {
70
        $this->column->setType(Type::getType(Types::DECIMAL));
3✔
71
        $this->column->setPrecision($precision);
3✔
72
        $this->column->setScale($scale);
3✔
73
        return $this->getOptions();
3✔
74
    }
75

76
    public function float(int $precision = 10, int $scale = 0): FluidColumnOptions
77
    {
78
        $this->column->setType(Type::getType(Types::FLOAT));
3✔
79
        $this->column->setPrecision($precision);
3✔
80
        $this->column->setScale($scale);
3✔
81
        return $this->getOptions();
3✔
82
    }
83

84
    public function string(?int $length = null, bool $fixed = false): FluidColumnOptions
85
    {
86
        $this->column->setType(Type::getType(Types::STRING));
6✔
87
        $this->column->setLength($length);
6✔
88
        $this->column->setFixed($fixed);
6✔
89
        return $this->getOptions();
6✔
90
    }
91

92
    public function text(?int $length = null): FluidColumnOptions
93
    {
94
        $this->column->setType(Type::getType(Types::TEXT));
3✔
95
        $this->column->setLength($length);
3✔
96
        return $this->getOptions();
3✔
97
    }
98

99
    public function guid(): FluidColumnOptions
100
    {
101
        $this->column->setType(Type::getType(Types::GUID));
6✔
102
        return $this->getOptions();
6✔
103
    }
104

105
    /**
106
     * From Doctrine DBAL 2.4+.
107
     */
108
    public function binary(?int $length = null, bool $fixed = false): FluidColumnOptions
109
    {
110
        $this->column->setType(Type::getType(Types::BINARY));
3✔
111
        $this->column->setLength($length);
3✔
112
        $this->column->setFixed($fixed);
3✔
113
        return $this->getOptions();
3✔
114
    }
115

116
    public function blob(): FluidColumnOptions
117
    {
118
        $this->column->setType(Type::getType(Types::BLOB));
3✔
119
        return $this->getOptions();
3✔
120
    }
121

122
    public function boolean(): FluidColumnOptions
123
    {
124
        $this->column->setType(Type::getType(Types::BOOLEAN));
3✔
125
        return $this->getOptions();
3✔
126
    }
127

128
    public function date(): FluidColumnOptions
129
    {
130
        $this->column->setType(Type::getType(Types::DATE_MUTABLE));
3✔
131
        return $this->getOptions();
3✔
132
    }
133

134
    public function dateImmutable(): FluidColumnOptions
135
    {
136
        $this->column->setType(Type::getType(Types::DATE_IMMUTABLE));
3✔
137
        return $this->getOptions();
3✔
138
    }
139

140
    public function datetime(): FluidColumnOptions
141
    {
142
        $this->column->setType(Type::getType(Types::DATETIME_MUTABLE));
3✔
143
        return $this->getOptions();
3✔
144
    }
145

146
    public function datetimeImmutable(): FluidColumnOptions
147
    {
148
        $this->column->setType(Type::getType(Types::DATETIME_IMMUTABLE));
6✔
149
        return $this->getOptions();
6✔
150
    }
151

152
    public function datetimeTz(): FluidColumnOptions
153
    {
154
        $this->column->setType(Type::getType(Types::DATETIMETZ_MUTABLE));
3✔
155
        return $this->getOptions();
3✔
156
    }
157

158
    public function datetimeTzImmutable(): FluidColumnOptions
159
    {
160
        $this->column->setType(Type::getType(Types::DATETIMETZ_IMMUTABLE));
3✔
161
        return $this->getOptions();
3✔
162
    }
163

164
    public function time(): FluidColumnOptions
165
    {
166
        $this->column->setType(Type::getType(Types::TIME_MUTABLE));
3✔
167
        return $this->getOptions();
3✔
168
    }
169

170
    public function timeImmutable(): FluidColumnOptions
171
    {
172
        $this->column->setType(Type::getType(Types::TIME_IMMUTABLE));
3✔
173
        return $this->getOptions();
3✔
174
    }
175

176
    public function dateInterval(): FluidColumnOptions
177
    {
178
        $this->column->setType(Type::getType(Types::DATEINTERVAL));
3✔
179
        return $this->getOptions();
3✔
180
    }
181

182
    /**
183
     * @deprecated Use json() instead
184
     */
185
    public function array(): FluidColumnOptions
186
    {
187
        $this->column->setType(Type::getType(Types::ARRAY));
3✔
188
        return $this->getOptions();
3✔
189
    }
190

191
    public function simpleArray(): FluidColumnOptions
192
    {
193
        $this->column->setType(Type::getType(Types::SIMPLE_ARRAY));
3✔
194
        return $this->getOptions();
3✔
195
    }
196

197
    public function json(): FluidColumnOptions
198
    {
199
        $this->column->setType(Type::getType(Types::JSON));
3✔
200
        return $this->getOptions();
3✔
201
    }
202

203
    /**
204
     * @deprecated From DBAL 2.6, use json() instead.
205
     * @return FluidColumnOptions
206
     */
207
    public function jsonArray(): FluidColumnOptions
208
    {
209
        $this->column->setType(Type::getType(Types::JSON));
3✔
210
        return $this->getOptions();
3✔
211
    }
212

213
    /**
214
     * @deprecated Use json() instead
215
     */
216
    public function object(): FluidColumnOptions
217
    {
218
        $this->column->setType(Type::getType(Types::OBJECT));
3✔
219
        return $this->getOptions();
3✔
220
    }
221

222
    public function references(string $tableName, ?string $constraintName = null, string $onUpdate = 'RESTRICT', string $onDelete = 'RESTRICT'): FluidColumnOptions
223
    {
224
        $tableName = $this->namingStrategy->quoteIdentifier($tableName);
12✔
225

226
        $table = $this->fluidSchema->getDbalSchema()->getTable($tableName);
12✔
227

228
        $referencedColumns = $table->getPrimaryKey()->getColumns();
12✔
229

230
        if (count($referencedColumns) > 1) {
12✔
231
            throw new FluidSchemaException('You cannot reference a table with a primary key on several columns using FluidSchema. Use DBAL Schema methods instead.');
3✔
232
        }
233

234
        $referencedColumnName = $this->namingStrategy->quoteIdentifier($referencedColumns[0]);
9✔
235
        $referencedColumn = $table->getColumn($referencedColumnName);
9✔
236

237
        $this->column->setType($referencedColumn->getType());
9✔
238
        $this->column->setLength($referencedColumn->getLength());
9✔
239
        $this->column->setFixed($referencedColumn->getFixed());
9✔
240
        $this->column->setScale($referencedColumn->getScale());
9✔
241
        $this->column->setPrecision($referencedColumn->getPrecision());
9✔
242
        $this->column->setUnsigned($referencedColumn->getUnsigned());
9✔
243

244
        $this->table->addForeignKeyConstraint($table, [$this->namingStrategy->quoteIdentifier($this->column->getName())], $referencedColumns, [
9✔
245
            'onUpdate' => $onUpdate,
9✔
246
            'onDelete' => $onDelete
9✔
247
        ], $constraintName);
9✔
248
        return $this->getOptions();
9✔
249
    }
250

251
    private function getOptions(): FluidColumnOptions
252
    {
253
        return new FluidColumnOptions($this->fluidTable, $this->column, $this->namingStrategy);
48✔
254
    }
255

256
    /**
257
     * Returns the underlying DBAL column.
258
     * @return Column
259
     */
260
    public function getDbalColumn(): Column
261
    {
262
        return $this->column;
3✔
263
    }
264
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc