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