src/Entity/ContactEmail.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ContactEmailRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. #[ORM\Entity(repositoryClassContactEmailRepository::class)]
  8. class ContactEmail implements ContactInterface
  9. {
  10.     #[ORM\Id]
  11.     #[ORM\GeneratedValue]
  12.     #[ORM\Column(type'integer')]
  13.     private $id;
  14.     #[ORM\Column(type'string'length100)]
  15.     private $email;
  16.     #[ORM\ManyToOne(targetEntityPerson::class, cascade: ['persist'], inversedBy'ownedEmails')]
  17.     #[ORM\JoinColumn(nullablefalse)]
  18.     private $owner;
  19.     #[ORM\OneToMany(mappedBy'firstEmail'targetEntityPerson::class)]
  20.     private $firstEmailPersons;
  21.     #[ORM\OneToMany(mappedBy'secondEmail'targetEntityPerson::class)]
  22.     private $secondEmailPersons;
  23.     public function __construct()
  24.     {
  25.         $this->firstEmailPersons = new ArrayCollection();
  26.         $this->secondEmailPersons = new ArrayCollection();
  27.     }
  28.     public function __toString(): string
  29.     {
  30.         return $this->getEmail() ?? '';
  31.     }
  32.     public function getId(): ?int
  33.     {
  34.         return $this->id;
  35.     }
  36.     public function getEmail(): ?string
  37.     {
  38.         return $this->email;
  39.     }
  40.     public function setEmail(string $email): self
  41.     {
  42.         $this->email $email;
  43.         return $this;
  44.     }
  45.     public function getOwner(): ?Person
  46.     {
  47.         return $this->owner;
  48.     }
  49.     public function setOwner(Person $owner): self
  50.     {
  51.         $this->owner $owner;
  52.         return $this;
  53.     }
  54.     /**
  55.      * @return Collection<int, Person>
  56.      */
  57.     public function getFirstEmailPersons(): Collection
  58.     {
  59.         return $this->firstEmailPersons;
  60.     }
  61.     public function addFirstEmailPerson(Person $firstEmailPerson): self
  62.     {
  63.         if (!$this->firstEmailPersons->contains($firstEmailPerson)) {
  64.             $this->firstEmailPersons[] = $firstEmailPerson;
  65.             $firstEmailPerson->setFirstEmail($this);
  66.         }
  67.         return $this;
  68.     }
  69.     public function removeFirstEmailPerson(Person $firstEmailPerson): self
  70.     {
  71.         if ($this->firstEmailPersons->removeElement($firstEmailPerson)) {
  72.             // set the owning side to null (unless already changed)
  73.             if ($firstEmailPerson->getFirstEmail() === $this) {
  74.                 $firstEmailPerson->setFirstEmail(null);
  75.             }
  76.         }
  77.         return $this;
  78.     }
  79.     /**
  80.      * @return Collection<int, Person>
  81.      */
  82.     public function getSecondEmailPersons(): Collection
  83.     {
  84.         return $this->secondEmailPersons;
  85.     }
  86.     public function addSecondEmailPerson(Person $secondEmailPerson): self
  87.     {
  88.         if (!$this->secondEmailPersons->contains($secondEmailPerson)) {
  89.             $this->secondEmailPersons[] = $secondEmailPerson;
  90.             $secondEmailPerson->setSecondEmail($this);
  91.         }
  92.         return $this;
  93.     }
  94.     public function removeSecondEmailPerson(Person $secondEmailPerson): self
  95.     {
  96.         if ($this->secondEmailPersons->removeElement($secondEmailPerson)) {
  97.             // set the owning side to null (unless already changed)
  98.             if ($secondEmailPerson->getSecondEmail() === $this) {
  99.                 $secondEmailPerson->setSecondEmail(null);
  100.             }
  101.         }
  102.         return $this;
  103.     }
  104.     public function getFirstPerson(): ?Person
  105.     {
  106.         $person $this->getFirstEmailPersons()->first();
  107.         if (false === $person) {
  108.             $person $this->getSecondEmailPersons()->first();
  109.         }
  110.         return $person === false null $person;
  111.     }
  112.     /**
  113.      * @return Person[]
  114.      */
  115.     public function getPersons(): array
  116.     {
  117.         return array_merge(
  118.             $this->getFirstEmailPersons()->toArray(),
  119.             $this->getSecondEmailPersons()->toArray()
  120.         );
  121.     }
  122. }