Passed
Pull Request — 2.2 (#20357)
by Wilmer
13:33 queued 05:55
created

ColumnSchema::getArrayParser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
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\pgsql;
10
11
use yii\db\ArrayExpression;
12
use yii\db\ExpressionInterface;
13
use yii\db\JsonExpression;
14
15
/**
16
 * Class ColumnSchema for PostgreSQL database.
17
 *
18
 * @author Dmytro Naumenko <[email protected]>
19
 */
20
class ColumnSchema extends \yii\db\ColumnSchema
21
{
22
    /**
23
     * @var int the dimension of array. Defaults to 0, means this column is not an array.
24
     */
25
    public $dimension = 0;
26
    /**
27
     * @var string name of associated sequence if column is auto-incremental
28
     * @since 2.0.29
29
     */
30
    public $sequenceName;
31
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function dbTypecast($value)
37
    {
38
        if ($value === null) {
39
            return $value;
40
        }
41
42
        if ($value instanceof ExpressionInterface) {
43
            return $value;
44
        }
45
46
        if ($this->dimension > 0) {
47
            return new ArrayExpression($value, $this->dbType, $this->dimension);
48
        }
49
        if (in_array($this->dbType, [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
0 ignored issues
show
Bug introduced by
The type yii\db\pgsql\Schema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
            return new JsonExpression($value, $this->dbType);
51
        }
52
53
        return $this->typecast($value);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function phpTypecast($value)
60
    {
61
        if ($this->dimension > 0) {
62
            if (!is_array($value)) {
63
                $value = $this->getArrayParser()->parse($value);
64
            }
65
            if (is_array($value)) {
66
                array_walk_recursive($value, function (&$val, $key) {
67
                    $val = $this->phpTypecastValue($val);
68
                });
69
            } elseif ($value === null) {
0 ignored issues
show
introduced by
The condition $value === null is always true.
Loading history...
70
                return null;
71
            }
72
73
            return $value;
74
        }
75
76
        return $this->phpTypecastValue($value);
77
    }
78
79
    /**
80
     * Casts $value after retrieving from the DBMS to PHP representation.
81
     *
82
     * @param string|null $value
83
     * @return bool|mixed|null
84
     */
85
    protected function phpTypecastValue($value)
86
    {
87
        if ($value === null) {
88
            return null;
89
        }
90
91
        switch ($this->type) {
92
            case Schema::TYPE_BOOLEAN:
93
                switch (strtolower($value)) {
94
                    case 't':
95
                    case 'true':
96
                        return true;
97
                    case 'f':
98
                    case 'false':
99
                        return false;
100
                }
101
                return (bool) $value;
102
            case Schema::TYPE_JSON:
103
                return json_decode($value, true);
104
        }
105
106
        return parent::phpTypecast($value);
107
    }
108
109
    /**
110
     * Creates instance of ArrayParser
111
     *
112
     * @return ArrayParser
113
     */
114
    protected function getArrayParser()
115
    {
116
        static $parser = null;
117
118
        if ($parser === null) {
119
            $parser = new ArrayParser();
120
        }
121
122
        return $parser;
123
    }
124
}
125