Passed
Pull Request — master (#22)
by Wilmer
14:07
created

ColumnSchema   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 39.53%

Importance

Changes 0
Metric Value
eloc 48
c 0
b 0
f 0
dl 0
loc 142
ccs 17
cts 43
cp 0.3953
rs 10
wmc 26

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getSequenceName() 0 3 1
A phpTypecast() 0 18 5
A dbTypecast() 0 21 6
A dimension() 0 3 1
A getDimension() 0 3 1
B phpTypecastValue() 0 25 9
A sequenceName() 0 3 1
A getArrayParser() 0 9 2
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 49
     *
32
     * @return mixed converted value. This may also be an array containing the value as the first element and the PDO
33 49
     * type as the second element.
34 5
     */
35
    public function dbTypecast($value)
36
    {
37 49
        if ($value === null) {
38 10
            return $value;
39
        }
40
41 44
        if ($value instanceof ExpressionInterface) {
42
            return $value;
43
        }
44
45
        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 44
                ? (string) $value
48 1
                : new ArrayExpression($value, $this->getDbType(), $this->dimension);
49
        }
50
51 43
        if (\in_array($this->getDbType(), [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
52
            return new JsonExpression($value, $this->getDbType());
53
        }
54
55
        return $this->typecast($value);
56
    }
57 29
58
    /**
59 29
     * 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
    public function phpTypecast($value)
68
    {
69
        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 29
                \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 29
87
    /**
88
     * Casts $value after retrieving from the DBMS to PHP representation.
89
     *
90 29
     * @param string|null $value
91
     *
92
     * @return bool|mixed|null
93
     */
94
    protected function phpTypecastValue(?string $value)
95
    {
96
        if ($value === null) {
97
            return null;
98
        }
99
100
        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 27
                    case 't':
106
                    case 'true':
107
                        return true;
108 29
                    case 'f':
109
                    case 'false':
110
                        return false;
111
                }
112
113
                return (bool) $value;
114
            case Schema::TYPE_JSON:
115
                return \json_decode($value, true, 512, JSON_THROW_ON_ERROR);
116
        }
117
118
        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
    public function getDimension(): int
138
    {
139
        return $this->dimension;
140
    }
141
142
    public function getSequenceName(): ?string
143
    {
144
        return $this->sequenceName;
145
    }
146
147
    public function dimension(int $dimension): void
148
    {
149
        $this->dimension = $dimension;
150
    }
151
152
    public function sequenceName(?string $sequenceName): void
153
    {
154
        $this->sequenceName = $sequenceName;
155
    }
156
}
157