-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathColumnSchema.php
215 lines (189 loc) · 6.19 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* @link https://github.com/tigrov/yii2-pgsql
* @author Sergei Tigrov <[email protected]>
*/
namespace tigrov\pgsql;
use yii\db\ExpressionInterface;
use yii\db\JsonExpression;
use yii\db\PdoValue;
/**
* ColumnSchema is the improved class which describes the metadata of a column in a PostgreSQL database table
*
* @author Sergei Tigrov <[email protected]>
*/
class ColumnSchema extends \yii\db\pgsql\ColumnSchema
{
/**
* @var string the delimiter character to be used between values in arrays made of this type.
*/
public $delimiter;
/**
* @var ColumnSchema[]|null columns of composite type
*/
public $columns;
/**
* @inheritdoc
*/
public function dbTypecast($value)
{
if ($value instanceof ExpressionInterface) {
return $value;
}
if ($this->dimension > 0) {
if ($value === null) {
return null;
}
return new ArrayExpression($value, $this->dbType, $this->dimension, $this);
}
return $this->dbTypecastValue($value);
}
/**
* Converts the input value according to [[type]] and [[dbType]] for use in a db query.
* @param mixed $value input value
* @return mixed converted value.
*/
public function dbTypecastValue($value)
{
if ($value === null) {
return null;
}
switch ($this->type) {
case Schema::TYPE_BIT:
return decbin($value);
case Schema::TYPE_BINARY:
return is_string($value) ? new PdoValue($value, \PDO::PARAM_LOB) : $value;
case Schema::TYPE_TIMESTAMP:
case Schema::TYPE_DATETIME:
return \Yii::$app->formatter->asDatetime($value, 'yyyy-MM-dd HH:mm:ss');
case Schema::TYPE_DATE:
return \Yii::$app->formatter->asDate($value, 'yyyy-MM-dd');
case Schema::TYPE_TIME:
return \Yii::$app->formatter->asTime($value, 'HH:mm:ss');
case Schema::TYPE_JSON:
return new JsonExpression($value, $this->dbType);
case Schema::TYPE_COMPOSITE:
return $this->createCompositeExpression($value);
}
return $this->typecast($value);
}
/**
* @inheritdoc
*/
public function phpTypecast($value)
{
if ($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);
});
}
return $value;
}
return $this->phpTypecastValue($value);
}
/**
* Converts the input value according to [[phpType]] after retrieval from the database.
* @param mixed $value input value
* @return mixed converted value
*/
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_BIT:
return bindec($value);
case Schema::TYPE_BINARY:
return is_string($value) && strncmp($value, '\\x', 2) === 0 ? pack('H*', substr($value, 2)) : $value;
case Schema::TYPE_JSON:
return json_decode($value, true);
case Schema::TYPE_TIMESTAMP:
case Schema::TYPE_TIME:
case Schema::TYPE_DATE:
case Schema::TYPE_DATETIME:
return new \DateTime($value);
case Schema::TYPE_COMPOSITE:
return $this->phpTypecastComposite($value);
}
return $this->typecast($value);
}
/**
* Converts the composite type to PHP
* @param array|string|object|null $value the value to be converted
* @return array|object|null Composite object as described in `ColumnSchema::$phpType` (@see `Schema::$compositeMap`) or `null`
*/
public function phpTypecastComposite($value)
{
if (is_string($value)) {
$value = $this->getCompositeParser()->parse($value);
}
if (is_array($value)) {
$result = [];
$fields = array_keys($this->columns);
foreach ($value as $i => $item) {
$field = is_int($i) ? $fields[$i] : $i;
if (isset($this->columns[$field])) {
$result[$field] = $this->columns[$field]->phpTypecast($item);
}
}
return $this->createCompositeObject($result);
} elseif (!$value instanceof $this->phpType) {
return null;
}
return $value;
}
/**
* Creates an object for the composite type.
* @param array $values to be passed to the class constructor
* @return mixed
*/
public function createCompositeObject($values)
{
switch ($this->phpType) {
case 'array':
return $values;
case 'object':
return (object)$values;
}
return \Yii::createObject($this->phpType, [$values]);
}
/**
* Creates CompositeExpression object
*
* @param array|mixed $value the composite type content. Either represented as an array of values or a composite
* object which corresponds to Schema::compositeMap and could be converted to array.
* @return CompositeExpression
*/
public function createCompositeExpression($value)
{
return new CompositeExpression($value, $this->dbType, $this);
}
/**
* Creates instance of CompositeParser
*
* @return CompositeParser
*/
protected function getCompositeParser()
{
static $parser = null;
if ($parser === null) {
$parser = new CompositeParser();
}
return $parser;
}
}