Newer
Older
<?php
namespace Drupal\address\Plugin\Validation\Constraint;
use CommerceGuys\Addressing\Country\CountryRepositoryInterface;
use Drupal\address\AddressInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* Validates the country constraint.
*/
class CountryConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* The country repository.
*
* @var \CommerceGuys\Addressing\Country\CountryRepositoryInterface
*/
protected $countryRepository;
/**
* Constructs a new CountryConstraintValidator object.
*
* @param \CommerceGuys\Addressing\Country\CountryRepositoryInterface $country_repository
public function __construct(CountryRepositoryInterface $country_repository) {
$this->countryRepository = $country_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('address.country_repository'));
}
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint) {
Bojan Živanović
committed
if (!($value instanceof AddressInterface)) {
throw new UnexpectedTypeException($value, 'AddressInterface');
Bojan Živanović
committed
$address = $value;
$country_code = $address->getCountryCode();
if ($country_code === NULL || $country_code === '') {
Bojan Živanović
committed
return;
}
$countries = $this->countryRepository->getList();
if (!isset($countries[$country_code])) {
$this->context->buildViolation($constraint->invalidMessage)
->setParameter('%value', $this->formatValue($country_code))
return;
}
$available_countries = $constraint->availableCountries;
if (!empty($available_countries) && !in_array($country_code, $available_countries)) {
$this->context->buildViolation($constraint->notAvailableMessage)
->atPath('country_code')
->setParameter('%value', $this->formatValue($country_code))
->addViolation();