mirror of
https://github.com/Respect/Validation.git
synced 2024-06-01 13:22:24 +02:00
689026fc7b
The `filter_var` function is more of a sanitizer, but we as a validation library do not care for that use case. We should treat its sanitizings as a signal for checking if the type after sanitization matches the option provided. This fixes #1387
75 lines
2 KiB
PHP
75 lines
2 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Respect/Validation.
|
|
*
|
|
* (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
*
|
|
* 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 array_key_exists;
|
|
use function filter_var;
|
|
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 => 'is_bool',
|
|
FILTER_VALIDATE_DOMAIN => 'is_string',
|
|
FILTER_VALIDATE_EMAIL => 'is_string',
|
|
FILTER_VALIDATE_FLOAT => 'is_float',
|
|
FILTER_VALIDATE_INT => 'is_int',
|
|
FILTER_VALIDATE_IP => 'is_string',
|
|
FILTER_VALIDATE_REGEXP => 'is_string',
|
|
FILTER_VALIDATE_URL => 'is_string',
|
|
];
|
|
|
|
/**
|
|
* Initializes the rule.
|
|
*
|
|
* @param mixed $options
|
|
*
|
|
* @throws ComponentException
|
|
*/
|
|
public function __construct(int $filter, $options = null)
|
|
{
|
|
if (!array_key_exists($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(static function ($input) use ($filter, $arguments) {
|
|
return (self::ALLOWED_FILTERS[$filter])(
|
|
filter_var($input, ...$arguments)
|
|
);
|
|
}));
|
|
}
|
|
}
|