Passed
Pull Request — master (#752)
by Sergei
02:29
created

StringColumnSchema::dbTypecast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
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 StringColumnSchema extends AbstractColumnSchema
16
{
17
    public function __construct(string $name)
18
    {
19
        parent::__construct($name);
20
21
        $this->type(SchemaInterface::TYPE_STRING);
22
        $this->phpType(SchemaInterface::PHP_TYPE_STRING);
23
    }
24
25
    public function dbTypecast(mixed $value): mixed
26
    {
27
        return match (true) {
28
            is_string($value), $value === null, is_resource($value), $value instanceof ExpressionInterface => $value,
29
            /** ensure type cast always has . as decimal separator in all locales */
30
            is_float($value) => DbStringHelper::normalizeFloat($value),
31
            $value === false => '0',
32
            default => (string) $value,
33
        };
34
    }
35
}
36