Passed
Pull Request — master (#19)
by Wilmer
12:28
created

ColumnSchema::dbTypecast()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 11
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 21
rs 9.2222
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
/**
14
 * Class ColumnSchema for Postgres SQL database.
15
 */
16
class ColumnSchema extends AbstractColumnSchema
17
{
18
    /**
19
     * @var int the dimension of array. Defaults to 0, means this column is not an array.
20
     */
21
    public int $dimension = 0;
22
23
    /**
24
     * @var string name of associated sequence if column is auto-incremental.
25
     */
26
    public ?string $sequenceName = null;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function dbTypecast($value)
32
    {
33
        if ($value === null) {
34
            return $value;
35
        }
36
37
        if ($value instanceof ExpressionInterface) {
38
            return $value;
39
        }
40
41
        if ($this->dimension > 0) {
42
            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...
43
                ? (string) $value
44
                : new ArrayExpression($value, $this->getDbType(), $this->dimension);
45
        }
46
47
        if (\in_array($this->getDbType(), [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
48
            return new JsonExpression($value, $this->getDbType());
49
        }
50
51
        return $this->typecast($value);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function phpTypecast($value)
58
    {
59
        if ($this->dimension > 0) {
60
            if (!\is_array($value)) {
61
                $value = $this->getArrayParser()->parse($value);
62
            }
63
            if (\is_array($value)) {
64
                array_walk_recursive($value, function (&$val, $key) {
65
                    $val = $this->phpTypecastValue($val);
66
                });
67
            } elseif ($value === null) {
0 ignored issues
show
introduced by
The condition $value === null is always true.
Loading history...
68
                return null;
69
            }
70
71
            return $value;
72
        }
73
74
        return $this->phpTypecastValue($value);
75
    }
76
77
    /**
78
     * Casts $value after retrieving from the DBMS to PHP representation.
79
     *
80
     * @param string|null $value
81
     *
82
     * @return bool|mixed|null
83
     */
84
    protected function phpTypecastValue($value)
85
    {
86
        if ($value === null) {
87
            return null;
88
        }
89
90
        switch ($this->getType()) {
91
            case Schema::TYPE_BOOLEAN:
92
                $value = \is_string($value) ? strtolower($value) : $value;
0 ignored issues
show
introduced by
The condition is_string($value) is always true.
Loading history...
93
94
                switch ($value) {
95
                    case 't':
96
                    case 'true':
97
                        return true;
98
                    case 'f':
99
                    case 'false':
100
                        return false;
101
                }
102
103
                return (bool) $value;
104
            case Schema::TYPE_JSON:
105
                return \json_decode($value, true);
106
        }
107
108
        return parent::phpTypecast($value);
109
    }
110
111
    /**
112
     * Creates instance of ArrayParser
113
     *
114
     * @return ArrayParser
115
     */
116
    protected function getArrayParser(): ArrayParser
117
    {
118
        static $parser = null;
119
120
        if ($parser === null) {
121
            $parser = new ArrayParser();
122
        }
123
124
        return $parser;
125
    }
126
}
127