diff --git a/src/Entity/ModelType.php b/src/Entity/ModelType.php index c42458f..7f68799 100644 --- a/src/Entity/ModelType.php +++ b/src/Entity/ModelType.php @@ -221,7 +221,7 @@ class ModelType return match ($this->category) { ModelCategory::COMPONENT => $this->getComponentStructureFromRelations(), ModelCategory::PIECE => $this->getPieceStructureFromRelations(), - ModelCategory::PRODUCT => ['customFields' => []], + ModelCategory::PRODUCT => ['customFields' => $this->serializeCustomFields($this->productCustomFields)], }; } @@ -344,7 +344,7 @@ class ModelType private function getComponentStructureFromRelations(): array { - $structure = ['customFields' => [], 'pieces' => [], 'products' => [], 'subcomponents' => []]; + $structure = ['customFields' => $this->serializeCustomFields($this->customFields), 'pieces' => [], 'products' => [], 'subcomponents' => []]; foreach ($this->skeletonPieceRequirements as $req) { $structure['pieces'][] = [ @@ -373,11 +373,34 @@ class ModelType private function getPieceStructureFromRelations(): array { return [ - 'customFields' => [], + 'customFields' => $this->serializeCustomFields($this->pieceCustomFields), 'products' => array_map(fn (SkeletonProductRequirement $req) => [ 'typeProductId' => $req->getTypeProduct()->getId(), 'familyCode' => $req->getFamilyCode(), ], $this->skeletonProductRequirements->toArray()), ]; } + + /** + * @param Collection $fields + */ + private function serializeCustomFields(Collection $fields): array + { + $items = []; + foreach ($fields as $cf) { + $items[] = [ + 'id' => $cf->getId(), + 'name' => $cf->getName(), + 'type' => $cf->getType(), + 'required' => $cf->isRequired(), + 'options' => $cf->getOptions(), + 'defaultValue' => $cf->getDefaultValue(), + 'orderIndex' => $cf->getOrderIndex(), + ]; + } + + usort($items, static fn (array $a, array $b) => $a['orderIndex'] <=> $b['orderIndex']); + + return $items; + } }