Passed
Push — master ( 9c3a01...775781 )
by Wilmer
20:48 queued 18:28
created

AbstractColumnSchema::enumValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Schema;
6
7
use PDO;
8
use Yiisoft\Db\Command\Param;
9
use Yiisoft\Db\Expression\ExpressionInterface;
10
use Yiisoft\Strings\NumericHelper;
11
12
/**
13
 * The ColumnSchema class represents the metadata of a column in a database table. It provides information about the
14
 * column's name, type, size, precision, and other details.
15
 *
16
 * The ColumnSchema class is used to store and retrieve metadata about a column in a database table. It is typically
17
 * used in conjunction with the TableSchema class, which represents the metadata of a database table as a whole.
18
 *
19
 * Here is an example of how the ColumnSchema class might be used:
20
 *
21
 * ```php
22
 * use Yiisoft\Db\Schema\ColumnSchema;
23
 *
24
 * $column = new ColumnSchema();
25
 * $column->name('id');
26
 * $column->allowNull(false);
27
 * $column->dbType('int(11)');
28
 * $column->phpType('integer');
29
 * $column->type('integer');
30
 * $column->defaultValue(0);
31
 * $column->autoIncrement(true);
32
 * $column->primaryKey(true);
33
 * ``
34
 */
35
abstract class AbstractColumnSchema implements ColumnSchemaInterface
36
{
37
    private bool $allowNull = false;
38
    private bool $autoIncrement = false;
39
    private string|null $comment = null;
40
    private bool $computed = false;
41
    private string $dbType = '';
42
    private mixed $defaultValue = null;
43
    private array|null $enumValues = null;
44
    private string|null $extra = null;
45
    private bool $isPrimaryKey = false;
46
    private string $name = '';
47
    private string|null $phpType = null;
48
    private int|null $precision = null;
49
    private int|null $scale = null;
50
    private int|null $size = null;
51
    private string $type = '';
52
    private bool $unsigned = false;
53
54
    public function allowNull(bool $value): void
55
    {
56
        $this->allowNull = $value;
57
    }
58
59
    public function autoIncrement(bool $value): void
60
    {
61
        $this->autoIncrement = $value;
62
    }
63
64
    public function comment(string|null $value): void
65
    {
66
        $this->comment = $value;
67
    }
68
69
    public function computed(bool $value): void
70
    {
71
        $this->computed = $value;
72
    }
73
74
    public function dbType(string $value): void
75
    {
76
        $this->dbType = $value;
77
    }
78
79
    public function dbTypecast(mixed $value): mixed
80
    {
81
        /**
82
         * the default implementation does the same as casting for PHP, but it should be possible to override this with
83
         * annotation of explicit PDO type.
84
         */
85
        return $this->typecast($value);
86
    }
87
88
    public function defaultValue(mixed $value): void
89
    {
90
        $this->defaultValue = $value;
91
    }
92
93
    public function enumValues(array|null $value): void
94
    {
95
        $this->enumValues = $value;
96
    }
97
98
    public function extra(string|null $value): void
99
    {
100
        $this->extra = $value;
101
    }
102
103
    public function getComment(): string|null
104
    {
105
        return $this->comment;
106
    }
107
108
    public function getDbType(): string
109
    {
110
        return $this->dbType;
111
    }
112
113
    public function getDefaultValue(): mixed
114
    {
115
        return $this->defaultValue;
116
    }
117
118
    public function getEnumValues(): array|null
119
    {
120
        return $this->enumValues;
121
    }
122
123
    public function getExtra(): string|null
124
    {
125
        return $this->extra;
126
    }
127
128
    public function getName(): string
129
    {
130
        return $this->name;
131
    }
132
133
    public function getPrecision(): int|null
134
    {
135
        return $this->precision;
136
    }
137
138
    public function getPhpType(): string|null
139
    {
140
        return $this->phpType;
141
    }
142
143
    public function getScale(): int|null
144
    {
145
        return $this->scale;
146
    }
147
148
    public function getSize(): int|null
149
    {
150
        return $this->size;
151
    }
152
153
    public function getType(): string
154
    {
155
        return $this->type;
156
    }
157
158
    public function isAllowNull(): bool
159
    {
160
        return $this->allowNull;
161
    }
162
163
    public function isAutoIncrement(): bool
164
    {
165
        return $this->autoIncrement;
166
    }
167
168
    public function isComputed(): bool
169
    {
170
        return $this->computed;
171
    }
172
173
    public function isPrimaryKey(): bool
174
    {
175
        return $this->isPrimaryKey;
176
    }
177
178
    public function isUnsigned(): bool
179
    {
180
        return $this->unsigned;
181
    }
182
183
    public function name(string $value): void
184
    {
185
        $this->name = $value;
186
    }
187
188
    public function phpType(string|null $value): void
189
    {
190
        $this->phpType = $value;
191
    }
192
193
    public function phpTypecast(mixed $value): mixed
194
    {
195
        return $this->typecast($value);
196
    }
197
198
    public function precision(int|null $value): void
199
    {
200
        $this->precision = $value;
201
    }
202
203
    public function primaryKey(bool $value): void
204
    {
205
        $this->isPrimaryKey = $value;
206
    }
207
208
    public function scale(int|null $value): void
209
    {
210
        $this->scale = $value;
211
    }
212
213
    public function size(int|null $value): void
214
    {
215
        $this->size = $value;
216
    }
217
218
    public function type(string $value): void
219
    {
220
        $this->type = $value;
221
    }
222
223
    public function unsigned(bool $value): void
224
    {
225
        $this->unsigned = $value;
226
    }
227
228
    /**
229
     * Converts the input value according to {@see phpType} after retrieval from the database.
230
     *
231
     * If the value is null or an {@see Expression}, it will not be converted.
232
     *
233
     * @param mixed $value input value
234
     *
235
     * @return mixed converted value
236
     */
237
    protected function typecast(mixed $value): mixed
238
    {
239
        if (
240
            $value === ''
241
            && !in_array(
242
                $this->type,
243
                [
244
                    Schema::TYPE_TEXT,
245
                    Schema::TYPE_STRING,
246
                    Schema::TYPE_BINARY,
247
                    Schema::TYPE_CHAR,
248
                ],
249
                true
250
            )
251
        ) {
252
            return null;
253
        }
254
255
        if (
256
            $value === null
257
            || gettype($value) === $this->phpType
258
            || $value instanceof ExpressionInterface
259
        ) {
260
            return $value;
261
        }
262
263
        if (
264
            is_array($value)
265
            && count($value) === 2
266
            && isset($value[1])
267
            && in_array($value[1], $this->getPdoParamTypes(), true)
268
        ) {
269
            return new Param((string) $value[0], $value[1]);
270
        }
271
272
        switch ($this->phpType) {
273
            case Schema::PHP_TYPE_RESOURCE:
274
            case Schema::PHP_TYPE_STRING:
275
                if (is_resource($value)) {
276
                    return $value;
277
                }
278
279
                if (is_float($value)) {
280
                    /* ensure type cast always has . as decimal separator in all locales */
281
                    return NumericHelper::normalize((string) $value);
282
                }
283
284
                if (is_bool($value)) {
285
                    return $value ? '1' : '0';
286
                }
287
288
                return (string) $value;
289
            case Schema::PHP_TYPE_INTEGER:
290
                return (int) $value;
291
            case Schema::PHP_TYPE_BOOLEAN:
292
                /**
293
                 * treating a 0 bit value as false too
294
                 *
295
                 * @link https://github.com/yiisoft/yii2/issues/9006
296
                 */
297
                return (bool) $value && $value !== "\0";
298
            case Schema::PHP_TYPE_DOUBLE:
299
                return (float) $value;
300
        }
301
302
        return $value;
303
    }
304
305
    /**
306
     * @return int[] array of numbers that represent possible PDO parameter types
307
     */
308
    private function getPdoParamTypes(): array
309
    {
310
        return [
311
            PDO::PARAM_BOOL,
312
            PDO::PARAM_INT,
313
            PDO::PARAM_STR,
314
            PDO::PARAM_LOB,
315
            PDO::PARAM_NULL,
316
            PDO::PARAM_STMT,
317
        ];
318
    }
319
}
320