61 lines
1.9 KiB
PHP
61 lines
1.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Symfony package.
|
|
*
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Symfony\Component\Validator\Constraints;
|
|
|
|
use Symfony\Component\Intl\Countries;
|
|
use Symfony\Component\Validator\Constraint;
|
|
use Symfony\Component\Validator\Exception\LogicException;
|
|
|
|
/**
|
|
* Validates a value is a valid ISO 3166-1 alpha-2 country code.
|
|
*
|
|
* @see https://en.wikipedia.org/wiki/ISO_3166-1#Current_codes
|
|
*
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
*/
|
|
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
|
|
class Country extends Constraint
|
|
{
|
|
public const NO_SUCH_COUNTRY_ERROR = '8f900c12-61bd-455d-9398-996cd040f7f0';
|
|
|
|
protected const ERROR_NAMES = [
|
|
self::NO_SUCH_COUNTRY_ERROR => 'NO_SUCH_COUNTRY_ERROR',
|
|
];
|
|
|
|
public string $message = 'This value is not a valid country.';
|
|
public bool $alpha3 = false;
|
|
|
|
/**
|
|
* @param array<string,mixed>|null $options
|
|
* @param bool|null $alpha3 Whether to check for alpha-3 codes instead of alpha-2 (defaults to false)
|
|
* @param string[]|null $groups
|
|
*
|
|
* @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3#Current_codes
|
|
*/
|
|
public function __construct(
|
|
?array $options = null,
|
|
?string $message = null,
|
|
?bool $alpha3 = null,
|
|
?array $groups = null,
|
|
mixed $payload = null,
|
|
) {
|
|
if (!class_exists(Countries::class)) {
|
|
throw new LogicException('The Intl component is required to use the Country constraint. Try running "composer require symfony/intl".');
|
|
}
|
|
|
|
parent::__construct($options, $groups, $payload);
|
|
|
|
$this->message = $message ?? $this->message;
|
|
$this->alpha3 = $alpha3 ?? $this->alpha3;
|
|
}
|
|
}
|