Subset.php 901 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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_diff;
  9. use function is_array;
  10. /**
  11. * Validates whether the input is a subset of a given value.
  12. *
  13. * @author Henrique Moody <henriquemoody@gmail.com>
  14. * @author Singwai Chan <singwai.chan@live.com>
  15. */
  16. final class Subset extends AbstractRule
  17. {
  18. /**
  19. * @var mixed[]
  20. */
  21. private $superset;
  22. /**
  23. * Initializes the rule.
  24. *
  25. * @param mixed[] $superset
  26. */
  27. public function __construct(array $superset)
  28. {
  29. $this->superset = $superset;
  30. }
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function validate($input): bool
  35. {
  36. if (!is_array($input)) {
  37. return false;
  38. }
  39. return array_diff($input, $this->superset) === [];
  40. }
  41. }