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

IntegerColumnSchema::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
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\Schema\SchemaInterface;
9
10
use function is_int;
11
12
class IntegerColumnSchema extends AbstractColumnSchema
13
{
14
    public function __construct(string $name)
15
    {
16
        parent::__construct($name);
17
18
        $this->type(SchemaInterface::TYPE_INTEGER);
19
        $this->phpType(SchemaInterface::PHP_TYPE_INTEGER);
20
    }
21
22
    public function dbTypecast(mixed $value): int|ExpressionInterface|null
23
    {
24
        return match (true) {
25
            is_int($value), $value === null, $value instanceof ExpressionInterface => $value,
26
            $value === '' => null,
27
            default => (int) $value,
28
        };
29
    }
30
31
    public function phpTypecast(mixed $value): int|null
32
    {
33
        if ($value === null) {
34
            return null;
35
        }
36
37
        return (int) $value;
38
    }
39
}
40