respect-validation/library/Rules/FilterVar.php
Henrique Moody e0c6f33c01
Only define filter_var options when it is valid
The third argument of "filter_var" must be either an integer or an
array. On PHP 8 this "FilterVar" rule fails because we always pass that
argument, even if it is null.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2020-10-04 11:13:18 +02:00

70 lines
1.7 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 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 function is_array;
use function is_int;
use const FILTER_VALIDATE_BOOLEAN;
use const FILTER_VALIDATE_DOMAIN;
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_DOMAIN,
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');
}
$arguments = [$filter];
if (is_array($options) || is_int($options)) {
$arguments[] = $options;
}
parent::__construct(new Callback('filter_var', ...$arguments));
}
}