respect-validation/library/Rules/AbstractFilterRule.php
Henrique Moody 272f18dcf5
Apply "Symfony.Functions.ScopeOrder"
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2019-05-11 20:00:19 +02:00

66 lines
1.5 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 function implode;
use function is_scalar;
use function str_replace;
use function str_split;
/**
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Nick Lombard <github@jigsoft.co.za>
*/
abstract class AbstractFilterRule extends AbstractRule
{
/**
* @var string
*/
private $additionalChars;
/**
* Initializes the rule with a list of characters to be ignored by the validation.
*/
public function __construct(string ...$additionalChars)
{
$this->additionalChars = implode($additionalChars);
}
/**
* {@inheritDoc}
*/
public function validate($input): bool
{
if (!is_scalar($input)) {
return false;
}
$stringInput = (string) $input;
if ($stringInput === '') {
return false;
}
$filteredInput = $this->filter($stringInput);
return $filteredInput === '' || $this->validateFilteredInput($filteredInput);
}
abstract protected function validateFilteredInput(string $input): bool;
private function filter(string $input): string
{
return str_replace(str_split($this->additionalChars), '', $input);
}
}