<?php
/**
* @author Leo BANNHOLTZER (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace App\Entity;
use Symfony\Component\Uid\Uuid;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\CountryTranslation;
use App\Repository\CountryRepository;
use Gedmo\Mapping\Annotation as Gedmo;
use App\DoctrineExtensions\IsActiveEntity;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use App\DoctrineExtensions\Timestampable\Traits\UserTimestampableEntity;
use App\DoctrineExtensions\SoftDeleteable\Traits\UserSoftDeleteableEntity;
/**
* @ORM\Entity(repositoryClass=CountryRepository::class)
* @Gedmo\TranslationEntity(class="App\Entity\CountryTranslation")
* @ORM\Table(name="country", indexes={
* @ORM\Index(name="name", columns={"name"}),
* @ORM\Index(name="iso_code", columns={"iso_code"}),
* @ORM\Index(name="area_code", columns={"area_code"}),
* @ORM\Index(name="is_active", columns={"is_active"}),
* @ORM\Index(name="deleted_at", columns={"deleted_at"}),
* @ORM\Index(name="created_at", columns={"created_at"}),
* @ORM\Index(name="updated_at", columns={"updated_at"})
* })
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=false)
*/
class Country implements \JsonSerializable
{
use UserTimestampableEntity;
use UserSoftDeleteableEntity;
use IsActiveEntity;
/**
* @ORM\Id
* @ORM\Column(type="uuid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
*/
private ?Uuid $id = null;
/**
* @ORM\ManyToOne(targetEntity=CountryZone::class, inversedBy="countries")
*/
private ?CountryZone $country_zone;
/**
* @Gedmo\Translatable
* @ORM\Column(type="string", length=64)
*/
private string $name;
/**
* @ORM\Column(type="string", length=3, nullable=true)
*/
private ?string $iso_code;
/**
* @ORM\Column(type="string", length=10, nullable=true)
*/
private ?string $area_code;
/**
* @ORM\OneToMany(targetEntity=CountryState::class, mappedBy="country", fetch="EXTRA_LAZY")
*/
private Collection $countryStates;
/**
* @ORM\OneToMany(targetEntity=TaxRuleCountry::class, mappedBy="country", fetch="EXTRA_LAZY")
*/
private Collection $taxRuleCountries;
/**
* @ORM\OneToMany(targetEntity=Context::class, mappedBy="country", fetch="EXTRA_LAZY")
*/
private Collection $contexts;
/**
* @ORM\OneToMany(
* targetEntity="CountryTranslation",
* mappedBy="object",
* cascade={"persist", "remove"},
* fetch="EXTRA_LAZY"
* )
*/
private Collection $translations;
/**
* @ORM\OneToMany(targetEntity=Establishment::class, mappedBy="country")
*/
private ?Collection $establishments;
public function __construct()
{
$this->countryStates = new ArrayCollection();
$this->taxRuleCountries = new ArrayCollection();
$this->contexts = new ArrayCollection();
$this->translations = new ArrayCollection();
$this->establishments = new ArrayCollection();
}
/**
* @return Uuid|null
*/
public function getId(): ?Uuid
{
return $this->id;
}
/**
* @return CountryZone|null
*/
public function getCountryZone(): ?CountryZone
{
return $this->country_zone;
}
/**
* @param CountryZone|null $country_zone
* @return $this
*/
public function setCountryZone(?CountryZone $country_zone): self
{
$this->country_zone = $country_zone;
return $this;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
* @return $this
*/
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return string|null
*/
public function getIsoCode(): ?string
{
return $this->iso_code;
}
/**
* @param string|null $iso_code
* @return $this
*/
public function setIsoCode(?string $iso_code): self
{
$this->iso_code = $iso_code;
return $this;
}
/**
* @return string|null
*/
public function getAreaCode(): ?string
{
return $this->area_code;
}
/**
* @param string|null $area_code
* @return $this
*/
public function setAreaCode(?string $area_code): self
{
$this->area_code = $area_code;
return $this;
}
/**
* @return Collection|CountryState[]
*/
public function getCountryStates(): Collection
{
return $this->countryStates;
}
/**
* @return Collection|TaxRuleCountry[]
*/
public function getTaxRuleCountries(): Collection
{
return $this->taxRuleCountries;
}
/**
* @return Collection|Context[]
*/
public function getContexts(): Collection
{
return $this->contexts;
}
/**
* @param Context $context
* @return $this
*/
public function addContext(Context $context): self
{
if (!$this->contexts->contains($context)) {
$this->contexts[] = $context;
$context->setCountry($this);
}
return $this;
}
/**
* @return Collection|CountryTranslation[]
*/
public function getTranslations()
{
return $this->translations;
}
/**
* @param CountryTranslation $t
* @return $this
*/
public function addTranslation(CountryTranslation $t): self
{
if (!$this->translations->contains($t)) {
$this->translations[] = $t;
$t->setObject($this);
}
return $this;
}
/**
* @return mixed
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return [
'id' => $this->id,
'name' => $this->name,
'country_zone' => $this->country_zone,
'iso_code' => $this->iso_code,
'countryStates' => $this->countryStates->toArray(),
'taxRuleCountries' => $this->taxRuleCountries->toArray(),
'contexts' => $this->contexts
];
}
/**
* @return bool
*/
public function isEU(): bool
{
$eu_countrycodes = [
'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'EL',
'ES', 'FI', 'FR', 'GR', 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV',
'MC', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK'
];
return in_array(strtoupper($this->iso_code), $eu_countrycodes);
}
/**
* @return Collection<int, Establishment>
*/
public function getEstablishments(): Collection
{
return $this->establishments;
}
public function addEstablishment(Establishment $establishment): self
{
if (!$this->establishments->contains($establishment)) {
$this->establishments[] = $establishment;
$establishment->setCountry($this);
}
return $this;
}
public function removeEstablishment(Establishment $establishment): self
{
if ($this->establishments->removeElement($establishment)) {
// set the owning side to null (unless already changed)
if ($establishment->getCountry() === $this) {
$establishment->setCountry(null);
}
}
return $this;
}
}