Passed
Push — master ( fd9a54...1b375d )
by Alexander
06:02 queued 01:51
created

ColumnSchema::dbTypecast()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 4
nc 3
nop 1
crap 4
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