ContainsAny.php 1.2 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 function array_map;
  9. /**
  10. * Validates if the input contains at least one of defined values
  11. *
  12. * @author Henrique Moody <henriquemoody@gmail.com>
  13. * @author Kirill Dlussky <kirill@dlussky.ru>
  14. */
  15. final class ContainsAny extends AbstractEnvelope
  16. {
  17. /**
  18. * Initializes the rule.
  19. *
  20. * @param mixed[] $needles At least one of the values provided must be found in input string or array
  21. * @param bool $identical Defines whether the value should be compared strictly, when validating array
  22. */
  23. public function __construct(array $needles, bool $identical = false)
  24. {
  25. parent::__construct(
  26. new AnyOf(...$this->getRules($needles, $identical)),
  27. ['needles' => $needles]
  28. );
  29. }
  30. /**
  31. * @param mixed[] $needles
  32. *
  33. * @return Contains[]
  34. */
  35. private function getRules(array $needles, bool $identical): array
  36. {
  37. return array_map(
  38. static function ($needle) use ($identical): Contains {
  39. return new Contains($needle, $identical);
  40. },
  41. $needles
  42. );
  43. }
  44. }