<?php
namespace App\Entity;
use App\Repository\ParametreRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ParametreRepository::class)]
#[ORM\Table(name: 'parametre')]
#[ORM\Index(name: 'idx_parametre_cle', columns: ['cle'])]
#[ORM\Index(name: 'idx_parametre_categorie', columns: ['categorie'])]
class Parametre
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 100, unique: true)]
private ?string $cle = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $valeur = null;
#[ORM\Column(length: 50)]
private ?string $categorie = 'general'; // general, societe, contact, facturation, email, etc.
#[ORM\Column(length: 255, nullable: true)]
private ?string $label = null; // Libellé pour l'affichage
#[ORM\Column(length: 500, nullable: true)]
private ?string $description = null; // Description du paramètre
#[ORM\Column(length: 50)]
private ?string $type = 'text'; // text, textarea, number, email, tel, url, file, boolean, select
#[ORM\Column(type: Types::JSON, nullable: true)]
private ?array $options = null; // Options pour select, validation, etc.
#[ORM\Column]
private ?bool $estModifiable = true; // Peut être modifié par l'utilisateur
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $updatedAt = null;
public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
$this->estModifiable = true;
$this->categorie = 'general';
$this->type = 'text';
}
public function getId(): ?int
{
return $this->id;
}
public function getCle(): ?string
{
return $this->cle;
}
public function setCle(string $cle): static
{
$this->cle = $cle;
return $this;
}
public function getValeur(): ?string
{
return $this->valeur;
}
public function setValeur(?string $valeur): static
{
$this->valeur = $valeur;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
public function getCategorie(): ?string
{
return $this->categorie;
}
public function setCategorie(string $categorie): static
{
$this->categorie = $categorie;
return $this;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(?string $label): static
{
$this->label = $label;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): static
{
$this->description = $description;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): static
{
$this->type = $type;
return $this;
}
public function getOptions(): ?array
{
return $this->options;
}
public function setOptions(?array $options): static
{
$this->options = $options;
return $this;
}
public function isEstModifiable(): ?bool
{
return $this->estModifiable;
}
public function setEstModifiable(bool $estModifiable): static
{
$this->estModifiable = $estModifiable;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeImmutable $updatedAt): static
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Retourne la valeur convertie selon le type
*/
public function getValeurTypee()
{
if ($this->valeur === null) {
return null;
}
return match($this->type) {
'number' => (float) $this->valeur,
'boolean' => filter_var($this->valeur, FILTER_VALIDATE_BOOLEAN),
'json' => json_decode($this->valeur, true),
default => $this->valeur
};
}
public function __toString(): string
{
return $this->label ?? $this->cle ?? '';
}
}