|
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
|
|
|
|