Passed
Push — master ( b4b7d4...f84d88 )
by Wilmer
13:54 queued 12:25
created

ColumnSchema::getDimension()   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 0
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 $this->disableArraySupport
0 ignored issues
show
Bug Best Practice introduced by
The property disableArraySupport does not exist on Yiisoft\Db\Pgsql\Schema\ColumnSchema. Did you maybe forget to declare it?
Loading history...
47
                ? (string) $value
48
                : new ArrayExpression($value, $this->getDbType(), $this->dimension);
49
        }
50
51 44
        if (\in_array($this->getDbType(), [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
52 1
            return new JsonExpression($value, $this->getDbType());
53
        }
54
55 43
        return $this->typecast($value);
56
    }
57
58
    /**
59
     * Converts the input value according to {@see phpType} after retrieval from the database.
60
     *
61
     * If the value is null or an {@see Expression}, it will not be converted.
62
     *
63
     * @param mixed $value input value
64
     *
65
     * @return mixed converted value
66
     */
67 29
    public function phpTypecast($value)
68
    {
69 29
        if ($this->dimension > 0) {
70
            if (!\is_array($value)) {
71
                $value = $this->getArrayParser()->parse($value);
72
            }
73
            if (\is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
74
                \array_walk_recursive($value, function (&$val, $key) {
75
                    $val = $this->phpTypecastValue($val);
76
                });
77
            } elseif ($value === null) {
78
                return null;
79
            }
80
81
            return $value;
82
        }
83
84 29
        return $this->phpTypecastValue($value);
85
    }
86
87
    /**
88
     * Casts $value after retrieving from the DBMS to PHP representation.
89
     *
90
     * @param string|null $value
91
     *
92
     * @return bool|mixed|null
93
     */
94 29
    protected function phpTypecastValue(?string $value)
95
    {
96 29
        if ($value === null) {
97
            return null;
98
        }
99
100 29
        switch ($this->getType()) {
101
            case Schema::TYPE_BOOLEAN:
102
                $value = \is_string($value) ? \strtolower($value) : $value;
0 ignored issues
show
introduced by
The condition is_string($value) is always true.
Loading history...
103
104
                switch ($value) {
105
                    case 't':
106
                    case 'true':
107
                        return true;
108
                    case 'f':
109
                    case 'false':
110
                        return false;
111
                }
112
113
                return (bool) $value;
114
            case Schema::TYPE_JSON:
115 27
                return \json_decode($value, true, 512, JSON_THROW_ON_ERROR);
116
        }
117
118 29
        return parent::phpTypecast($value);
119
    }
120
121
    /**
122
     * Creates instance of ArrayParser
123
     *
124
     * @return ArrayParser
125
     */
126
    protected function getArrayParser(): ArrayParser
127
    {
128
        static $parser = null;
129
130
        if ($parser === null) {
131
            $parser = new ArrayParser();
132
        }
133
134
        return $parser;
135
    }
136
137 1
    public function getDimension(): int
138
    {
139 1
        return $this->dimension;
140
    }
141
142 62
    public function getSequenceName(): ?string
143
    {
144 62
        return $this->sequenceName;
145
    }
146
147 91
    public function dimension(int $dimension): void
148
    {
149 91
        $this->dimension = $dimension;
150 91
    }
151
152 59
    public function sequenceName(?string $sequenceName): void
153
    {
154 59
        $this->sequenceName = $sequenceName;
155 59
    }
156
}
157