src/Entity/Person.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\ORM\Mapping\JoinColumn;
  7. use Doctrine\ORM\Mapping\ManyToOne;
  8. use JMS\Serializer\Annotation\Groups;
  9. /**
  10.  * @ORM\Table("Person")
  11.  *
  12.  * @ORM\HasLifecycleCallbacks()
  13.  *
  14.  * @ORM\Entity(repositoryClass="App\Repository\PersonRepository")
  15.  */
  16. class Person
  17. {
  18.     /**
  19.      * @Groups({"adminindex", "persondetail"})
  20.      *
  21.      * @ORM\Id()
  22.      *
  23.      * @ORM\GeneratedValue()
  24.      *
  25.      * @ORM\Column(type="integer")
  26.      */
  27.     private $id;
  28.     /**
  29.      * @Groups({"persondetail", "adminindex", "webmasterindex", "amateurupgradeindex"})
  30.      *
  31.      * @ORM\Column(type="string", length=255)
  32.      */
  33.     private $first_name;
  34.     /**
  35.      * @Groups({"persondetail", "adminindex", "webmasterindex", "amateurupgradeindex"})
  36.      *
  37.      * @ORM\Column(type="string", length=255)
  38.      */
  39.     private $last_name;
  40.     /**
  41.      * @Groups({"persondetail", "adminindex"})
  42.      *
  43.      * @ORM\Column(type="date", nullable=true)
  44.      */
  45.     private $date_of_birth;
  46.     /**
  47.      * @Groups({"persondetail", "adminindex"})
  48.      *
  49.      * @ORM\Column(type="string", length=255, nullable=true)
  50.      */
  51.     private $cell_phone;
  52.     /**
  53.      * @Groups({"bankaccount"})
  54.      *
  55.      * @ManyToOne(targetEntity="App\Entity\BankAccount", cascade={"persist"})
  56.      *
  57.      * @JoinColumn(name="bank_account_id", referencedColumnName="id", onDelete="SET NULL")
  58.      */
  59.     private $bank_account;
  60.     /**
  61.      * @Groups({"persondetail", "adminindex"})
  62.      *
  63.      * @ORM\Column(type="datetime")
  64.      */
  65.     private $created_at;
  66.     /**
  67.      * @Groups({"adminindex", "commonids"})
  68.      *
  69.      * @ManyToOne(targetEntity="App\Entity\Account", cascade={"persist"})
  70.      *
  71.      * @JoinColumn(name="account_id", referencedColumnName="id", onDelete="SET NULL")
  72.      */
  73.     private $account;
  74.     /**
  75.      * @Groups({"adminindex", "addressindex"})
  76.      *
  77.      * @ManyToOne(targetEntity="App\Entity\Address", cascade={"persist"})
  78.      *
  79.      * @JoinColumn(name="address_id", referencedColumnName="id", onDelete="SET NULL")
  80.      */
  81.     private $address;
  82.     /**
  83.      * @Groups({"persondetail", "adminindex"})
  84.      *
  85.      * @ORM\Column(type="string", length=255, nullable=true)
  86.      */
  87.     private $company_name;
  88.     /**
  89.      * @Groups({"persondetail", "adminindex"})
  90.      *
  91.      * @ORM\ManyToMany(targetEntity="App\Entity\TaxNumber", mappedBy="person")
  92.      *
  93.      * @ORM\JoinTable(name="PersonTaxNumbers")
  94.      */
  95.     private $taxNumbers;
  96.     public function __construct()
  97.     {
  98.         $this->taxNumbers = new ArrayCollection();
  99.     }
  100.     /**
  101.      * @ORM\PrePersist()
  102.      */
  103.     public function prePersist()
  104.     {
  105.         $this->setCreatedAt(new \DateTime());
  106.     }
  107.     public function __toString(): string
  108.     {
  109.         return $this->getFullName();
  110.     }
  111.     public function getFullName(): string
  112.     {
  113.         return trim($this->getFirstName().' '.$this->getLastName());
  114.     }
  115.     public function getId()
  116.     {
  117.         return $this->id;
  118.     }
  119.     public function getFirstName(): ?string
  120.     {
  121.         return $this->first_name;
  122.     }
  123.     public function setFirstName(string $first_name): self
  124.     {
  125.         $this->first_name $first_name;
  126.         return $this;
  127.     }
  128.     public function getLastName(): ?string
  129.     {
  130.         return $this->last_name;
  131.     }
  132.     public function setLastName(string $last_name): self
  133.     {
  134.         $this->last_name $last_name;
  135.         return $this;
  136.     }
  137.     public function getDateOfBirth(): ?\DateTimeInterface
  138.     {
  139.         return $this->date_of_birth;
  140.     }
  141.     public function setDateOfBirth(?\DateTimeInterface $date_of_birth): self
  142.     {
  143.         $this->date_of_birth $date_of_birth;
  144.         return $this;
  145.     }
  146.     public function getCellPhone(): ?string
  147.     {
  148.         return $this->cell_phone;
  149.     }
  150.     public function setCellPhone(?string $cell_phone): self
  151.     {
  152.         $this->cell_phone $cell_phone;
  153.         return $this;
  154.     }
  155.     public function getCreatedAt(): ?\DateTimeInterface
  156.     {
  157.         return $this->created_at;
  158.     }
  159.     public function setCreatedAt(\DateTimeInterface $created_at): self
  160.     {
  161.         $this->created_at $created_at;
  162.         return $this;
  163.     }
  164.     public function getAccount(): ?Account
  165.     {
  166.         return $this->account;
  167.     }
  168.     public function setAccount(?Account $account): self
  169.     {
  170.         $this->account $account;
  171.         return $this;
  172.     }
  173.     public function getCompanyName(): ?string
  174.     {
  175.         return $this->company_name;
  176.     }
  177.     public function setCompanyName(?string $company_name): self
  178.     {
  179.         $this->company_name $company_name;
  180.         return $this;
  181.     }
  182.     public function setAddress(?Address $address): self
  183.     {
  184.         $this->address $address;
  185.         return $this;
  186.     }
  187.     public function getAddress(): ?Address
  188.     {
  189.         return $this->address;
  190.     }
  191.     public function setBankAccount(?BankAccount $bank_account): self
  192.     {
  193.         $this->bank_account $bank_account;
  194.         return $this;
  195.     }
  196.     public function getBankAccount(): ?BankAccount
  197.     {
  198.         return $this->bank_account;
  199.     }
  200.     /**
  201.      * @return Collection|TaxNumber[]
  202.      */
  203.     public function getTaxNumbers(): Collection
  204.     {
  205.         return $this->taxNumbers;
  206.     }
  207.     public function addTaxNumber(TaxNumber $taxNumber): self
  208.     {
  209.         if (!$this->taxNumbers->contains($taxNumber)) {
  210.             $this->taxNumbers[] = $taxNumber;
  211.             $taxNumber->addPerson($this);
  212.         }
  213.         return $this;
  214.     }
  215.     public function removeTaxNumber(TaxNumber $taxNumber): self
  216.     {
  217.         if ($this->taxNumbers->contains($taxNumber)) {
  218.             $this->taxNumbers->removeElement($taxNumber);
  219.             $taxNumber->removePerson($this);
  220.         }
  221.         return $this;
  222.     }
  223.     /**
  224.      * @throws \Exception
  225.      */
  226.     public function hasChangedMainFields(Person $personbool $includeAddress truebool $includeBankAccount truebool $includeTaxNumbers true): bool
  227.     {
  228.         // $this is the new entity, person is the old one
  229.         $hasChangedPerson $person->getFirstName() !== $this->getFirstName()
  230.             || $person->getLastName() !== $this->getLastName()
  231.             || $person->getCompanyName() !== $this->getCompanyName()
  232.             || $person->getCellPhone() !== $this->getCellPhone();
  233.         if (null !== $person->getDateOfBirth() && null !== $this->getDateOfBirth()) {
  234.             $hasChangedPerson $hasChangedPerson || $person->getDateOfBirth()->format('Ymd') !== $this->getDateOfBirth()->format('Ymd');
  235.         }
  236.         if (null === $person->getDateOfBirth() && null !== $this->getDateOfBirth()) {
  237.             $hasChangedPerson true;
  238.         }
  239.         $hasChangedAddress false;
  240.         if ($includeAddress) {
  241.             if (null === $person->getAddress() && null === $this->getAddress()) {
  242.                 $hasChangedAddress false;
  243.             } elseif ($person->getAddress() instanceof Address) {
  244.                 $hasChangedAddress $person->getAddress()->hasChangedMainFields($this->getAddress());
  245.             } elseif ($this->getAddress() instanceof Address) {
  246.                 $hasChangedAddress $this->getAddress()->hasChangedMainFields($person->getAddress());
  247.             } else {
  248.                 throw new \Exception('impossible');
  249.             }
  250.         }
  251.         $hasChangedBankAccount false;
  252.         if ($includeBankAccount) {
  253.             if (null === $this->getBankAccount() && null === $person->getBankAccount()) {
  254.                 $hasChangedBankAccount false;
  255.             } elseif ($person->getBankAccount() instanceof BankAccount) {
  256.                 $hasChangedBankAccount $person->getBankAccount()->hasChangedMainFields($this->getBankAccount());
  257.             } elseif ($this->getBankAccount() instanceof BankAccount) {
  258.                 $hasChangedBankAccount $this->getBankAccount()->hasChangedMainFields($person->getBankAccount());
  259.             } else {
  260.                 throw new \Exception('impossible');
  261.             }
  262.         }
  263.         $hasChangedTaxNumbers false;
  264.         if ($includeTaxNumbers) {
  265.             if ($this->getTaxNumbers()->count() !== $person->getTaxNumbers()->count()) {
  266.                 $hasChangedTaxNumbers true;
  267.             } else {
  268.                 $newNumbers = [];
  269.                 $oldNumbers = [];
  270.                 foreach ($person->getTaxNumbers() as $taxNumber) {
  271.                     $oldNumbers[] = $taxNumber->getTaxNumber().' / '.$taxNumber->getType();
  272.                 }
  273.                 foreach ($this->getTaxNumbers() as $taxNumber) {
  274.                     $newNumbers[] = $taxNumber->getTaxNumber().' / '.$taxNumber->getType();
  275.                 }
  276.                 foreach ($newNumbers as $newNumber) {
  277.                     if (!in_array($newNumber$oldNumbers)) {
  278.                         $hasChangedTaxNumbers true;
  279.                         break;
  280.                     }
  281.                 }
  282.             }
  283.         }
  284.         return $hasChangedPerson || $hasChangedAddress || $hasChangedBankAccount || $hasChangedTaxNumbers;
  285.     }
  286. }