respect-validation/library/Rules/CurrencyCode.php

80 lines
1.9 KiB
PHP
Raw Normal View History

2015-10-18 16:02:48 +02:00
<?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 file
* that was distributed with this source code.
2015-10-18 16:02:48 +02:00
*/
declare(strict_types=1);
2015-10-18 16:02:48 +02:00
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use Sokil\IsoCodes\IsoCodesFactory;
use Sokil\IsoCodes\TranslationDriver\DummyDriver;
use function implode;
use function in_array;
use function is_int;
use function is_string;
use function sprintf;
2015-10-18 16:02:48 +02:00
/**
* Validates currency codes in ISO 4217.
*
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Justin Hook <justinhook88@yahoo.co.uk>
* @author Tim Strijdhorst <tstrijdhorst@users.noreply.github.com>
* @author William Espindola <oi@williamespindola.com.br>
2015-10-18 16:02:48 +02:00
*/
final class CurrencyCode extends AbstractRule
2015-10-18 16:02:48 +02:00
{
public const ALPHA3 = 'alpha-3';
public const NUMERIC = 'numeric';
/**
* @var string
*/
private $set;
/**
* @var IsoCodesFactory
*/
private $factory;
public function __construct(string $set = self::ALPHA3)
{
if (!in_array($set, [self::ALPHA3, self::NUMERIC])) {
throw new ComponentException(sprintf(
'"%s" is not a valid set for ISO 4217 (Available: %s)',
$set,
implode(', ', [self::ALPHA3, self::NUMERIC])
));
}
$this->set = $set;
$this->factory = new IsoCodesFactory(null, new DummyDriver());
}
2015-10-18 16:02:48 +02:00
/**
* {@inheritDoc}
2015-10-18 16:02:48 +02:00
*/
public function validate($input): bool
2015-10-18 16:02:48 +02:00
{
if (!is_string($input) && !is_int($input)) {
return false;
}
$currencies = $this->factory->getCurrencies();
if ($this->set === self::ALPHA3) {
return $currencies->getByLetterCode((string) $input) !== null;
}
return $currencies->getByNumericCode($input) !== null;
2015-10-18 16:02:48 +02:00
}
}