src/Entity/Country.php line 15

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CountryRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. /**
  8.  * @ORM\Entity(repositoryClass=CountryRepository::class)
  9.  * @ORM\Table(name="country")
  10.  * @ORM\HasLifecycleCallbacks()
  11.  */
  12. class Country
  13. {
  14.     /**
  15.      * @ORM\Id
  16.      * @ORM\GeneratedValue
  17.      * @ORM\Column(type="integer")
  18.      */
  19.     private ?int $id null;
  20.     /**
  21.      * @ORM\Column(type="string", length=100)
  22.      */
  23.     private string $name;
  24.     /**
  25.      * ISO 3166-1 alpha-2, e.g. "AM", "FR"
  26.      * @ORM\Column(type="string", length=2, unique=true)
  27.      */
  28.     private string $code;
  29.     /**
  30.      * @ORM\Column(type="string", length=120, unique=true)
  31.      */
  32.     private string $slug;
  33.     /**
  34.      * @ORM\Column(type="boolean", options={"default": true})
  35.      */
  36.     private bool $isActive true;
  37.     /**
  38.      * @ORM\Column(type="datetime_immutable")
  39.      */
  40.     private \DateTimeImmutable $createdAt;
  41.     /**
  42.      * @ORM\OneToMany(targetEntity=City::class, mappedBy="country", cascade={"persist"}, orphanRemoval=true)
  43.      */
  44.     private Collection $cities;
  45.     public function __construct()
  46.     {
  47.         $this->cities    = new ArrayCollection();
  48.         $this->createdAt = new \DateTimeImmutable();
  49.     }
  50.     public function getId(): ?int
  51.     {
  52.         return $this->id;
  53.     }
  54.     public function getName(): string
  55.     {
  56.         return $this->name;
  57.     }
  58.     public function setName(string $name): self
  59.     {
  60.         $this->name $name;
  61.         return $this;
  62.     }
  63.     public function getCode(): string
  64.     {
  65.         return $this->code;
  66.     }
  67.     public function setCode(string $code): self
  68.     {
  69.         $this->code strtoupper($code);
  70.         return $this;
  71.     }
  72.     public function getSlug(): string
  73.     {
  74.         return $this->slug;
  75.     }
  76.     public function setSlug(string $slug): self
  77.     {
  78.         $this->slug $slug;
  79.         return $this;
  80.     }
  81.     public function isActive(): bool
  82.     {
  83.         return $this->isActive;
  84.     }
  85.     public function setIsActive(bool $isActive): self
  86.     {
  87.         $this->isActive $isActive;
  88.         return $this;
  89.     }
  90.     public function getCreatedAt(): \DateTimeImmutable
  91.     {
  92.         return $this->createdAt;
  93.     }
  94.     public function getCities(): Collection
  95.     {
  96.         return $this->cities;
  97.     }
  98.     public function addCity(City $city): self
  99.     {
  100.         if (!$this->cities->contains($city)) {
  101.             $this->cities->add($city);
  102.             $city->setCountry($this);
  103.         }
  104.         return $this;
  105.     }
  106.     public function removeCity(City $city): self
  107.     {
  108.         if ($this->cities->removeElement($city)) {
  109.             if ($city->getCountry() === $this) {
  110.                 $city->setCountry(null);
  111.             }
  112.         }
  113.         return $this;
  114.     }
  115.     public function __toString(): string
  116.     {
  117.         return ($this->title ?? '');
  118.     }
  119. }