-
Notifications
You must be signed in to change notification settings - Fork 43
/
ColumnSchema.php
78 lines (69 loc) · 2.01 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
<?php
/**
* @author Dmytro Karpovych
* @copyright 2016 NRE
*/
namespace kak\clickhouse;
use yii\db\ColumnSchema as BaseColumnSchema;
use yii\db\ExpressionInterface;
use yii\db\PdoValue;
use yii\helpers\StringHelper;
class ColumnSchema extends BaseColumnSchema
{
/**
* @inheritdoc
* @param mixed $value
* @return bool|float|int|PdoValue|mixed|null|string
*/
protected function typecast($value)
{
if ($value === ''
&& !in_array(
$this->type,
[
Schema::TYPE_TEXT,
Schema::TYPE_STRING,
Schema::TYPE_BINARY,
Schema::TYPE_CHAR
],
true)
) {
return null;
}
if ($value === null
|| gettype($value) === $this->phpType
|| $value instanceof ExpressionInterface
|| $value instanceof Query
) {
return $value;
}
if (is_array($value)
&& count($value) === 2
&& isset($value[1])
&& in_array($value[1], $this->getPdoParamTypes(), true)
) {
return new PdoValue($value[0], $value[1]);
}
switch ($this->phpType) {
case 'resource':
case 'string':
if (is_resource($value)) {
return $value;
}
if (is_float($value)) {
// ensure type cast always has . as decimal separator in all locales
return StringHelper::floatToString($value);
}
return (string) $value;
case 'integer':
return (int) $value;
case 'boolean':
// treating a 0 bit value as false too
// https://github.com/yiisoft/yii2/issues/9006
return (bool) $value && $value !== "\0";
case 'double':
return (float) $value;
}
return $value;
}
}