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();
$structure = $composant->getStructure();
$piece = $link->getPiece();
if (!is_array($structure) || !isset($structure['pieces']) || !is_array($structure['pieces'])) {
return 1;
}
$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);
foreach ($composant->getPieceSlots() as $slot) {
if ($slot->getSelectedPiece()?->getId() === $piece->getId()) {
return $slot->getQuantity();
}
}
return 1;
return $link->getQuantity();
}
private function normalizeProductLinks(array $links): array
@@ -709,7 +698,7 @@ class MachineStructureController extends AbstractController
'typeComposant' => $this->normalizeModelType($type),
'productId' => $composant->getProduct()?->getId(),
'product' => $composant->getProduct() ? $this->normalizeProduct($composant->getProduct()) : null,
'structure' => $this->enrichStructureWithPieceData($composant->getStructure()),
'structure' => $this->buildStructureFromSlots($composant),
'constructeurs' => $this->normalizeConstructeurs($composant->getConstructeurs()),
'documents' => [],
'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'])) {
return $structure;
$pieces = [];
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) {
if (!is_array($entry)) {
continue;
}
$selectedId = $entry['selectedPieceId'] ?? null;
if ($selectedId) {
$piece = $this->pieceRepository->find($selectedId);
if ($piece instanceof Piece) {
$entry['resolvedPiece'] = $this->normalizePiece($piece);
}
}
$subcomponents = [];
foreach ($composant->getSubcomponentSlots() as $slot) {
$subcomponents[] = [
'alias' => $slot->getAlias(),
'familyCode' => $slot->getFamilyCode(),
'typeComposantId' => $slot->getTypeComposant()?->getId(),
'selectedComponentId' => $slot->getSelectedComposant()?->getId(),
];
}
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

View File

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