<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Component\Addressing\Model;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Resource\Model\ToggleableTrait;
use Symfony\Component\Intl\Intl;
class Country implements CountryInterface
{
use ToggleableTrait;
/** @var mixed */
protected $id;
/**
* Country code ISO 3166-1 alpha-2.
*
* @var string|null
*/
protected $code;
/** @var Collection|ProvinceInterface[] */
protected $provinces;
public function __construct()
{
$this->provinces = new ArrayCollection();
}
public function __toString(): string
{
return (string) ($this->getName() ?? $this->getCode());
}
/**
* {@inheritdoc}
*/
public function getId()
{
return $this->id;
}
/**
* {@inheritdoc}
*/
public function getCode(): ?string
{
return $this->code;
}
/**
* {@inheritdoc}
*/
public function setCode(?string $code): void
{
$this->code = $code;
}
/**
* {@inheritdoc}
*/
public function getName(?string $locale = null): ?string
{
return Intl::getRegionBundle()->getCountryName($this->code, $locale);
}
/**
* {@inheritdoc}
*/
public function getProvinces(): Collection
{
return $this->provinces;
}
/**
* {@inheritdoc}
*/
public function hasProvinces(): bool
{
return !$this->provinces->isEmpty();
}
/**
* {@inheritdoc}
*/
public function addProvince(ProvinceInterface $province): void
{
if (!$this->hasProvince($province)) {
$this->provinces->add($province);
$province->setCountry($this);
}
}
/**
* {@inheritdoc}
*/
public function removeProvince(ProvinceInterface $province): void
{
if ($this->hasProvince($province)) {
$this->provinces->removeElement($province);
$province->setCountry(null);
}
}
/**
* {@inheritdoc}
*/
public function hasProvince(ProvinceInterface $province): bool
{
return $this->provinces->contains($province);
}
}