class CountryValidator

Validates whether a value is a valid country code

@author Bernhard Schussek <bschussek@gmail.com>

@api

Hierarchy

Expanded class hierarchy of CountryValidator

1 file declares its use of CountryValidator
CountryValidatorTest.php in drupal/core/vendor/symfony/validator/Symfony/Component/Validator/Tests/Constraints/CountryValidatorTest.php

File

drupal/core/vendor/symfony/validator/Symfony/Component/Validator/Constraints/CountryValidator.php, line 26

Namespace

Symfony\Component\Validator\Constraints
View source
class CountryValidator extends ConstraintValidator {

  /**
   * {@inheritDoc}
   */
  public function validate($value, Constraint $constraint) {
    if (null === $value || '' === $value) {
      return;
    }
    if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
      throw new UnexpectedTypeException($value, 'string');
    }
    $value = (string) $value;
    $countries = Intl::getRegionBundle()
      ->getCountryNames();
    if (!isset($countries[$value])) {
      $this->context
        ->addViolation($constraint->message, array(
        '{{ value }}' => $value,
      ));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConstraintValidator::$context protected property
ConstraintValidator::initialize public function Initializes the constraint validator. Overrides ConstraintValidatorInterface::initialize 1
CountryValidator::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate