Passed
Pull Request — 2.2 (#20357)
by Wilmer
13:33 queued 05:55
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
/**
4
 * @link https://www.yiiframework.com/
5
 * @copyright Copyright (c) 2008 Yii Software LLC
6
 * @license https://www.yiiframework.com/license/
7
 */
8
9
namespace yii\db\mysql;
10
11
use yii\db\ExpressionInterface;
12
use yii\db\JsonExpression;
13
14
/**
15
 * Class ColumnSchema for MySQL database
16
 *
17
 * @author Dmytro Naumenko <[email protected]>
18
 * @since 2.0.14.1
19
 */
20
class ColumnSchema extends \yii\db\ColumnSchema
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function dbTypecast($value)
26
    {
27
        if ($value === null) {
28
            return $value;
29
        }
30
31
        if ($value instanceof ExpressionInterface) {
32
            return $value;
33
        }
34
35
        if ($this->dbType === Schema::TYPE_JSON) {
36
            return new JsonExpression($value, $this->type);
37
        }
38
39
        return $this->typecast($value);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function phpTypecast($value)
46
    {
47
        if ($value === null) {
48
            return null;
49
        }
50
51
        if ($this->type === Schema::TYPE_JSON) {
52
            return json_decode($value, true);
53
        }
54
55
        return parent::phpTypecast($value);
56
    }
57
}
58