Issues (836)

framework/db/ColumnSchema.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\db;
9
10
use yii\base\BaseObject;
11
use yii\helpers\StringHelper;
12
13
/**
14
 * ColumnSchema class describes the metadata of a column in a database table.
15
 *
16
 * @author Qiang Xue <[email protected]>
17
 * @since 2.0
18
 */
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 25
    public function phpTypecast($value)
90
    {
91 25
        return $this->typecast($value);
92
    }
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 32
    public function dbTypecast($value)
102
    {
103
        // the default implementation does the same as casting for PHP, but it should be possible
104
        // to override this with annotation of explicit PDO type.
105 32
        return $this->typecast($value);
106
    }
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 32
    protected function typecast($value)
116
    {
117
        if (
118 32
            $value === ''
119 32
            && !in_array(
120 32
                $this->type,
121 32
                [
122 32
                    Schema::TYPE_TEXT,
123 32
                    Schema::TYPE_STRING,
124 32
                    Schema::TYPE_BINARY,
125 32
                    Schema::TYPE_CHAR
126 32
                ],
127 32
                true
128 32
            )
129
        ) {
130
            return null;
131
        }
132
133
        if (
134 32
            $value === null
135 32
            || gettype($value) === $this->phpType
136 25
            || $value instanceof ExpressionInterface
137 32
            || $value instanceof Query
138
        ) {
139 31
            return $value;
140
        }
141
142
        if (
143 25
            is_array($value)
144 25
            && count($value) === 2
145 25
            && isset($value[1])
146 25
            && in_array($value[1], $this->getPdoParamTypes(), true)
147
        ) {
148
            return new PdoValue($value[0], $value[1]);
149
        }
150
151 25
        switch ($this->phpType) {
152 25
            case 'resource':
153 25
            case 'string':
154 2
                if (is_resource($value)) {
155
                    return $value;
156
                }
157 2
                if (is_float($value)) {
158
                    // ensure type cast always has . as decimal separator in all locales
159
                    return StringHelper::floatToString($value);
160
                }
161
                if (
162 2
                    is_numeric($value)
163 2
                    && ColumnSchemaBuilder::CATEGORY_NUMERIC === ColumnSchemaBuilder::$typeCategoryMap[$this->type]
164
                ) {
165
                    // https://github.com/yiisoft/yii2/issues/14663
166
                    return $value;
167
                }
168
169 2
                if (PHP_VERSION_ID >= 80100 && is_object($value) && $value instanceof \BackedEnum) {
0 ignored issues
show
The type BackedEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
170
                    return (string) $value->value;
171
                }
172
173 2
                return (string) $value;
174 25
            case 'integer':
175 25
                if (PHP_VERSION_ID >= 80100 && is_object($value) && $value instanceof \BackedEnum) {
176
                    return (int) $value->value;
177
                }
178 25
                return (int) $value;
179 2
            case 'boolean':
180
                // treating a 0 bit value as false too
181
                // https://github.com/yiisoft/yii2/issues/9006
182 2
                return (bool) $value && $value !== "\0" && strtolower($value) !== 'false';
183 2
            case 'double':
184 2
                return (float) $value;
185
        }
186
187
        return $value;
188
    }
189
190
    /**
191
     * @return int[] array of numbers that represent possible PDO parameter types
192
     */
193
    private function getPdoParamTypes()
194
    {
195
        return [\PDO::PARAM_BOOL, \PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_NULL, \PDO::PARAM_STMT];
196
    }
197
}
198