Passed
Pull Request — master (#20362)
by Wilmer
05:37
created

ColumnSchema::dbTypecast()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 7
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 15
ccs 0
cts 8
cp 0
crap 20
rs 10
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\db\mysql;
9
10
use yii\db\ExpressionInterface;
11
use yii\db\JsonExpression;
12
13
/**
14
 * Class ColumnSchema for MySQL database
15
 *
16
 * @author Dmytro Naumenko <[email protected]>
17
 * @since 2.0.14.1
18
 */
19
class ColumnSchema extends \yii\db\ColumnSchema
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function dbTypecast($value)
25
    {
26
        if ($value === null) {
27
            return $value;
28
        }
29
30
        if ($value instanceof ExpressionInterface) {
31
            return $value;
32
        }
33
34
        if ($this->dbType === Schema::TYPE_JSON) {
35
            return new JsonExpression($value, $this->type);
36
        }
37
38
        return $this->typecast($value);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function phpTypecast($value)
45
    {
46
        if ($value === null) {
47
            return null;
48
        }
49
50
        if ($this->type === Schema::TYPE_JSON) {
51
            return json_decode($value, true);
52
        }
53
54
        return parent::phpTypecast($value);
55
    }
56
}
57