Skip to content

Commit b61a8c3

Browse files
committed
Improve
1 parent 915fc2b commit b61a8c3

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

src/Column/ColumnFactory.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,7 @@ public function fromType(string $type, array $info = []): ColumnInterface
124124
$column = parent::fromType($type, $info);
125125

126126
if ($column instanceof StructuredColumn) {
127-
/** @psalm-var array|null $defaultValue */
128-
$defaultValue = $column->getDefaultValue();
129-
130-
if (is_array($defaultValue)) {
131-
foreach ($column->getColumns() as $structuredColumnName => $structuredColumn) {
132-
if (isset($defaultValue[$structuredColumnName])) {
133-
$structuredColumn->defaultValue($defaultValue[$structuredColumnName]);
134-
}
135-
}
136-
}
127+
$this->initStructuredDefaultValue($column);
137128
}
138129

139130
return $column;
@@ -179,4 +170,25 @@ protected function normalizeNotNullDefaultValue(string $defaultValue, ColumnInte
179170

180171
return $value;
181172
}
173+
174+
/**
175+
* Initializes the default value for structured columns.
176+
*/
177+
private function initStructuredDefaultValue(StructuredColumn $column): void
178+
{
179+
/** @psalm-var array|null $defaultValue */
180+
$defaultValue = $column->getDefaultValue();
181+
182+
if (is_array($defaultValue)) {
183+
foreach ($column->getColumns() as $structuredColumnName => $structuredColumn) {
184+
if (isset($defaultValue[$structuredColumnName])) {
185+
$structuredColumn->defaultValue($defaultValue[$structuredColumnName]);
186+
187+
if ($structuredColumn instanceof StructuredColumn) {
188+
$this->initStructuredDefaultValue($structuredColumn);
189+
}
190+
}
191+
}
192+
}
193+
}
182194
}

tests/Provider/StructuredTypeProvider.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ public static function columns(): array
110110
dimension: 2,
111111
column: new StructuredColumn(
112112
dbType: 'currency_money_structured',
113-
defaultValue: null,
114113
columns: [
115114
'value' => new DoubleColumn(
116115
ColumnType::DECIMAL,
@@ -143,7 +142,7 @@ public static function columns(): array
143142
columns: [
144143
'price_from' => new StructuredColumn(
145144
dbType: 'currency_money_structured',
146-
defaultValue: null,
145+
defaultValue: ['value' => 0.0, 'currency_code' => 'USD'],
147146
columns: [
148147
'value' => new DoubleColumn(
149148
ColumnType::DECIMAL,
@@ -152,23 +151,23 @@ public static function columns(): array
152151
notNull: false,
153152
size: 10,
154153
scale: 2,
155-
defaultValue: null,
154+
defaultValue: 0.0,
156155
),
157156
'currency_code' => new StringColumn(
158157
ColumnType::CHAR,
159158
dbType: 'bpchar',
160159
name: 'currency_code',
161160
notNull: false,
162161
size: 3,
163-
defaultValue: null,
162+
defaultValue: 'USD',
164163
),
165164
],
166165
name: 'price_from',
167166
notNull: false,
168167
),
169168
'price_to' => new StructuredColumn(
170169
dbType: 'currency_money_structured',
171-
defaultValue: null,
170+
defaultValue: ['value' => 100.0, 'currency_code' => 'USD'],
172171
columns: [
173172
'value' => new DoubleColumn(
174173
ColumnType::DECIMAL,
@@ -177,15 +176,15 @@ public static function columns(): array
177176
notNull: false,
178177
size: 10,
179178
scale: 2,
180-
defaultValue: null,
179+
defaultValue: 100.0,
181180
),
182181
'currency_code' => new StringColumn(
183182
ColumnType::CHAR,
184183
dbType: 'bpchar',
185184
name: 'currency_code',
186185
notNull: false,
187186
size: 3,
188-
defaultValue: null,
187+
defaultValue: 'USD',
189188
),
190189
],
191190
name: 'price_to',

0 commit comments

Comments
 (0)