2022-12-24 22:10:40 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2024-01-31 22:15:08 +08:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2022-12-24 22:10:40 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
|
|
|
use Respect\Validation\Helpers\CanValidateUndefined;
|
|
|
|
|
|
|
|
use function in_array;
|
2024-01-31 22:15:08 +08:00
|
|
|
use function is_scalar;
|
|
|
|
use function mb_strtoupper;
|
2022-12-24 22:10:40 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class for searches into arrays.
|
|
|
|
*
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
*/
|
|
|
|
abstract class AbstractSearcher extends AbstractRule
|
|
|
|
{
|
|
|
|
use CanValidateUndefined;
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
/**
|
|
|
|
* @param mixed $input
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
|
|
|
abstract protected function getDataSource($input = null): array;
|
|
|
|
|
2022-12-24 22:10:40 +08:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function validate($input): bool
|
|
|
|
{
|
2024-01-31 22:15:08 +08:00
|
|
|
$dataSource = $this->getDataSource($input);
|
|
|
|
|
2022-12-24 22:10:40 +08:00
|
|
|
if ($this->isUndefined($input) && empty($dataSource)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
if (!is_scalar($input)) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-12-24 22:10:40 +08:00
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
return in_array(mb_strtoupper((string) $input), $dataSource, true);
|
|
|
|
}
|
2022-12-24 22:10:40 +08:00
|
|
|
}
|