Passed
Push — master ( 93aa2d...65319e )
by Def
20:48 queued 14:28
created

ColumnSchema::autoIncrement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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