ColumnSchema   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
c 1
b 0
f 1
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A dbTypecast() 0 15 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Oracle;
6
7
use Yiisoft\Db\Command\ParamInterface;
8
use Yiisoft\Db\Expression\Expression;
9
use Yiisoft\Db\Schema\AbstractColumnSchema;
10
use Yiisoft\Db\Schema\SchemaInterface;
11
12
use function is_string;
13
use function preg_replace;
14
use function uniqid;
15
16
/**
17
 * Represents the metadata of a column in a database table for Oracle Server.
18
 *
19
 * It provides information about the column's name, type, size, precision, and other details.
20
 *
21
 * It's used to store and retrieve metadata about a column in a database table and is typically used in conjunction with
22
 * the {@see TableSchema}, which represents the metadata of a database table as a whole.
23
 *
24
 * The following code shows how to use:
25
 *
26
 * ```php
27
 * use Yiisoft\Db\Oracle\ColumnSchema;
28
 *
29
 * $column = new ColumnSchema();
30
 * $column->name('id');
31
 * $column->allowNull(false);
32
 * $column->dbType('number');
33
 * $column->phpType('integer');
34
 * $column->type('integer');
35
 * $column->defaultValue(0);
36
 * $column->autoIncrement(true);
37
 * $column->primaryKey(true);
38
 * ```
39
 */
40
final class ColumnSchema extends AbstractColumnSchema
41
{
42 90
    public function dbTypecast(mixed $value): mixed
43
    {
44 90
        if ($this->getType() === SchemaInterface::TYPE_BINARY && $this->getDbType() === 'BLOB') {
45 6
            if ($value instanceof ParamInterface && is_string($value->getValue())) {
46 1
                $value = (string) $value->getValue();
47
            }
48
49 6
            if (is_string($value)) {
50
                /** @var non-empty-string $placeholder */
51 6
                $placeholder = uniqid('exp_' . preg_replace('/[^a-z0-9]/i', '', $this->getName()));
52 6
                return new Expression('TO_BLOB(UTL_RAW.CAST_TO_RAW(:' . $placeholder . '))', [$placeholder => $value]);
53
            }
54
        }
55
56 90
        return parent::dbTypecast($value);
57
    }
58
}
59