Passed
Pull Request — master (#808)
by Sergei
15:21 queued 13:01
created

StringColumn::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Schema\Column;
6
7
use Yiisoft\Db\Expression\ExpressionInterface;
8
use Yiisoft\Db\Helper\DbStringHelper;
9
use Yiisoft\Db\Schema\SchemaInterface;
10
11
use function is_float;
12
use function is_resource;
13
use function is_string;
14
15
class StringColumn extends Column
16
{
17
    public function __construct(
18
        string|null $type = SchemaInterface::TYPE_STRING,
19
        string|null $phpType = SchemaInterface::PHP_TYPE_STRING,
20
    ) {
21
        parent::__construct($type, $phpType);
22
    }
23
24
    public function dbTypecast(mixed $value): string|ExpressionInterface|null
25
    {
26
        return match (true) {
27
            is_string($value), $value === null, is_resource($value), $value instanceof ExpressionInterface => $value,
28
            /** ensure type cast always has . as decimal separator in all locales */
29
            is_float($value) => DbStringHelper::normalizeFloat($value),
30
            $value === false => '0',
31
            default => (string) $value,
32
        };
33
    }
34
}
35