Passed
Pull Request — master (#303)
by
unknown
03:27
created

CompositeExpression::getColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql\Composite;
6
7
use Yiisoft\Db\Expression\ExpressionInterface;
8
use Yiisoft\Db\Schema\ColumnSchemaInterface;
9
10
/**
11
 * Represents a composite SQL expression.
12
 *
13
 * For example:
14
 *
15
 * ```php
16
 * new CompositeExpression(['price' => 10, 'currency_code' => 'USD']);
17
 * ```
18
 *
19
 * Will be encoded to `ROW(10, USD)`
20
 */
21
class CompositeExpression implements ExpressionInterface
22
{
23
    /**
24
     * @param ColumnSchemaInterface[]|null $columns
25
     * @psalm-param array<string, ColumnSchemaInterface>|null $columns
26
     */
27 3
    public function __construct(
28
        private mixed $value,
29
        private string|null $type = null,
30
        private array|null $columns = null,
31
    ) {
32 3
    }
33
34
    /**
35
     * The composite type name.
36
     *
37
     * Defaults to `null` which means the type isn't explicitly specified.
38
     *
39
     * Note that in the case where a type isn't specified explicitly and DBMS can't guess it from the context, SQL error
40
     * will be raised.
41
     */
42 9
    public function getType(): string|null
43
    {
44 9
        return $this->type;
45
    }
46
47
    /**
48
     * @return ColumnSchemaInterface[]|null
49
     */
50 7
    public function getColumns(): array|null
51
    {
52 7
        return $this->columns;
53
    }
54
55
    /**
56
     * The composite type's content. It can be represented as an associative array of the composite type's column names
57
     * and values.
58
     */
59 11
    public function getValue(): mixed
60
    {
61 11
        return $this->value;
62
    }
63
64
    /**
65
     * Sorted values according to order of the composite type columns and filled with default values skipped items.
66
     */
67 9
    public function getNormalizedValue(): mixed
68
    {
69 9
        if ($this->columns === null || !is_array($this->value)) {
70 6
            return $this->value;
71
        }
72
73 3
        $value = [];
74 3
        $columns = $this->columns;
75
76 3
        if (is_int(array_key_first($this->value))) {
77 1
            $columns = array_values($this->columns);
78
        }
79
80 3
        foreach ($columns as $name => $column) {
81 3
            if (array_key_exists($name, $this->value)) {
82
                /** @psalm-suppress MixedAssignment */
83 3
                $value[$name] = $this->value[$name];
84
            } else {
85
                /** @psalm-suppress MixedAssignment */
86 1
                $value[$name] = $column->getDefaultValue();
87
            }
88
        }
89
90 3
        return $value;
91
    }
92
}
93