2015-10-26 23:59:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Respect/Validation.
|
|
|
|
*
|
|
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
*
|
2019-05-23 16:21:34 +02:00
|
|
|
* For the full copyright and license information, please view the LICENSE file
|
|
|
|
* that was distributed with this source code.
|
2015-10-26 23:59:48 +01:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2015-10-26 23:59:48 +01:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
2021-02-06 14:25:14 +01:00
|
|
|
use Sokil\IsoCodes\IsoCodesFactory;
|
|
|
|
use Sokil\IsoCodes\TranslationDriver\DummyDriver;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2021-02-06 14:25:14 +01:00
|
|
|
use function in_array;
|
|
|
|
use function is_string;
|
2019-02-17 19:41:55 +01:00
|
|
|
use function sprintf;
|
2015-10-26 23:59:48 +01:00
|
|
|
|
|
|
|
/**
|
2018-10-06 22:58:42 +02:00
|
|
|
* Validates whether the input is language code based on ISO 639.
|
|
|
|
*
|
|
|
|
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
2018-11-25 17:20:02 +01:00
|
|
|
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
|
2018-10-06 22:58:42 +02:00
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
2015-10-26 23:59:48 +01:00
|
|
|
*/
|
2021-02-06 14:25:14 +01:00
|
|
|
final class LanguageCode extends AbstractRule
|
2015-10-26 23:59:48 +01:00
|
|
|
{
|
2018-05-10 21:19:42 +02:00
|
|
|
public const ALPHA2 = 'alpha-2';
|
|
|
|
public const ALPHA3 = 'alpha-3';
|
2020-05-26 16:00:24 +02:00
|
|
|
|
|
|
|
public const AVAILABLE_SETS = [self::ALPHA2, self::ALPHA3];
|
2015-10-26 23:59:48 +01:00
|
|
|
|
|
|
|
/**
|
2021-02-06 14:25:14 +01:00
|
|
|
* @var string
|
2015-10-26 23:59:48 +01:00
|
|
|
*/
|
2021-02-06 14:25:14 +01:00
|
|
|
private $set;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var IsoCodesFactory
|
|
|
|
*/
|
|
|
|
private $factory;
|
2015-10-26 23:59:48 +01:00
|
|
|
|
2018-10-06 22:58:42 +02:00
|
|
|
/**
|
|
|
|
* Initializes the rule defining the ISO 639 set.
|
|
|
|
*
|
|
|
|
* @throws ComponentException
|
|
|
|
*/
|
|
|
|
public function __construct(string $set = self::ALPHA2)
|
2015-10-26 23:59:48 +01:00
|
|
|
{
|
2021-02-06 14:25:14 +01:00
|
|
|
if (!in_array($set, self::AVAILABLE_SETS)) {
|
2015-10-26 23:59:48 +01:00
|
|
|
throw new ComponentException(sprintf('"%s" is not a valid language set for ISO 639', $set));
|
|
|
|
}
|
|
|
|
|
2021-02-06 14:25:14 +01:00
|
|
|
$this->set = $set;
|
|
|
|
$this->factory = new IsoCodesFactory(null, new DummyDriver());
|
2015-10-26 23:59:48 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
2021-02-06 14:25:14 +01:00
|
|
|
* {@inheritDoc}
|
2019-02-07 21:22:10 +01:00
|
|
|
*/
|
2021-02-06 14:25:14 +01:00
|
|
|
public function validate($input): bool
|
2015-10-26 23:59:48 +01:00
|
|
|
{
|
2021-02-06 14:25:14 +01:00
|
|
|
if (!is_string($input) || $input === '') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$languages = $this->factory->getLanguages();
|
|
|
|
if ($this->set === self::ALPHA2) {
|
|
|
|
return $languages->getByAlpha2($input) !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $languages->getByAlpha3($input) !== null;
|
2015-10-26 23:59:48 +01:00
|
|
|
}
|
|
|
|
}
|