AbstractSearcher.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /*
  3. * Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
  4. * SPDX-License-Identifier: MIT
  5. */
  6. declare(strict_types=1);
  7. namespace Respect\Validation\Rules;
  8. use Respect\Validation\Helpers\CanValidateUndefined;
  9. use function in_array;
  10. use function is_scalar;
  11. use function mb_strtoupper;
  12. /**
  13. * Abstract class for searches into arrays.
  14. *
  15. * @author Henrique Moody <henriquemoody@gmail.com>
  16. */
  17. abstract class AbstractSearcher extends AbstractRule
  18. {
  19. use CanValidateUndefined;
  20. /**
  21. * @param mixed $input
  22. * @return mixed[]
  23. */
  24. abstract protected function getDataSource($input = null): array;
  25. /**
  26. * {@inheritDoc}
  27. */
  28. public function validate($input): bool
  29. {
  30. $dataSource = $this->getDataSource($input);
  31. if ($this->isUndefined($input) && empty($dataSource)) {
  32. return true;
  33. }
  34. if (!is_scalar($input)) {
  35. return false;
  36. }
  37. return in_array(mb_strtoupper((string) $input), $dataSource, true);
  38. }
  39. }