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
|
|
|
* skipped items filled with default values, extra items removed. |
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
|
|
|
|