src/Entity/Country.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CountryRepository;
  4. use App\Tools\Utils;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. #[ORM\Table(name'country')]
  10. #[ORM\UniqueConstraint(name'country_name_unique'columns: ['name'])]
  11. #[ORM\Entity(repositoryClassCountryRepository::class)]
  12. #[ORM\HasLifecycleCallbacks]
  13. class Country {
  14.     #[ORM\Id]
  15.     #[ORM\GeneratedValue]
  16.     #[ORM\Column(type'integer')]
  17.     private ?int $id null;
  18.     #[ORM\Column(name'valid'type'boolean')]
  19.     private bool $valid;
  20.     #[ORM\Column(name'name'type'string'length255uniquefalse)]
  21.     #[Assert\NotBlank]
  22.     private ?string $name null;
  23.     #[ORM\Column(name'iso_code'type'string'length3uniquefalsenullabletrue)]
  24.     private ?string $isoCode null;
  25.     #[ORM\Column(name'phone_code'type'integer')]
  26.     private int $phoneCode;
  27.     #[ORM\Column(name'phone_digit'type'integer')]
  28.     private int $phoneDigit;
  29.     #[ORM\OneToMany(mappedBy'country'targetEntityRegion::class, cascade: ['remove''persist'])]
  30.     private Collection $regions;
  31.     #[ORM\ManyToOne(targetEntityCurrency::class, inversedBy'countries')]
  32.     #[ORM\JoinColumn(name'id_currency'referencedColumnName'id')]
  33.     private Currency $currency;
  34.     public function __construct() {
  35.         $this->regions = new ArrayCollection();
  36.         $this->valid false;
  37.     }
  38.     public static function fromFixtures(
  39.         string $name,
  40.         string $isoCode,
  41.         string $phoneCode,
  42.         string $phoneDigit,
  43.         bool $isValid
  44.     ): static {
  45.         return (new static())
  46.             ->setName($name)
  47.             ->setIsoCode($isoCode)
  48.             ->setPhoneCode($phoneCode)
  49.             ->setPhoneDigit($phoneDigit)
  50.             ->setValid($isValid);
  51.     }
  52.     public function setValid(bool $valid): self {
  53.         $this->valid $valid;
  54.         return $this;
  55.     }
  56.     #[ORM\PrePersist]
  57.     #[ORM\PreFlush]
  58.     public function prePersist() {
  59.         if ($this->regions->count()) {
  60.             /** @var Region $region */
  61.             foreach ($this->regions as $region) {
  62.                 $region->setCountry($this);
  63.             }
  64.         }
  65.     }
  66.     public function getId(): ?int {
  67.         return $this->id;
  68.     }
  69.     public function isValid(): bool {
  70.         return $this->valid;
  71.     }
  72.     public function getIsoCode(): string {
  73.         return $this->isoCode;
  74.     }
  75.     public function setIsoCode(string $isoCode null): self {
  76.         $this->isoCode strtoupper($isoCode);
  77.         return $this;
  78.     }
  79.     public function getPhoneCode(): int {
  80.         return $this->phoneCode;
  81.     }
  82.     public function setPhoneCode(string $phoneCode): self {
  83.         $this->phoneCode $phoneCode;
  84.         return $this;
  85.     }
  86.     public function getPhoneDigit(): int {
  87.         return $this->phoneDigit;
  88.     }
  89.     public function setPhoneDigit(int $phoneDigit): self {
  90.         $this->phoneDigit $phoneDigit;
  91.         return $this;
  92.     }
  93.     public function getCurrency(): Currency {
  94.         return $this->currency;
  95.     }
  96.     public function setCurrency(Currency $currency): self {
  97.         $this->currency $currency;
  98.         return $this;
  99.     }
  100.     public function addRegion(Region $region): self {
  101.         $this->regions->add($region);
  102.         return $this;
  103.     }
  104.     public function removeRegion(Region $region): void {
  105.         $this->regions->removeElement($region);
  106.     }
  107.     public function getRegions(): Collection {
  108.         return $this->regions;
  109.     }
  110.     public function __toString(): string {
  111.         return $this->getName();
  112.     }
  113.     public function getName(): ?string {
  114.         return $this->name;
  115.     }
  116.     public function setName(string $name): self {
  117.         $this->name $name;
  118.         return $this;
  119.     }
  120. }