Passed
Push — master ( 14edf8...640dd0 )
by Alexander
08:24
created

ColumnSchema::dimension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql\Schema;
6
7
use Yiisoft\Db\Expression\ArrayExpression;
8
use Yiisoft\Db\Expression\ExpressionInterface;
9
use Yiisoft\Db\Expression\JsonExpression;
10
use Yiisoft\Db\Pgsql\Query\ArrayParser;
11
use Yiisoft\Db\Schema\ColumnSchema as AbstractColumnSchema;
12
13
class ColumnSchema extends AbstractColumnSchema
14
{
15
    /**
16
     * @var int the dimension of array. Defaults to 0, means this column is not an array.
17
     */
18
    private int $dimension = 0;
19
20
    /**
21
     * @var string name of associated sequence if column is auto-incremental.
22
     */
23
    private ?string $sequenceName = null;
24
25
    /**
26
     * Converts the input value according to {@see type} and {@see dbType} for use in a db query.
27
     *
28
     * If the value is null or an {@see Expression}, it will not be converted.
29
     *
30
     * @param mixed $value input value
31
     *
32
     * @return mixed converted value. This may also be an array containing the value as the first element and the PDO
33
     * type as the second element.
34
     */
35 49
    public function dbTypecast($value)
36
    {
37 49
        if ($value === null) {
38 5
            return $value;
39
        }
40
41 49
        if ($value instanceof ExpressionInterface) {
42 10
            return $value;
43
        }
44
45 44
        if ($this->dimension > 0) {
46
            return new ArrayExpression($value, $this->getDbType(), $this->dimension);
47
        }
48
49 44
        if (\in_array($this->getDbType(), [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
50 1
            return new JsonExpression($value, $this->getDbType());
51
        }
52
53 43
        return $this->typecast($value);
54
    }
55
56
    /**
57
     * Converts the input value according to {@see phpType} after retrieval from the database.
58
     *
59
     * If the value is null or an {@see Expression}, it will not be converted.
60
     *
61
     * @param mixed $value input value
62
     *
63
     * @return mixed converted value
64
     */
65 29
    public function phpTypecast($value)
66
    {
67 29
        if ($this->dimension > 0) {
68
            if (!\is_array($value)) {
69
                $value = $this->getArrayParser()->parse($value);
70
            }
71
            if (\is_array($value)) {
72
                \array_walk_recursive($value, function (&$val, $key) {
73
                    $val = $this->phpTypecastValue($val);
74
                });
75
            } elseif ($value === null) {
0 ignored issues
show
introduced by
The condition $value === null is always true.
Loading history...
76
                return null;
77
            }
78
79
            return $value;
80
        }
81
82 29
        return $this->phpTypecastValue($value);
83
    }
84
85
    /**
86
     * Casts $value after retrieving from the DBMS to PHP representation.
87
     *
88
     * @param string|int|null $value
89
     *
90
     * @return bool|mixed|null
91
     */
92 29
    protected function phpTypecastValue($value)
93
    {
94 29
        if ($value === null) {
95
            return null;
96
        }
97
98 29
        switch ($this->getType()) {
99
            case Schema::TYPE_BOOLEAN:
100
                $value = \is_string($value) ? \strtolower($value) : $value;
101
102
                switch ($value) {
103
                    case 't':
104
                    case 'true':
105
                        return true;
106
                    case 'f':
107
                    case 'false':
108
                        return false;
109
                }
110
111
                return (bool) $value;
112
            case Schema::TYPE_JSON:
113 27
                return \json_decode($value, true, 512, JSON_THROW_ON_ERROR);
114
        }
115
116 29
        return parent::phpTypecast($value);
117
    }
118
119
    /**
120
     * Creates instance of ArrayParser
121
     *
122
     * @return ArrayParser
123
     */
124
    protected function getArrayParser(): ArrayParser
125
    {
126
        static $parser = null;
127
128
        if ($parser === null) {
129
            $parser = new ArrayParser();
130
        }
131
132
        return $parser;
133
    }
134
135 1
    public function getDimension(): int
136
    {
137 1
        return $this->dimension;
138
    }
139
140 62
    public function getSequenceName(): ?string
141
    {
142 62
        return $this->sequenceName;
143
    }
144
145 91
    public function dimension(int $dimension): void
146
    {
147 91
        $this->dimension = $dimension;
148 91
    }
149
150 59
    public function sequenceName(?string $sequenceName): void
151
    {
152 59
        $this->sequenceName = $sequenceName;
153 59
    }
154
}
155