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

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\Intl;
  16. class Country implements CountryInterface
  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.     /** @var Collection|ProvinceInterface[] */
  28.     protected $provinces;
  29.     public function __construct()
  30.     {
  31.         $this->provinces = new ArrayCollection();
  32.     }
  33.     public function __toString(): string
  34.     {
  35.         return (string) ($this->getName() ?? $this->getCode());
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public function getId()
  41.     {
  42.         return $this->id;
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function getCode(): ?string
  48.     {
  49.         return $this->code;
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      */
  54.     public function setCode(?string $code): void
  55.     {
  56.         $this->code $code;
  57.     }
  58.     /**
  59.      * {@inheritdoc}
  60.      */
  61.     public function getName(?string $locale null): ?string
  62.     {
  63.         return Intl::getRegionBundle()->getCountryName($this->code$locale);
  64.     }
  65.     /**
  66.      * {@inheritdoc}
  67.      */
  68.     public function getProvinces(): Collection
  69.     {
  70.         return $this->provinces;
  71.     }
  72.     /**
  73.      * {@inheritdoc}
  74.      */
  75.     public function hasProvinces(): bool
  76.     {
  77.         return !$this->provinces->isEmpty();
  78.     }
  79.     /**
  80.      * {@inheritdoc}
  81.      */
  82.     public function addProvince(ProvinceInterface $province): void
  83.     {
  84.         if (!$this->hasProvince($province)) {
  85.             $this->provinces->add($province);
  86.             $province->setCountry($this);
  87.         }
  88.     }
  89.     /**
  90.      * {@inheritdoc}
  91.      */
  92.     public function removeProvince(ProvinceInterface $province): void
  93.     {
  94.         if ($this->hasProvince($province)) {
  95.             $this->provinces->removeElement($province);
  96.             $province->setCountry(null);
  97.         }
  98.     }
  99.     /**
  100.      * {@inheritdoc}
  101.      */
  102.     public function hasProvince(ProvinceInterface $province): bool
  103.     {
  104.         return $this->provinces->contains($province);
  105.     }
  106. }