vendor/sylius/sylius/src/Sylius/Component/Addressing/Model/Country.php line 21

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Component\Addressing\Model;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Sylius\Component\Resource\Model\ToggleableTrait;
  15. use Symfony\Component\Intl\Countries;
  16. class Country implements CountryInterface\Stringable
  17. {
  18.     use ToggleableTrait;
  19.     /** @var mixed */
  20.     protected $id;
  21.     /**
  22.      * Country code ISO 3166-1 alpha-2.
  23.      *
  24.      * @var string|null
  25.      */
  26.     protected $code;
  27.     /**
  28.      * @var Collection|ProvinceInterface[]
  29.      *
  30.      * @psalm-var Collection<array-key, ProvinceInterface>
  31.      */
  32.     protected $provinces;
  33.     public function __construct()
  34.     {
  35.         /** @var ArrayCollection<array-key, ProvinceInterface> $this->provinces */
  36.         $this->provinces = new ArrayCollection();
  37.     }
  38.     public function __toString(): string
  39.     {
  40.         return (string) ($this->getName() ?? $this->getCode());
  41.     }
  42.     public function getId()
  43.     {
  44.         return $this->id;
  45.     }
  46.     public function getCode(): ?string
  47.     {
  48.         return $this->code;
  49.     }
  50.     public function setCode(?string $code): void
  51.     {
  52.         $this->code $code;
  53.     }
  54.     public function getName(?string $locale null): ?string
  55.     {
  56.         return Countries::getName($this->code$locale);
  57.     }
  58.     public function getProvinces(): Collection
  59.     {
  60.         return $this->provinces;
  61.     }
  62.     public function hasProvinces(): bool
  63.     {
  64.         return !$this->provinces->isEmpty();
  65.     }
  66.     public function addProvince(ProvinceInterface $province): void
  67.     {
  68.         if (!$this->hasProvince($province)) {
  69.             $this->provinces->add($province);
  70.             $province->setCountry($this);
  71.         }
  72.     }
  73.     public function removeProvince(ProvinceInterface $province): void
  74.     {
  75.         if ($this->hasProvince($province)) {
  76.             $this->provinces->removeElement($province);
  77.             $province->setCountry(null);
  78.         }
  79.     }
  80.     public function hasProvince(ProvinceInterface $province): bool
  81.     {
  82.         return $this->provinces->contains($province);
  83.     }
  84. }