Passed
Pull Request — master (#56)
by Def
06:27
created

ColumnSchema::dbTypecast()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 0
cp 0
crap 20
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use Yiisoft\Db\Expression\Expression;
8
use Yiisoft\Db\Schema\ColumnSchema as AbstractColumnSchema;
9
10
use function substr;
11
12
/**
13
 * Class ColumnSchema for MSSQL database
14
 */
15
final class ColumnSchema extends AbstractColumnSchema
16
{
17
    /**
18
     * Prepares default value and converts it according to {@see phpType}.
19
     *
20
     * @param mixed $value default value
21
     *
22
     * @return mixed converted value
23 85
     */
24
    public function defaultPhpTypecast($value)
25 85
    {
26
        if ($value !== null) {
27 62
            /** convert from MSSQL column_default format, e.g. ('1') -> 1, ('string') -> string */
28
            $value = substr(substr($value, 2), 0, -2);
29
        }
30 85
31
        return $this->phpTypecast($value);
32
    }
33
34
    public function dbTypecast($value)
35
    {
36
        if (
37
            $this->getType() === Schema::TYPE_BINARY &&
38
            $this->getDbType() === 'varbinary' &&
39
            is_string($value)
40
        ) {
41
            return new Expression('CONVERT(VARBINARY(MAX), '. ('0x' . bin2hex($value)) .')');
42
        }
43
44
        return parent::dbTypecast($value);
45
    }
46
}
47