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

IntegerColumn::__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\Schema\SchemaInterface;
9
10
use function is_int;
11
12
class IntegerColumn extends Column
13
{
14
    public function __construct(
15
        string|null $type = SchemaInterface::TYPE_INTEGER,
16
        string|null $phpType = SchemaInterface::PHP_TYPE_INTEGER,
17
    ) {
18
        parent::__construct($type, $phpType);
19
    }
20
21
    public function dbTypecast(mixed $value): int|ExpressionInterface|null
22
    {
23
        return match (true) {
24
            is_int($value), $value === null, $value instanceof ExpressionInterface => $value,
25
            $value === '' => null,
26
            default => (int) $value,
27
        };
28
    }
29
30
    public function phpTypecast(mixed $value): int|null
31
    {
32
        if ($value === null) {
33
            return null;
34
        }
35
36
        return (int) $value;
37
    }
38
}
39