src/Entity/Country.php line 39

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Leo BANNHOLTZER (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace App\Entity;
  9. use Symfony\Component\Uid\Uuid;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use App\Entity\CountryTranslation;
  12. use App\Repository\CountryRepository;
  13. use Gedmo\Mapping\Annotation as Gedmo;
  14. use App\DoctrineExtensions\IsActiveEntity;
  15. use Doctrine\Common\Collections\Collection;
  16. use Doctrine\Common\Collections\ArrayCollection;
  17. use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
  18. use App\DoctrineExtensions\Timestampable\Traits\UserTimestampableEntity;
  19. use App\DoctrineExtensions\SoftDeleteable\Traits\UserSoftDeleteableEntity;
  20. /**
  21.  * @ORM\Entity(repositoryClass=CountryRepository::class)
  22.  * @Gedmo\TranslationEntity(class="App\Entity\CountryTranslation")
  23.  * @ORM\Table(name="country", indexes={
  24.  *  @ORM\Index(name="name", columns={"name"}),
  25.  *  @ORM\Index(name="iso_code", columns={"iso_code"}),
  26.  *  @ORM\Index(name="area_code", columns={"area_code"}),
  27.  *  @ORM\Index(name="is_active", columns={"is_active"}),
  28.  *  @ORM\Index(name="deleted_at", columns={"deleted_at"}),
  29.  *  @ORM\Index(name="created_at", columns={"created_at"}),
  30.  *  @ORM\Index(name="updated_at", columns={"updated_at"})
  31.  * })
  32.  * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=false)
  33.  */
  34. class Country implements \JsonSerializable
  35. {
  36.     use UserTimestampableEntity;
  37.     use UserSoftDeleteableEntity;
  38.     use IsActiveEntity;
  39.     /**
  40.      * @ORM\Id
  41.      * @ORM\Column(type="uuid")
  42.      * @ORM\GeneratedValue(strategy="CUSTOM")
  43.      * @ORM\CustomIdGenerator(class=UuidGenerator::class)
  44.      */
  45.     private ?Uuid $id null;
  46.     /**
  47.      * @ORM\ManyToOne(targetEntity=CountryZone::class, inversedBy="countries")
  48.      */
  49.     private ?CountryZone $country_zone;
  50.     /**
  51.      * @Gedmo\Translatable
  52.      * @ORM\Column(type="string", length=64)
  53.      */
  54.     private string $name;
  55.     /**
  56.      * @ORM\Column(type="string", length=3, nullable=true)
  57.      */
  58.     private ?string $iso_code;
  59.     /**
  60.      * @ORM\Column(type="string", length=10, nullable=true)
  61.      */
  62.     private ?string $area_code;
  63.     /**
  64.      * @ORM\OneToMany(targetEntity=CountryState::class, mappedBy="country", fetch="EXTRA_LAZY")
  65.      */
  66.     private Collection $countryStates;
  67.     /**
  68.      * @ORM\OneToMany(targetEntity=TaxRuleCountry::class, mappedBy="country", fetch="EXTRA_LAZY")
  69.      */
  70.     private Collection $taxRuleCountries;
  71.     /**
  72.      * @ORM\OneToMany(targetEntity=Context::class, mappedBy="country", fetch="EXTRA_LAZY")
  73.      */
  74.     private Collection $contexts;
  75.     /**
  76.      * @ORM\OneToMany(
  77.      *   targetEntity="CountryTranslation",
  78.      *   mappedBy="object",
  79.      *   cascade={"persist", "remove"},
  80.      *   fetch="EXTRA_LAZY"
  81.      * )
  82.      */
  83.     private Collection $translations;
  84.     /**
  85.      * @ORM\OneToMany(targetEntity=Establishment::class, mappedBy="country")
  86.      */
  87.     private ?Collection $establishments;
  88.     public function __construct()
  89.     {
  90.         $this->countryStates = new ArrayCollection();
  91.         $this->taxRuleCountries = new ArrayCollection();
  92.         $this->contexts = new ArrayCollection();
  93.         $this->translations = new ArrayCollection();
  94.         $this->establishments = new ArrayCollection();
  95.     }
  96.     /**
  97.      * @return Uuid|null
  98.      */
  99.     public function getId(): ?Uuid
  100.     {
  101.         return $this->id;
  102.     }
  103.     /**
  104.      * @return CountryZone|null
  105.      */
  106.     public function getCountryZone(): ?CountryZone
  107.     {
  108.         return $this->country_zone;
  109.     }
  110.     /**
  111.      * @param CountryZone|null $country_zone
  112.      * @return $this
  113.      */
  114.     public function setCountryZone(?CountryZone $country_zone): self
  115.     {
  116.         $this->country_zone $country_zone;
  117.         return $this;
  118.     }
  119.     /**
  120.      * @return string
  121.      */
  122.     public function getName(): string
  123.     {
  124.         return $this->name;
  125.     }
  126.     /**
  127.      * @param string $name
  128.      * @return $this
  129.      */
  130.     public function setName(string $name): self
  131.     {
  132.         $this->name $name;
  133.         return $this;
  134.     }
  135.     /**
  136.      * @return string|null
  137.      */
  138.     public function getIsoCode(): ?string
  139.     {
  140.         return $this->iso_code;
  141.     }
  142.     /**
  143.      * @param string|null $iso_code
  144.      * @return $this
  145.      */
  146.     public function setIsoCode(?string $iso_code): self
  147.     {
  148.         $this->iso_code $iso_code;
  149.         return $this;
  150.     }
  151.     /**
  152.      * @return string|null
  153.      */
  154.     public function getAreaCode(): ?string
  155.     {
  156.         return $this->area_code;
  157.     }
  158.     /**
  159.      * @param string|null $area_code
  160.      * @return $this
  161.      */
  162.     public function setAreaCode(?string $area_code): self
  163.     {
  164.         $this->area_code $area_code;
  165.         return $this;
  166.     }
  167.     /**
  168.      * @return Collection|CountryState[]
  169.      */
  170.     public function getCountryStates(): Collection
  171.     {
  172.         return $this->countryStates;
  173.     }
  174.     /**
  175.      * @return Collection|TaxRuleCountry[]
  176.      */
  177.     public function getTaxRuleCountries(): Collection
  178.     {
  179.         return $this->taxRuleCountries;
  180.     }
  181.     /**
  182.      * @return Collection|Context[]
  183.      */
  184.     public function getContexts(): Collection
  185.     {
  186.         return $this->contexts;
  187.     }
  188.     /**
  189.      * @param Context $context
  190.      * @return $this
  191.      */
  192.     public function addContext(Context $context): self
  193.     {
  194.         if (!$this->contexts->contains($context)) {
  195.             $this->contexts[] = $context;
  196.             $context->setCountry($this);
  197.         }
  198.         return $this;
  199.     }
  200.     /**
  201.      * @return Collection|CountryTranslation[]
  202.      */
  203.     public function getTranslations()
  204.     {
  205.         return $this->translations;
  206.     }
  207.     /**
  208.      * @param CountryTranslation $t
  209.      * @return $this
  210.      */
  211.     public function addTranslation(CountryTranslation $t): self
  212.     {
  213.         if (!$this->translations->contains($t)) {
  214.             $this->translations[] = $t;
  215.             $t->setObject($this);
  216.         }
  217.         return $this;
  218.     }
  219.     /**
  220.      * @return mixed
  221.      */
  222.     #[\ReturnTypeWillChange]
  223.     public function jsonSerialize()
  224.     {
  225.         return [
  226.             'id' => $this->id,
  227.             'name' => $this->name,
  228.             'country_zone' => $this->country_zone,
  229.             'iso_code' => $this->iso_code,
  230.             'countryStates' => $this->countryStates->toArray(),
  231.             'taxRuleCountries' => $this->taxRuleCountries->toArray(),
  232.             'contexts' => $this->contexts
  233.         ];
  234.     }
  235.     /**
  236.      * @return bool
  237.      */
  238.     public function isEU(): bool
  239.     {
  240.         $eu_countrycodes = [
  241.             'AT''BE''BG''CY''CZ''DE''DK''EE''EL',
  242.             'ES''FI''FR''GR''HR''HU''IE''IT''LT''LU''LV',
  243.             'MC''MT''NL''PL''PT''RO''SE''SI''SK'
  244.         ];
  245.         return in_array(strtoupper($this->iso_code), $eu_countrycodes);
  246.     }
  247.     /**
  248.      * @return Collection<int, Establishment>
  249.      */
  250.     public function getEstablishments(): Collection
  251.     {
  252.         return $this->establishments;
  253.     }
  254.     public function addEstablishment(Establishment $establishment): self
  255.     {
  256.         if (!$this->establishments->contains($establishment)) {
  257.             $this->establishments[] = $establishment;
  258.             $establishment->setCountry($this);
  259.         }
  260.         return $this;
  261.     }
  262.     public function removeEstablishment(Establishment $establishment): self
  263.     {
  264.         if ($this->establishments->removeElement($establishment)) {
  265.             // set the owning side to null (unless already changed)
  266.             if ($establishment->getCountry() === $this) {
  267.                 $establishment->setCountry(null);
  268.             }
  269.         }
  270.         return $this;
  271.     }
  272. }