<?php
namespace App\Entity;
use App\Repository\ContactPhoneRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ContactPhoneRepository::class)]
class ContactPhone implements ContactInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 20)]
private $number;
#[ORM\Column(type: 'string', length: 5, nullable: true)]
private $code;
#[ORM\ManyToOne(targetEntity: Person::class, cascade: ['persist'], inversedBy: 'ownedPhones')]
#[ORM\JoinColumn(nullable: false)]
private $owner;
#[ORM\OneToMany(mappedBy: 'firstPhone', targetEntity: Person::class)]
private $firstPhonePersons;
#[ORM\OneToMany(mappedBy: 'secondPhone', targetEntity: Person::class)]
private $secondPhonePersons;
public function __construct()
{
$this->firstPhonePersons = new ArrayCollection();
$this->secondPhonePersons = new ArrayCollection();
}
public function __toString(): string
{
$str = '';
if ($this->code) {
$str .= $this->getCode().' ';
}
$str .= $this->getNumber();
return $str;
}
public function getId(): ?int
{
return $this->id;
}
public function getNumber(): ?string
{
return $this->number;
}
public function setNumber(string $number): self
{
$this->number = $number;
return $this;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(?string $code): self
{
$this->code = $code;
return $this;
}
public function getOwner(): ?Person
{
return $this->owner;
}
public function setOwner(Person $owner): self
{
$this->owner = $owner;
return $this;
}
/**
* @return Collection<int, Person>
*/
public function getFirstPhonePersons(): Collection
{
return $this->firstPhonePersons;
}
public function addFirstPhonePerson(Person $firstPhonePerson): self
{
if (!$this->firstPhonePersons->contains($firstPhonePerson)) {
$this->firstPhonePersons[] = $firstPhonePerson;
$firstPhonePerson->setFirstPhone($this);
}
return $this;
}
public function removeFirstPhonePerson(Person $firstPhonePerson): self
{
if ($this->firstPhonePersons->removeElement($firstPhonePerson)) {
// set the owning side to null (unless already changed)
if ($firstPhonePerson->getFirstPhone() === $this) {
$firstPhonePerson->setFirstPhone(null);
}
}
return $this;
}
/**
* @return Collection<int, Person>
*/
public function getSecondPhonePersons(): Collection
{
return $this->secondPhonePersons;
}
public function addSecondPhonePerson(Person $secondPhonePerson): self
{
if (!$this->secondPhonePersons->contains($secondPhonePerson)) {
$this->secondPhonePersons[] = $secondPhonePerson;
$secondPhonePerson->setSecondPhone($this);
}
return $this;
}
public function removeSecondPhonePerson(Person $secondPhonePerson): self
{
if ($this->secondPhonePersons->removeElement($secondPhonePerson)) {
// set the owning side to null (unless already changed)
if ($secondPhonePerson->getSecondPhone() === $this) {
$secondPhonePerson->setSecondPhone(null);
}
}
return $this;
}
public function getFirstPerson(): ?Person
{
$person = $this->getFirstPhonePersons()->first();
if (false === $person) {
$person = $this->getSecondPhonePersons()->first();
}
return $person === false ? null : $person;
}
/**
* @return Person[]
*/
public function getPersons(): array
{
return array_merge(
$this->getFirstPhonePersons()->toArray(),
$this->getSecondPhonePersons()->toArray()
);
}
}