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

StringColumn   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 17
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A dbTypecast() 0 8 1
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