Passed
Pull Request — master (#22)
by Wilmer
15:13
created

ColumnSchema::phpTypecast()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql\Schema;
6
7
use Yiisoft\Db\Expression\ExpressionInterface;
8
use Yiisoft\Db\Expression\JsonExpression;
9
use Yiisoft\Db\Schema\ColumnSchema as AbstractColumnSchema;
10
11
/**
12
 * Class ColumnSchema for MySQL database.
13
 */
14
class ColumnSchema extends AbstractColumnSchema
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function dbTypecast($value)
20
    {
21
        if ($value === null) {
22
            return $value;
23
        }
24
25
        if ($value instanceof ExpressionInterface) {
26
            return $value;
27
        }
28
29
        if ($this->getDbType() === Schema::TYPE_JSON) {
30
            return new JsonExpression($value, $this->getType());
31
        }
32
33
        return $this->typecast($value);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function phpTypecast($value)
40
    {
41
        if ($value === null) {
42
            return;
43
        }
44
45
        if ($this->getType() === Schema::TYPE_JSON) {
46
            return \json_decode($value, true);
47
        }
48
49
        return parent::phpTypecast($value);
50
    }
51
}
52