Passed
Pull Request — master (#808)
by Sergei
02:31
created

StringColumn   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
c 2
b 0
f 0
dl 0
loc 20
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A dbTypecast() 0 11 3
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\Schema\SchemaInterface;
9
10
use function gettype;
11
12
class StringColumn extends Column
13
{
14
    public function __construct(
15
        string|null $type = SchemaInterface::TYPE_STRING,
16
        string|null $phpType = SchemaInterface::PHP_TYPE_STRING,
17
    ) {
18
        parent::__construct($type, $phpType);
19
    }
20
21
    public function dbTypecast(mixed $value): string|ExpressionInterface|null
22
    {
23
        if ($value instanceof ExpressionInterface) {
24
            return $value;
25
        }
26
27
        return match (gettype($value)) {
28
            'string', 'resource' => $value,
29
            'NULL' => null,
30
            'boolean' => $value ? '1' : '0',
31
            default => (string) $value,
32
        };
33
    }
34
}
35