respect-validation/library/Rules/FilterVar.php
Henrique Moody c60ca7937b
Update the Coding Standards
This commit will add a couple of rules that we already use but also
introduce new ones and make sure all the code is in accordance with the
new coding standards.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2019-02-17 19:52:26 +01:00

59 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 in_array;
use const FILTER_VALIDATE_BOOLEAN;
use const FILTER_VALIDATE_EMAIL;
use const FILTER_VALIDATE_FLOAT;
use const FILTER_VALIDATE_INT;
use const FILTER_VALIDATE_IP;
use const FILTER_VALIDATE_REGEXP;
use const FILTER_VALIDATE_URL;
/**
* Validates the input with the PHP's filter_var() function.
*
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class FilterVar extends AbstractEnvelope
{
private const ALLOWED_FILTERS = [
FILTER_VALIDATE_BOOLEAN,
FILTER_VALIDATE_EMAIL,
FILTER_VALIDATE_FLOAT,
FILTER_VALIDATE_INT,
FILTER_VALIDATE_IP,
FILTER_VALIDATE_REGEXP,
FILTER_VALIDATE_URL,
];
/**
* Initializes the rule.
*
* @param mixed $options
*
* @throws ComponentException
*/
public function __construct(int $filter, $options = null)
{
if (!in_array($filter, self::ALLOWED_FILTERS)) {
throw new ComponentException('Cannot accept the given filter');
}
parent::__construct(new Callback('filter_var', $filter, $options));
}
}