src/Entity/CRM/MailingEntry.php line 19

  1. <?php
  2. namespace App\Entity\CRM;
  3. use App\Repository\MailingEntryRepository;
  4. use DateTime;
  5. use DateTimeImmutable;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. #[ORM\Entity(repositoryClassMailingEntryRepository::class)]
  11. #[ORM\Table(name'mailing_entry')]
  12. #[ORM\UniqueConstraint(name'uniq_email_group'columns: ['email''group_name'])]
  13. #[ORM\Index(name'idx_group_name'columns: ['group_name'])]
  14. #[ORM\HasLifecycleCallbacks]
  15. #[UniqueEntity(fields: ['email''groupName'], message'Ten email w tej grupie już istnieje.')]
  16. class MailingEntry
  17. {
  18.     #[ORM\Id]
  19.     #[ORM\GeneratedValue]
  20.     #[ORM\Column]
  21.     private ?int $id null;
  22.     #[Assert\NotBlank]
  23.     #[Assert\Length(max320)]
  24.     #[Assert\Email(modeAssert\Email::VALIDATION_MODE_STRICT)]
  25.     #[Assert\Regex(pattern'/^[^\s;]+$/u'message'Email nie może zawierać spacji ani średników')]
  26.     #[ORM\Column(length320)]
  27.     private string $email;
  28.     #[Assert\NotBlank]
  29.     #[Assert\Length(max100)]
  30.     #[Assert\Regex(pattern'/^[^;]+$/u'message'Nazwa grupy nie może zawierać średników')]
  31.     #[ORM\Column(name'group_name'length100)]
  32.     private string $groupName;
  33.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  34.     private DateTime $createdAt;
  35.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  36.     private DateTime $updatedAt;
  37.     #[ORM\Column]
  38.     private ?bool $isActive null;
  39.     public function __construct()
  40.     {
  41.         $this->createdAt = new DateTime();
  42.         $this->isActive true;
  43.     }
  44.     public function getId(): ?int
  45.     {
  46.         return $this->id;
  47.     }
  48.     public function getEmail(): string
  49.     {
  50.         return $this->email;
  51.     }
  52.     public function setEmail(string $email): self
  53.     {
  54.         $email trim($email);
  55.         $this->email mb_strtolower($email);
  56.         return $this;
  57.     }
  58.     public function getGroupName(): string
  59.     {
  60.         return $this->groupName;
  61.     }
  62.     public function setGroupName(string $groupName): self
  63.     {
  64.         $this->groupName trim($groupName);
  65.         return $this;
  66.     }
  67.     public function getCreatedAt(): DateTime
  68.     {
  69.         return $this->createdAt;
  70.     }
  71.     public function getUpdatedAt(): DateTime
  72.     {
  73.         return $this->updatedAt;
  74.     }
  75.     #[ORM\PrePersist]
  76.     public function onPrePersist(): void
  77.     {
  78.         $now = new DateTime('now');
  79.         $this->createdAt $now;
  80.         $this->updatedAt $now;
  81.         // normalize to ensure constraints hold
  82.         $this->email mb_strtolower(trim($this->email));
  83.         $this->groupName trim($this->groupName);
  84.     }
  85.     #[ORM\PreUpdate]
  86.     public function onPreUpdate(): void
  87.     {
  88.         $this->updatedAt = new DateTime('now');
  89.         $this->email mb_strtolower(trim($this->email));
  90.         $this->groupName trim($this->groupName);
  91.     }
  92.     public function isIsActive(): ?bool
  93.     {
  94.         return $this->isActive;
  95.     }
  96.     public function setIsActive(bool $isActive): static
  97.     {
  98.         $this->isActive $isActive;
  99.         return $this;
  100.     }
  101. }