-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
Copy pathColumnSchema.php
143 lines (127 loc) · 4.09 KB
/
ColumnSchema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db\pgsql;
use yii\db\ArrayExpression;
use yii\db\ExpressionInterface;
use yii\db\JsonExpression;
/**
* Class ColumnSchema for PostgreSQL database.
*
* @author Dmytro Naumenko <[email protected]>
*/
class ColumnSchema extends \yii\db\ColumnSchema
{
/**
* @var int the dimension of array. Defaults to 0, means this column is not an array.
*/
public $dimension = 0;
/**
* @var bool whether the column schema should OMIT using JSON support feature.
* You can use this property to make upgrade to Yii 2.0.14 easier.
* Default to `false`, meaning JSON support is enabled.
*
* @since 2.0.14.1
* @deprecated Since 2.0.14.1 and will be removed in 2.1.
*/
public $disableJsonSupport = false;
/**
* @var bool whether the column schema should OMIT using PgSQL Arrays support feature.
* You can use this property to make upgrade to Yii 2.0.14 easier.
* Default to `false`, meaning Arrays support is enabled.
*
* @since 2.0.14.1
* @deprecated Since 2.0.14.1 and will be removed in 2.1.
*/
public $disableArraySupport = false;
/**
* @var bool whether the Array column value should be unserialized to an [[ArrayExpression]] object.
* You can use this property to make upgrade to Yii 2.0.14 easier.
* Default to `true`, meaning arrays are unserialized to [[ArrayExpression]] objects.
*
* @since 2.0.14.1
* @deprecated Since 2.0.14.1 and will be removed in 2.1.
*/
public $deserializeArrayColumnToArrayExpression = true;
/**
* {@inheritdoc}
*/
public function dbTypecast($value)
{
if ($value instanceof ExpressionInterface) {
return $value;
}
if (!$this->disableArraySupport && $this->dimension > 0) {
return new ArrayExpression($value, $this->dbType, $this->dimension);
}
if (!$this->disableJsonSupport && in_array($this->dbType, [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
return new JsonExpression($value, $this->type);
}
return $this->typecast($value);
}
/**
* {@inheritdoc}
*/
public function phpTypecast($value)
{
if (!$this->disableArraySupport && $this->dimension > 0) {
if (!is_array($value)) {
$value = $this->getArrayParser()->parse($value);
}
if (is_array($value)) {
array_walk_recursive($value, function (&$val, $key) {
$val = $this->phpTypecastValue($val);
});
} elseif ($value === null) {
return null;
}
return $this->deserializeArrayColumnToArrayExpression
? new ArrayExpression($value, $this->dbType, $this->dimension)
: $value;
}
return $this->phpTypecastValue($value);
}
/**
* Casts $value after retrieving from the DBMS to PHP representation.
*
* @param string|null $value
* @return bool|mixed|null
*/
protected function phpTypecastValue($value)
{
if ($value === null) {
return null;
}
switch ($this->type) {
case Schema::TYPE_BOOLEAN:
switch (strtolower($value)) {
case 't':
case 'true':
return true;
case 'f':
case 'false':
return false;
}
return (bool) $value;
case Schema::TYPE_JSON:
return $this->disableJsonSupport ? $value : json_decode($value, true);
}
return parent::phpTypecast($value);
}
/**
* Creates instance of ArrayParser
*
* @return ArrayParser
*/
protected function getArrayParser()
{
static $parser = null;
if ($parser === null) {
$parser = new ArrayParser();
}
return $parser;
}
}