1 | <?php |
||
19 | class ColumnSchema extends BaseObject |
||
20 | { |
||
21 | /** |
||
22 | * @var string name of this column (without quotes). |
||
23 | */ |
||
24 | public $name; |
||
25 | /** |
||
26 | * @var bool whether this column can be null. |
||
27 | */ |
||
28 | public $allowNull; |
||
29 | /** |
||
30 | * @var string abstract type of this column. Possible abstract types include: |
||
31 | * char, string, text, boolean, smallint, integer, bigint, float, decimal, datetime, |
||
32 | * timestamp, time, date, binary, and money. |
||
33 | */ |
||
34 | public $type; |
||
35 | /** |
||
36 | * @var string the PHP type of this column. Possible PHP types include: |
||
37 | * `string`, `boolean`, `integer`, `double`, `array`. |
||
38 | */ |
||
39 | public $phpType; |
||
40 | /** |
||
41 | * @var string the DB type of this column. Possible DB types vary according to the type of DBMS. |
||
42 | */ |
||
43 | public $dbType; |
||
44 | /** |
||
45 | * @var mixed default value of this column |
||
46 | */ |
||
47 | public $defaultValue; |
||
48 | /** |
||
49 | * @var array enumerable values. This is set only if the column is declared to be an enumerable type. |
||
50 | */ |
||
51 | public $enumValues; |
||
52 | /** |
||
53 | * @var int display size of the column. |
||
54 | */ |
||
55 | public $size; |
||
56 | /** |
||
57 | * @var int precision of the column data, if it is numeric. |
||
58 | */ |
||
59 | public $precision; |
||
60 | /** |
||
61 | * @var int scale of the column data, if it is numeric. |
||
62 | */ |
||
63 | public $scale; |
||
64 | /** |
||
65 | * @var bool whether this column is a primary key |
||
66 | */ |
||
67 | public $isPrimaryKey; |
||
68 | /** |
||
69 | * @var bool whether this column is auto-incremental |
||
70 | */ |
||
71 | public $autoIncrement = false; |
||
72 | /** |
||
73 | * @var bool whether this column is unsigned. This is only meaningful |
||
74 | * when [[type]] is `smallint`, `integer` or `bigint`. |
||
75 | */ |
||
76 | public $unsigned; |
||
77 | /** |
||
78 | * @var string comment of this column. Not all DBMS support this. |
||
79 | */ |
||
80 | public $comment; |
||
81 | |||
82 | |||
83 | /** |
||
84 | * Converts the input value according to [[phpType]] after retrieval from the database. |
||
85 | * If the value is null or an [[Expression]], it will not be converted. |
||
86 | * @param mixed $value input value |
||
87 | * @return mixed converted value |
||
88 | */ |
||
89 | 524 | public function phpTypecast($value) |
|
93 | |||
94 | /** |
||
95 | * Converts the input value according to [[type]] and [[dbType]] for use in a db query. |
||
96 | * If the value is null or an [[Expression]], it will not be converted. |
||
97 | * @param mixed $value input value |
||
98 | * @return mixed converted value. This may also be an array containing the value as the first element |
||
99 | * and the PDO type as the second element. |
||
100 | */ |
||
101 | 166 | public function dbTypecast($value) |
|
107 | |||
108 | /** |
||
109 | * Converts the input value according to [[phpType]] after retrieval from the database. |
||
110 | * If the value is null or an [[Expression]], it will not be converted. |
||
111 | * @param mixed $value input value |
||
112 | * @return mixed converted value |
||
113 | * @since 2.0.3 |
||
114 | */ |
||
115 | 909 | protected function typecast($value) |
|
116 | { |
||
117 | 909 | if ($value === '' |
|
118 | 909 | && $this->allowNull |
|
119 | 10 | && !in_array( |
|
120 | 10 | $this->type, |
|
121 | [ |
||
122 | 10 | Schema::TYPE_TEXT, |
|
123 | Schema::TYPE_STRING, |
||
124 | Schema::TYPE_BINARY, |
||
125 | Schema::TYPE_CHAR |
||
126 | ], |
||
127 | 909 | true) |
|
128 | ) { |
||
129 | 3 | return null; |
|
130 | } |
||
131 | |||
132 | 909 | if ($value === null |
|
133 | 909 | || gettype($value) === $this->phpType |
|
134 | 601 | || $value instanceof ExpressionInterface |
|
135 | 909 | || $value instanceof Query |
|
136 | ) { |
||
137 | 860 | return $value; |
|
138 | } |
||
139 | |||
140 | 601 | if (is_array($value) |
|
141 | 601 | && count($value) === 2 |
|
142 | 601 | && isset($value[1]) |
|
143 | 601 | && in_array($value[1], $this->getPdoParamTypes(), true) |
|
144 | ) { |
||
145 | return new PdoValue($value[0], $value[1]); |
||
146 | } |
||
147 | |||
148 | 601 | switch ($this->phpType) { |
|
149 | 601 | case 'resource': |
|
150 | 534 | case 'string': |
|
151 | 191 | if (is_resource($value)) { |
|
152 | return $value; |
||
153 | } |
||
154 | 191 | if (is_float($value)) { |
|
155 | // ensure type cast always has . as decimal separator in all locales |
||
156 | 6 | return StringHelper::floatToString($value); |
|
157 | } |
||
158 | 185 | return (string) $value; |
|
159 | 426 | case 'integer': |
|
160 | 422 | return (int) $value; |
|
161 | 90 | case 'boolean': |
|
162 | // treating a 0 bit value as false too |
||
163 | // https://github.com/yiisoft/yii2/issues/9006 |
||
164 | 23 | return (bool) $value && $value !== "\0"; |
|
165 | 87 | case 'double': |
|
166 | 84 | return (float) $value; |
|
167 | } |
||
168 | |||
169 | 3 | return $value; |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return int[] array of numbers that represent possible PDO parameter types |
||
174 | */ |
||
175 | private function getPdoParamTypes() |
||
179 | } |
||
180 |