|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mysql; |
|
6
|
|
|
|
|
7
|
|
|
use JsonException; |
|
8
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
|
9
|
|
|
use Yiisoft\Db\Expression\JsonExpression; |
|
10
|
|
|
use Yiisoft\Db\Schema\AbstractColumnSchema; |
|
11
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
|
12
|
|
|
|
|
13
|
|
|
use function json_decode; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Represents the metadata of a column in a database table for MySQL, MariaDB. |
|
17
|
|
|
* |
|
18
|
|
|
* It provides information about the column's name, type, size, precision, and other details. |
|
19
|
|
|
* |
|
20
|
|
|
* It's used to store and retrieve metadata about a column in a database table, typically in conjunction with |
|
21
|
|
|
* the {@see TableSchema}, which represents the metadata of a database table as a whole. |
|
22
|
|
|
* |
|
23
|
|
|
* The following code shows how to use: |
|
24
|
|
|
* |
|
25
|
|
|
* ```php |
|
26
|
|
|
* use Yiisoft\Db\Mysql\ColumnSchema; |
|
27
|
|
|
* |
|
28
|
|
|
* $column = new ColumnSchema(); |
|
29
|
|
|
* $column->name('id'); |
|
30
|
|
|
* $column->allowNull(false); |
|
31
|
|
|
* $column->dbType('int(11)'); |
|
32
|
|
|
* $column->phpType('integer'); |
|
33
|
|
|
* $column->type('integer'); |
|
34
|
|
|
* $column->defaultValue(0); |
|
35
|
|
|
* $column->autoIncrement(true); |
|
36
|
|
|
* $column->primaryKey(true); |
|
37
|
|
|
* ``` |
|
38
|
|
|
*/ |
|
39
|
|
|
final class ColumnSchema extends AbstractColumnSchema |
|
40
|
|
|
{ |
|
41
|
|
|
/** |
|
42
|
|
|
* Converts the input value according to {@see phpType} after retrieval from the database. |
|
43
|
|
|
* |
|
44
|
|
|
* If the value is `null` or an {@see \Yiisoft\Db\Expression\Expression}, it won't be converted. |
|
45
|
|
|
* |
|
46
|
|
|
* @param mixed $value The value to convert. |
|
47
|
|
|
* |
|
48
|
|
|
* @throws JsonException If the value can't be decoded. |
|
49
|
|
|
* |
|
50
|
|
|
* @return mixed The converted value. |
|
51
|
|
|
*/ |
|
52
|
89 |
|
public function phpTypecast(mixed $value): mixed |
|
53
|
|
|
{ |
|
54
|
89 |
|
if ($value === null) { |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
89 |
|
if ($this->getType() === SchemaInterface::TYPE_JSON) { |
|
59
|
2 |
|
return json_decode((string) $value, true, 512, JSON_THROW_ON_ERROR); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
88 |
|
return parent::phpTypecast($value); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Converts the input value according to {@see type} and {@see dbType} for use in a db query. |
|
67
|
|
|
* |
|
68
|
|
|
* If the value is `null` or an {@see \Yiisoft\Db\Expression\Expression}, it won't be converted. |
|
69
|
|
|
* |
|
70
|
|
|
* @param mixed $value The value to convert. |
|
71
|
|
|
* |
|
72
|
|
|
* @return mixed The converted value. This may also be an array containing the value as the first element and the |
|
73
|
|
|
* PDO type as the second element. |
|
74
|
|
|
*/ |
|
75
|
81 |
|
public function dbTypecast(mixed $value): mixed |
|
76
|
|
|
{ |
|
77
|
81 |
|
if ($value === null || $value instanceof ExpressionInterface) { |
|
78
|
25 |
|
return $value; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
77 |
|
if ($this->getType() === SchemaInterface::TYPE_JSON) { |
|
82
|
4 |
|
return new JsonExpression($value, $this->getDbType()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
74 |
|
return parent::dbTypecast($value); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|