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