Passed
Pull Request — master (#56)
by Def
01:34
created

ColumnSchema   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 30
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dbTypecast() 0 11 4
A defaultPhpTypecast() 0 8 2
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