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
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
198 |
|
public function dbTypecast($value) |
30
|
|
|
{ |
31
|
198 |
|
if ($value instanceof ExpressionInterface) { |
32
|
83 |
|
return $value; |
33
|
|
|
} |
34
|
|
|
|
35
|
196 |
|
if ($this->dimension > 0) { |
36
|
|
|
return new ArrayExpression($value, $this->dbType, $this->dimension); |
37
|
|
|
} |
38
|
196 |
|
if (in_array($this->dbType, [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) { |
39
|
3 |
|
return new JsonExpression($value, $this->type); |
40
|
|
|
} |
41
|
|
|
|
42
|
193 |
|
return $this->typecast($value); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
143 |
|
public function phpTypecast($value) |
49
|
|
|
{ |
50
|
143 |
|
if ($this->dimension > 0) { |
51
|
11 |
|
if (!is_array($value)) { |
52
|
11 |
|
$value = $this->getArrayParser()->parse($value); |
53
|
|
|
} |
54
|
11 |
|
if (is_array($value)) { |
55
|
2 |
|
array_walk_recursive($value, function (&$val, $key) { |
|
|
|
|
56
|
2 |
|
$val = $this->phpTypecastValue($val); |
57
|
2 |
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
11 |
|
return new ArrayExpression($value, $this->dbType, $this->dimension); |
61
|
|
|
} |
62
|
|
|
|
63
|
143 |
|
return $this->phpTypecastValue($value); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Casts $value after retrieving from the DBMS to PHP representation. |
68
|
|
|
* |
69
|
|
|
* @param string|null $value |
70
|
|
|
* @return bool|mixed|null |
71
|
|
|
*/ |
72
|
143 |
|
protected function phpTypecastValue($value) |
73
|
|
|
{ |
74
|
143 |
|
if ($value === null) { |
75
|
41 |
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
143 |
|
switch ($this->type) { |
79
|
143 |
|
case Schema::TYPE_BOOLEAN: |
80
|
56 |
|
switch (strtolower($value)) { |
81
|
56 |
|
case 't': |
82
|
56 |
|
case 'true': |
83
|
|
|
return true; |
84
|
56 |
|
case 'f': |
85
|
56 |
|
case 'false': |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
56 |
|
return (bool) $value; |
89
|
143 |
|
case Schema::TYPE_JSON: |
90
|
33 |
|
return json_decode($value, true); |
91
|
|
|
} |
92
|
|
|
|
93
|
143 |
|
return parent::phpTypecast($value); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Creates instance of ArrayParser |
98
|
|
|
* |
99
|
|
|
* @return ArrayParser |
100
|
|
|
*/ |
101
|
11 |
|
protected function getArrayParser() |
102
|
|
|
{ |
103
|
11 |
|
static $parser = null; |
104
|
|
|
|
105
|
11 |
|
if ($parser === null) { |
106
|
1 |
|
$parser = new ArrayParser(); |
107
|
|
|
} |
108
|
|
|
|
109
|
11 |
|
return $parser; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.