Skip to content

Commit baa3fa4

Browse files
committed
feat(Form): refactor property handling in Form class and add test for appendConstValue method
1 parent ab5cc29 commit baa3fa4

File tree

2 files changed

+114
-6
lines changed

2 files changed

+114
-6
lines changed

src/Structure/Form/Form.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,20 +483,30 @@ public function appendConstValue(array $input): void
483483

484484
if ($this->getItems()->getType()->isComplex()) {
485485
$count = count($input);
486-
$properties = null;
486+
$properties = [];
487487
for ($i = 0; $i < $count; ++$i) {
488-
$items = clone $this->getItems();
488+
// Use existing property if available, otherwise clone items
489+
$existingProperty = $this->getProperties()[$i] ?? null;
490+
if ($existingProperty) {
491+
$items = $existingProperty;
492+
} else {
493+
$items = clone $this->getItems();
494+
$items->setKey((string) $i);
495+
$items->setSort($i);
496+
}
489497
$items->appendConstValue($input[$i] ?? []);
490-
$properties[] = $items;
498+
$properties[$i] = $items;
491499
}
492500
} else {
493-
$properties = null;
501+
$properties = [];
494502
$index = 0;
495503
foreach ($input as $item) {
496504
$property = clone $this->getItems();
505+
$property->setKey((string) $index);
497506
$property->setValue(Value::buildConst($item));
498-
$property->setSort($index++);
499-
$properties[] = $property;
507+
$property->setSort($index);
508+
$properties[$index] = $property;
509+
++$index;
500510
}
501511
}
502512
$this->setProperties($properties);

tests/Structure/Form/FormTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,104 @@ public function testAppendConstValue()
945945
$this->assertEquals($result, $form->getKeyValue(execExpression: false));
946946
}
947947

948+
public function testAppendConstValue2()
949+
{
950+
$form = $this->builder->build(json_decode(<<<'JSON'
951+
{
952+
"type": "object",
953+
"key": "root",
954+
"sort": 0,
955+
"title": null,
956+
"description": null,
957+
"required": [
958+
"data"
959+
],
960+
"value": null,
961+
"encryption": false,
962+
"encryption_value": null,
963+
"items": null,
964+
"properties": {
965+
"data": {
966+
"type": "object",
967+
"properties": {
968+
"details": {
969+
"type": "array",
970+
"items": {
971+
"type": "object",
972+
"title": "",
973+
"description": "",
974+
"value": null,
975+
"encryption": false,
976+
"properties": {}
977+
},
978+
"properties": {
979+
"0": {
980+
"type": "object",
981+
"title": "",
982+
"description": "",
983+
"value": null,
984+
"encryption": false,
985+
"properties": {
986+
"sku": {
987+
"type": "string",
988+
"title": "",
989+
"description": "",
990+
"value": null,
991+
"encryption": false
992+
},
993+
"qty": {
994+
"type": "string",
995+
"title": "",
996+
"description": "",
997+
"value": null,
998+
"encryption": false
999+
}
1000+
},
1001+
"required": [
1002+
"sku",
1003+
"qty"
1004+
]
1005+
}
1006+
},
1007+
"title": "",
1008+
"description": "",
1009+
"value": null,
1010+
"encryption": false,
1011+
"required": [
1012+
"0"
1013+
]
1014+
}
1015+
},
1016+
"required": [
1017+
"details"
1018+
],
1019+
"title": "",
1020+
"description": "",
1021+
"value": null,
1022+
"encryption": false
1023+
}
1024+
}
1025+
}
1026+
JSON, true));
1027+
$data = [
1028+
'data' => [
1029+
'details' => [
1030+
[
1031+
'sku' => 'SKU123',
1032+
'qty' => '123',
1033+
],
1034+
[
1035+
'sku' => 'SKU456',
1036+
'qty' => '456',
1037+
],
1038+
],
1039+
],
1040+
];
1041+
$form->appendConstValue($data);
1042+
$responseResult = $form->getKeyValue(check: true);
1043+
$this->assertEquals($data, $responseResult);
1044+
}
1045+
9481046
public function testGetKeyValue()
9491047
{
9501048
$form = $this->builder->build($this->getFormJsonArray());

0 commit comments

Comments
 (0)