respect-validation/library/Rules/AbstractLocaleWrapper.php
Henrique Moody c30603759e
Apply "SlevomatCodingStandard.TypeHints.TypeHintDeclaration"
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2019-02-09 14:09:28 +01:00

55 lines
1.4 KiB
PHP

<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use function class_exists;
use function mb_strtolower;
use function sprintf;
use function ucfirst;
/**
* Abstract class to help creating rules based on location.
*
* @author Henrique Moody <henriquemoody@gmail.com>
*/
abstract class AbstractLocaleWrapper extends AbstractWrapper
{
/**
* @var string
*/
private $countryCode;
/**
* Initializes the rule.
*
* @throws ComponentException when country is not supported
*/
public function __construct(string $countryCode)
{
$normalized = ucfirst(mb_strtolower($countryCode));
$className = sprintf('%s\\Locale\\%s%s', __NAMESPACE__, $normalized, $this->getSuffix());
if (!class_exists($className)) {
throw new ComponentException(sprintf('"%s" is not a supported country code', $countryCode));
}
$this->countryCode = $countryCode;
parent::__construct(new $className());
}
/**
* Returns the class name based on the identifier.
*/
abstract protected function getSuffix(): string;
}