feat(modeles): exposer la structure selon la categorie

This commit is contained in:
2026-01-15 13:42:18 +01:00
parent 3c5fb83673
commit 2c3fbb093a

View File

@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace App\Entity; namespace App\Entity;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\ApiResource;
use App\Enum\ModelCategory; use App\Enum\ModelCategory;
use App\Repository\ModelTypeRepository; use App\Repository\ModelTypeRepository;
@@ -17,49 +19,57 @@ use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Table(name: 'model_types')] #[ORM\Table(name: 'model_types')]
#[ORM\UniqueConstraint(name: 'unique_category_name', columns: ['category', 'name'])] #[ORM\UniqueConstraint(name: 'unique_category_name', columns: ['category', 'name'])]
#[ORM\HasLifecycleCallbacks] #[ORM\HasLifecycleCallbacks]
#[ApiFilter(SearchFilter::class, properties: ['category' => 'exact'])]
#[ApiResource] #[ApiResource]
class ModelType class ModelType
{ {
#[ORM\Id] #[ORM\Id]
#[ORM\Column(type: Types::STRING, length: 36)] #[ORM\Column(type: Types::STRING, length: 36)]
#[Groups(['type_machine:read'])] #[Groups(['type_machine:read', 'model_type:read'])]
private ?string $id = null; private ?string $id = null;
#[ORM\Column(type: Types::STRING, length: 120)] #[ORM\Column(type: Types::STRING, length: 120)]
#[Groups(['type_machine:read'])] #[Groups(['type_machine:read', 'model_type:read', 'model_type:write'])]
private string $name; private string $name;
#[ORM\Column(type: Types::STRING, length: 60, unique: true)] #[ORM\Column(type: Types::STRING, length: 60, unique: true)]
#[Groups(['type_machine:read'])] #[Groups(['type_machine:read', 'model_type:read', 'model_type:write'])]
private string $code; private string $code;
#[ORM\Column(enumType: ModelCategory::class)] #[ORM\Column(enumType: ModelCategory::class)]
#[Groups(['type_machine:read'])] #[Groups(['type_machine:read', 'model_type:read', 'model_type:write'])]
private ModelCategory $category; private ModelCategory $category;
#[ORM\Column(type: Types::TEXT, nullable: true)] #[ORM\Column(type: Types::TEXT, nullable: true)]
#[Groups(['type_machine:read'])] #[Groups(['type_machine:read', 'model_type:read', 'model_type:write'])]
private ?string $notes = null; private ?string $notes = null;
#[ORM\Column(type: Types::TEXT, nullable: true)] #[ORM\Column(type: Types::TEXT, nullable: true)]
#[Groups(['type_machine:read'])] #[Groups(['type_machine:read', 'model_type:read', 'model_type:write'])]
private ?string $description = null; private ?string $description = null;
#[ORM\Column(type: Types::JSON, nullable: true, name: 'componentSkeleton')] #[ORM\Column(type: Types::JSON, nullable: true, name: 'componentSkeleton')]
#[Groups(['model_type:read'])]
private ?array $componentSkeleton = null; private ?array $componentSkeleton = null;
#[ORM\Column(type: Types::JSON, nullable: true, name: 'pieceSkeleton')] #[ORM\Column(type: Types::JSON, nullable: true, name: 'pieceSkeleton')]
#[Groups(['model_type:read'])]
private ?array $pieceSkeleton = null; private ?array $pieceSkeleton = null;
#[ORM\Column(type: Types::JSON, nullable: true, name: 'productSkeleton')] #[ORM\Column(type: Types::JSON, nullable: true, name: 'productSkeleton')]
#[Groups(['model_type:read'])]
private ?array $productSkeleton = null; private ?array $productSkeleton = null;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'createdAt')] #[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'createdAt')]
#[Groups(['model_type:read'])]
private \DateTimeImmutable $createdAt; private \DateTimeImmutable $createdAt;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'updatedAt')] #[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'updatedAt')]
#[Groups(['model_type:read'])]
private \DateTimeImmutable $updatedAt; private \DateTimeImmutable $updatedAt;
private ?array $pendingStructure = null;
/** /**
* @var Collection<int, Composant> * @var Collection<int, Composant>
*/ */
@@ -195,6 +205,11 @@ class ModelType
{ {
$this->category = $category; $this->category = $category;
if ($this->pendingStructure !== null) {
$this->applyStructureForCategory($this->pendingStructure, $category);
$this->pendingStructure = null;
}
return $this; return $this;
} }
@@ -258,6 +273,50 @@ class ModelType
return $this; return $this;
} }
#[Groups(['model_type:read'])]
public function getStructure(): ?array
{
return match ($this->category) {
ModelCategory::COMPONENT => $this->componentSkeleton,
ModelCategory::PIECE => $this->pieceSkeleton,
ModelCategory::PRODUCT => $this->productSkeleton,
};
}
#[Groups(['model_type:write'])]
public function setStructure(?array $structure): static
{
if (!isset($this->category)) {
$this->pendingStructure = $structure;
return $this;
}
$this->applyStructureForCategory($structure, $this->category);
return $this;
}
private function applyStructureForCategory(?array $structure, ModelCategory $category): void
{
if ($category === ModelCategory::COMPONENT) {
$this->componentSkeleton = $structure;
$this->pieceSkeleton = null;
$this->productSkeleton = null;
return;
}
if ($category === ModelCategory::PIECE) {
$this->pieceSkeleton = $structure;
$this->componentSkeleton = null;
$this->productSkeleton = null;
return;
}
$this->productSkeleton = $structure;
$this->componentSkeleton = null;
$this->pieceSkeleton = null;
}
public function getCreatedAt(): \DateTimeImmutable public function getCreatedAt(): \DateTimeImmutable
{ {
return $this->createdAt; return $this->createdAt;