Passed
Pull Request — master (#295)
by
unknown
14:16
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 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
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\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 88
    public function phpTypecast(mixed $value): mixed
53
    {
54 88
        if ($value === null) {
55
            return null;
56
        }
57
58 88
        if ($this->getType() === SchemaInterface::TYPE_JSON) {
59 1
            return json_decode((string) $value, true, 512, JSON_THROW_ON_ERROR);
60
        }
61
62 87
        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 80
    public function dbTypecast(mixed $value): mixed
76
    {
77 80
        if ($value === null || $value instanceof ExpressionInterface) {
78 24
            return $value;
79
        }
80
81 76
        if ($this->getType() === SchemaInterface::TYPE_JSON) {
82 3
            return new JsonExpression($value, $this->getDbType());
83
        }
84
85 73
        return parent::dbTypecast($value);
86
    }
87
}
88