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

BinaryColumnSchema   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A dbTypecast() 0 9 1
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\Helper\DbStringHelper;
11
use Yiisoft\Db\Schema\SchemaInterface;
12
13
use function is_float;
14
use function is_resource;
15
use function is_string;
16
17
class BinaryColumnSchema extends AbstractColumnSchema
18
{
19
    public function __construct(string $name)
20
    {
21
        parent::__construct($name);
22
23
        $this->type(SchemaInterface::TYPE_BINARY);
24
        $this->phpType(SchemaInterface::PHP_TYPE_RESOURCE);
25
    }
26
27
    public function dbTypecast(mixed $value): mixed
28
    {
29
        return match (true) {
30
            is_string($value) => new Param($value, PDO::PARAM_LOB),
31
            $value === null, is_resource($value), $value instanceof ExpressionInterface => $value,
32
            /** ensure type cast always has . as decimal separator in all locales */
33
            is_float($value) => DbStringHelper::normalizeFloat($value),
34
            $value === false => '0',
35
            default => (string) $value,
36
        };
37
    }
38
}
39