Passed
Push — master ( 6bbdcb...f9cfec )
by Def
05:50 queued 02:16
created

ColumnSchema::defaultPhpTypecast()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 13
ccs 6
cts 6
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use Yiisoft\Db\Command\ParamInterface;
8
use Yiisoft\Db\Expression\Expression;
9
use Yiisoft\Db\Schema\AbstractColumnSchema;
10
use Yiisoft\Db\Schema\SchemaInterface;
11
12
use function bin2hex;
13
use function is_string;
14
15
/**
16
 * Class ColumnSchema for MSSQL database
17
 */
18
final class ColumnSchema extends AbstractColumnSchema
19
{
20 77
    public function dbTypecast(mixed $value): mixed
21
    {
22 77
        if ($this->getType() === SchemaInterface::TYPE_BINARY && $this->getDbType() === 'varbinary') {
23 6
            if ($value instanceof ParamInterface && is_string($value->getValue())) {
24 1
                $value = (string) $value->getValue();
25
            }
26 6
            if (is_string($value)) {
27 6
                return new Expression('CONVERT(VARBINARY(MAX), ' . ('0x' . bin2hex($value)) . ')');
28
            }
29
        }
30
31 77
        return parent::dbTypecast($value);
32
    }
33
}
34