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

CompositeExpression::getNormalizedValue()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

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