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

BinaryColumn   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 21
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A dbTypecast() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Schema\Column;
6
7
use PDO;
8
use Yiisoft\Db\Command\Param;
9
use Yiisoft\Db\Expression\ExpressionInterface;
10
use Yiisoft\Db\Schema\SchemaInterface;
11
12
use function gettype;
13
14
class BinaryColumn extends Column
15
{
16
    public function __construct(
17
        string|null $type = SchemaInterface::TYPE_BINARY,
18
        string|null $phpType = SchemaInterface::PHP_TYPE_RESOURCE,
19
    ) {
20
        parent::__construct($type, $phpType);
21
    }
22
23
    public function dbTypecast(mixed $value): mixed
24
    {
25
        if ($value instanceof ExpressionInterface) {
26
            return $value;
27
        }
28
29
        return match (gettype($value)) {
30
            'string' => new Param($value, PDO::PARAM_LOB),
31
            'resource' => $value,
32
            'NULL' => null,
33
            'boolean' => $value ? '1' : '0',
34
            default => (string) $value,
35
        };
36
    }
37
}
38