1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Sqlite; |
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 SQLite Server. |
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 and is typically used 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\Sqlite\ColumnSchema; |
27
|
|
|
* |
28
|
|
|
* $column = new ColumnSchema(); |
29
|
|
|
* $column->name('id'); |
30
|
|
|
* $column->allowNull(false); |
31
|
|
|
* $column->dbType('integer'); |
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 a value from its PHP representation to a database-specific representation. |
43
|
|
|
* |
44
|
|
|
* If the value is null or an {@see Expression}, it won't be converted. |
45
|
|
|
* |
46
|
|
|
* @param mixed $value The value to be converted. |
47
|
|
|
* |
48
|
|
|
* @return mixed The converted value. |
49
|
|
|
*/ |
50
|
62 |
|
public function dbTypecast(mixed $value): mixed |
51
|
|
|
{ |
52
|
62 |
|
if ($value === null || $value instanceof ExpressionInterface) { |
53
|
22 |
|
return $value; |
54
|
|
|
} |
55
|
|
|
|
56
|
62 |
|
if ($this->getType() === SchemaInterface::TYPE_JSON) { |
57
|
4 |
|
return new JsonExpression($value, $this->getDbType()); |
58
|
|
|
} |
59
|
|
|
|
60
|
61 |
|
return parent::dbTypecast($value); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Converts the input value according to {@see phpType} after retrieval from the database. |
65
|
|
|
* |
66
|
|
|
* If the value is null or an {@see Expression}, it won't be converted. |
67
|
|
|
* |
68
|
|
|
* @param mixed $value The value to be converted. |
69
|
|
|
* |
70
|
|
|
* @throws JsonException |
71
|
|
|
* @return mixed The converted value. |
72
|
|
|
*/ |
73
|
79 |
|
public function phpTypecast(mixed $value): mixed |
74
|
|
|
{ |
75
|
79 |
|
if ($value === null) { |
76
|
1 |
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
79 |
|
if ($this->getType() === SchemaInterface::TYPE_JSON) { |
80
|
26 |
|
return json_decode((string) $value, true, 512, JSON_THROW_ON_ERROR); |
81
|
|
|
} |
82
|
|
|
|
83
|
78 |
|
return parent::phpTypecast($value); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|