mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 02:30:06 +02:00
c60ca7937b
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>
59 lines
1.4 KiB
PHP
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));
|
|
}
|
|
}
|