src/Entity/ContactPhone.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ContactPhoneRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. #[ORM\Entity(repositoryClassContactPhoneRepository::class)]
  8. class ContactPhone implements ContactInterface
  9. {
  10.     #[ORM\Id]
  11.     #[ORM\GeneratedValue]
  12.     #[ORM\Column(type'integer')]
  13.     private $id;
  14.     #[ORM\Column(type'string'length20)]
  15.     private $number;
  16.     #[ORM\Column(type'string'length5nullabletrue)]
  17.     private $code;
  18.     #[ORM\ManyToOne(targetEntityPerson::class, cascade: ['persist'], inversedBy'ownedPhones')]
  19.     #[ORM\JoinColumn(nullablefalse)]
  20.     private $owner;
  21.     #[ORM\OneToMany(mappedBy'firstPhone'targetEntityPerson::class)]
  22.     private $firstPhonePersons;
  23.     #[ORM\OneToMany(mappedBy'secondPhone'targetEntityPerson::class)]
  24.     private $secondPhonePersons;
  25.     public function __construct()
  26.     {
  27.         $this->firstPhonePersons = new ArrayCollection();
  28.         $this->secondPhonePersons = new ArrayCollection();
  29.     }
  30.     public function __toString(): string
  31.     {
  32.         $str '';
  33.         if ($this->code) {
  34.             $str .= $this->getCode().' ';
  35.         }
  36.         $str .= $this->getNumber();
  37.         return $str;
  38.     }
  39.     public function getId(): ?int
  40.     {
  41.         return $this->id;
  42.     }
  43.     public function getNumber(): ?string
  44.     {
  45.         return $this->number;
  46.     }
  47.     public function setNumber(string $number): self
  48.     {
  49.         $this->number $number;
  50.         return $this;
  51.     }
  52.     public function getCode(): ?string
  53.     {
  54.         return $this->code;
  55.     }
  56.     public function setCode(?string $code): self
  57.     {
  58.         $this->code $code;
  59.         return $this;
  60.     }
  61.     public function getOwner(): ?Person
  62.     {
  63.         return $this->owner;
  64.     }
  65.     public function setOwner(Person $owner): self
  66.     {
  67.         $this->owner $owner;
  68.         return $this;
  69.     }
  70.     /**
  71.      * @return Collection<int, Person>
  72.      */
  73.     public function getFirstPhonePersons(): Collection
  74.     {
  75.         return $this->firstPhonePersons;
  76.     }
  77.     public function addFirstPhonePerson(Person $firstPhonePerson): self
  78.     {
  79.         if (!$this->firstPhonePersons->contains($firstPhonePerson)) {
  80.             $this->firstPhonePersons[] = $firstPhonePerson;
  81.             $firstPhonePerson->setFirstPhone($this);
  82.         }
  83.         return $this;
  84.     }
  85.     public function removeFirstPhonePerson(Person $firstPhonePerson): self
  86.     {
  87.         if ($this->firstPhonePersons->removeElement($firstPhonePerson)) {
  88.             // set the owning side to null (unless already changed)
  89.             if ($firstPhonePerson->getFirstPhone() === $this) {
  90.                 $firstPhonePerson->setFirstPhone(null);
  91.             }
  92.         }
  93.         return $this;
  94.     }
  95.     /**
  96.      * @return Collection<int, Person>
  97.      */
  98.     public function getSecondPhonePersons(): Collection
  99.     {
  100.         return $this->secondPhonePersons;
  101.     }
  102.     public function addSecondPhonePerson(Person $secondPhonePerson): self
  103.     {
  104.         if (!$this->secondPhonePersons->contains($secondPhonePerson)) {
  105.             $this->secondPhonePersons[] = $secondPhonePerson;
  106.             $secondPhonePerson->setSecondPhone($this);
  107.         }
  108.         return $this;
  109.     }
  110.     public function removeSecondPhonePerson(Person $secondPhonePerson): self
  111.     {
  112.         if ($this->secondPhonePersons->removeElement($secondPhonePerson)) {
  113.             // set the owning side to null (unless already changed)
  114.             if ($secondPhonePerson->getSecondPhone() === $this) {
  115.                 $secondPhonePerson->setSecondPhone(null);
  116.             }
  117.         }
  118.         return $this;
  119.     }
  120.     public function getFirstPerson(): ?Person
  121.     {
  122.         $person $this->getFirstPhonePersons()->first();
  123.         if (false === $person) {
  124.             $person $this->getSecondPhonePersons()->first();
  125.         }
  126.         return $person === false null $person;
  127.     }
  128.     /**
  129.      * @return Person[]
  130.      */
  131.     public function getPersons(): array
  132.     {
  133.         return array_merge(
  134.             $this->getFirstPhonePersons()->toArray(),
  135.             $this->getSecondPhonePersons()->toArray()
  136.         );
  137.     }
  138. }