feat(structure) : read composant structure from slot relations instead of JSON

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Matthieu
2026-03-12 18:23:56 +01:00
parent 5194543d16
commit 8ed5f90b63
2 changed files with 48 additions and 48 deletions

View File

@@ -656,26 +656,15 @@ class MachineStructureController extends AbstractController
} }
$composant = $parentLink->getComposant(); $composant = $parentLink->getComposant();
$structure = $composant->getStructure(); $piece = $link->getPiece();
if (!is_array($structure) || !isset($structure['pieces']) || !is_array($structure['pieces'])) { foreach ($composant->getPieceSlots() as $slot) {
return 1; if ($slot->getSelectedPiece()?->getId() === $piece->getId()) {
} return $slot->getQuantity();
$piece = $link->getPiece();
$typePiece = $piece->getTypePiece();
$typePieceId = $typePiece?->getId();
foreach ($structure['pieces'] as $pieceDef) {
if (!is_array($pieceDef)) {
continue;
}
if (isset($pieceDef['typePieceId']) && $pieceDef['typePieceId'] === $typePieceId) {
return (int) ($pieceDef['quantity'] ?? 1);
} }
} }
return 1; return $link->getQuantity();
} }
private function normalizeProductLinks(array $links): array private function normalizeProductLinks(array $links): array
@@ -709,7 +698,7 @@ class MachineStructureController extends AbstractController
'typeComposant' => $this->normalizeModelType($type), 'typeComposant' => $this->normalizeModelType($type),
'productId' => $composant->getProduct()?->getId(), 'productId' => $composant->getProduct()?->getId(),
'product' => $composant->getProduct() ? $this->normalizeProduct($composant->getProduct()) : null, 'product' => $composant->getProduct() ? $this->normalizeProduct($composant->getProduct()) : null,
'structure' => $this->enrichStructureWithPieceData($composant->getStructure()), 'structure' => $this->buildStructureFromSlots($composant),
'constructeurs' => $this->normalizeConstructeurs($composant->getConstructeurs()), 'constructeurs' => $this->normalizeConstructeurs($composant->getConstructeurs()),
'documents' => [], 'documents' => [],
'customFields' => $type ? $this->normalizeCustomFieldDefinitions($type->getComponentCustomFields()) : [], 'customFields' => $type ? $this->normalizeCustomFieldDefinitions($type->getComponentCustomFields()) : [],
@@ -717,26 +706,45 @@ class MachineStructureController extends AbstractController
]; ];
} }
private function enrichStructureWithPieceData(?array $structure): ?array private function buildStructureFromSlots(Composant $composant): array
{ {
if (!is_array($structure) || !isset($structure['pieces']) || !is_array($structure['pieces'])) { $pieces = [];
return $structure; foreach ($composant->getPieceSlots() as $slot) {
$pieceData = [
'typePieceId' => $slot->getTypePiece()?->getId(),
'quantity' => $slot->getQuantity(),
'selectedPieceId' => $slot->getSelectedPiece()?->getId(),
];
if ($slot->getSelectedPiece()) {
$pieceData['resolvedPiece'] = $this->normalizePiece($slot->getSelectedPiece());
}
$pieces[] = $pieceData;
} }
foreach ($structure['pieces'] as &$entry) { $subcomponents = [];
if (!is_array($entry)) { foreach ($composant->getSubcomponentSlots() as $slot) {
continue; $subcomponents[] = [
} 'alias' => $slot->getAlias(),
$selectedId = $entry['selectedPieceId'] ?? null; 'familyCode' => $slot->getFamilyCode(),
if ($selectedId) { 'typeComposantId' => $slot->getTypeComposant()?->getId(),
$piece = $this->pieceRepository->find($selectedId); 'selectedComponentId' => $slot->getSelectedComposant()?->getId(),
if ($piece instanceof Piece) { ];
$entry['resolvedPiece'] = $this->normalizePiece($piece);
}
}
} }
return $structure; $products = [];
foreach ($composant->getProductSlots() as $slot) {
$products[] = [
'typeProductId' => $slot->getTypeProduct()?->getId(),
'familyCode' => $slot->getFamilyCode(),
'selectedProductId' => $slot->getSelectedProduct()?->getId(),
];
}
return [
'pieces' => $pieces,
'subcomponents' => $subcomponents,
'products' => $products,
];
} }
private function normalizePiece(Piece $piece): array private function normalizePiece(Piece $piece): array

View File

@@ -212,9 +212,13 @@ class Piece
{ {
$this->product = $product; $this->product = $product;
if ($product && empty($this->productIds)) { if (null !== $product) {
$productId = $product->getId(); $this->addProduct($product);
$this->productIds = $productId ? [$productId] : null;
if (empty($this->productIds)) {
$productId = $product->getId();
$this->productIds = $productId ? [$productId] : null;
}
} }
if (!$product && empty($this->productIds)) { if (!$product && empty($this->productIds)) {
@@ -229,19 +233,7 @@ class Piece
*/ */
public function getProductIds(): array public function getProductIds(): array
{ {
if (!is_array($this->productIds)) { return $this->products->map(fn (Product $p) => $p->getId())->toArray();
return [];
}
return array_values(
array_filter(
array_map(
static fn ($value) => is_string($value) ? trim($value) : '',
$this->productIds,
),
static fn (string $value) => '' !== $value,
),
);
} }
public function setProductIds(?array $productIds): static public function setProductIds(?array $productIds): static