src/Entity/WebsiteSetting.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\WebsiteSettingRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. /**
  6.  * @ORM\Entity(repositoryClass=WebsiteSettingRepository::class)
  7.  * @ORM\Table(name="website_setting")
  8.  * @ORM\HasLifecycleCallbacks
  9.  */
  10. class WebsiteSetting
  11. {
  12.     /**
  13.      * @ORM\Id
  14.      * @ORM\GeneratedValue
  15.      * @ORM\Column(type="integer")
  16.      */
  17.     private ?int $id null;
  18.     /**
  19.      * Clé fonctionnelle IMMUTABLE (ex: site_maintenance, homepage_mode)
  20.      *
  21.      * @ORM\Column(name="setting_key", type="string", length=100, unique=true)
  22.      */
  23.     private string $key;
  24.     /**
  25.      * Type logique de la valeur
  26.      * Valeurs autorisées :
  27.      * string | int | float | bool | json | text
  28.      *
  29.      * @ORM\Column(type="string", length=20)
  30.      */
  31.     private string $type;
  32.     /** @ORM\Column(type="string", length=255, nullable=true) */
  33.     private ?string $valueString null;
  34.     /** @ORM\Column(type="integer", nullable=true) */
  35.     private ?int $valueInt null;
  36.     /** @ORM\Column(type="float", nullable=true) */
  37.     private ?float $valueFloat null;
  38.     /** @ORM\Column(type="boolean", nullable=true) */
  39.     private ?bool $valueBool null;
  40.     /** @ORM\Column(type="json", nullable=true) */
  41.     private ?array $valueJson null;
  42.     /** @ORM\Column(type="text", nullable=true) */
  43.     private ?string $valueText null;
  44.     /** @ORM\Column(type="string", length=255, nullable=true) */
  45.     private ?string $description null;
  46.     /**
  47.      * @ORM\Column(type="datetime_immutable")
  48.      */
  49.     private \DateTimeImmutable $updatedAt;
  50.     public function __construct()
  51.     {
  52.         $this->updatedAt = new \DateTimeImmutable();
  53.     }
  54.     /**
  55.      * Sectione fonctionnel (website, seo, social, system, etc.)
  56.      * @ORM\Column(type="string", length=50, nullable=true)
  57.      */
  58.     private ?string $section 'website';
  59.     /* ======================================================
  60.      * GETTERS / SETTERS DE BASE
  61.      * ====================================================== */
  62.     public function getId(): ?int
  63.     {
  64.         return $this->id;
  65.     }
  66.     public function getKey(): string
  67.     {
  68.         return $this->key;
  69.     }
  70.     /**
  71.      * À n'utiliser qu'à la création
  72.      * La clé NE DOIT PAS changer ensuite
  73.      */
  74.     public function setKey(string $key): self
  75.     {
  76.         $this->key $key;
  77.         return $this;
  78.     }
  79.     public function getType(): string
  80.     {
  81.         return $this->type;
  82.     }
  83.     /**
  84.      * Le type est figé après création
  85.      */
  86.     public function setType(string $type): self
  87.     {
  88.         $allowed = ['string','int','float','bool','json','text'];
  89.         if (!in_array($type$allowedtrue)) {
  90.             throw new \InvalidArgumentException('Type de setting invalide');
  91.         }
  92.         $this->type $type;
  93.         return $this;
  94.     }
  95.     public function getDescription(): ?string
  96.     {
  97.         return $this->description;
  98.     }
  99.     public function setDescription(?string $description): self
  100.     {
  101.         $this->description $description;
  102.         return $this;
  103.     }
  104.     public function getUpdatedAt(): \DateTimeImmutable
  105.     {
  106.         return $this->updatedAt;
  107.     }
  108.     /**
  109.      * @ORM\PrePersist
  110.      * @ORM\PreUpdate
  111.      */
  112.     public function updateTimestamp(): void
  113.     {
  114.         $this->updatedAt = new \DateTimeImmutable();
  115.     }
  116.     private function touch(): void
  117.     {
  118.         $this->updatedAt = new \DateTimeImmutable();
  119.     }
  120.     public function getSection(): string
  121.     {
  122.         return $this->section ?? 'website';
  123.     }
  124.     public function setSection(string $section): self
  125.     {
  126.         $this->section $section;
  127.         return $this;
  128.     }
  129.     /* ======================================================
  130.      * ACCÈS VALEUR TYPÉE (API PUBLIQUE)
  131.      * ====================================================== */
  132.     /**
  133.      * Valeur lisible par le métier / Twig / services
  134.      */
  135.     public function getTypedValue()
  136.     {
  137.         return match ($this->type) {
  138.             'string' => $this->valueString,
  139.             'int'    => $this->valueInt,
  140.             'float'  => $this->valueFloat,
  141.             'bool'   => $this->valueBool,
  142.             'json'   => $this->valueJson,
  143.             'text'   => $this->valueText,
  144.             default  => null,
  145.         };
  146.     }
  147.     /**
  148.      * Setter centralisé — nettoie les autres colonnes automatiquement
  149.      */
  150.     public function setTypedValue($value): self
  151.     {
  152.         // Reset strict pour éviter toute incohérence
  153.         $this->valueString null;
  154.         $this->valueInt    null;
  155.         $this->valueFloat  null;
  156.         $this->valueBool   null;
  157.         $this->valueJson   null;
  158.         $this->valueText   null;
  159.         switch ($this->type) {
  160.             case 'int':
  161.                 $this->valueInt $value !== null ? (int) $value null;
  162.                 break;
  163.             case 'float':
  164.                 $this->valueFloat $value !== null ? (float) $value null;
  165.                 break;
  166.             case 'bool':
  167.                 $this->valueBool = (bool) $value;
  168.                 break;
  169.             case 'json':
  170.                 $this->valueJson is_array($value) ? $value : [];
  171.                 break;
  172.             case 'text':
  173.                 $this->valueText $value !== null ? (string) $value null;
  174.                 break;
  175.             default: // string
  176.                 $this->valueString $value !== null ? (string) $value null;
  177.         }
  178.         $this->touch();
  179.         return $this;
  180.     }
  181.     /* ======================================================
  182.      * HELPERS OPTIONNELS
  183.      * ====================================================== */
  184.     public function isBoolean(): bool
  185.     {
  186.         return $this->type === 'bool';
  187.     }
  188.     public function isJson(): bool
  189.     {
  190.         return $this->type === 'json';
  191.     }
  192.     public function isNumeric(): bool
  193.     {
  194.         return in_array($this->type, ['int''float'], true);
  195.     }
  196.     public function getValue()
  197.     {
  198.         return $this->getTypedValue();
  199.     }
  200.     public function setValue($value): self
  201.     {
  202.         switch ($this->type) {
  203.             case 'string':
  204.                 $this->valueString $value !== null ? (string) $value null;
  205.                 break;
  206.             case 'int':
  207.                 $this->valueInt $value !== null ? (int) $value null;
  208.                 break;
  209.             case 'float':
  210.                 $this->valueFloat $value !== null ? (float) $value null;
  211.                 break;
  212.             case 'bool':
  213.                 $this->valueBool = (bool) $value;
  214.                 break;
  215.             case 'json':
  216.                 $this->valueJson $value !== null ? (array) $value null;
  217.                 break;
  218.             case 'text':
  219.                 $this->valueText $value !== null ? (string) $value null;
  220.                 break;
  221.         }
  222.         $this->updatedAt = new \DateTimeImmutable();
  223.         return $this;
  224.     }
  225. }