HighSpeaker/vendor/workerman/validation/library/Rules/AbstractSearcher.php

51 lines
1.0 KiB
PHP
Raw Normal View History

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