Unique.php 747 B

12345678910111213141516171819202122232425262728293031323334353637
  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_unique;
  9. use function is_array;
  10. use const SORT_REGULAR;
  11. /**
  12. * Validates whether the input array contains only unique values.
  13. *
  14. * @author Henrique Moody <henriquemoody@gmail.com>
  15. * @author Krzysztof Śmiałek <admin@avensome.net>
  16. * @author Paul Karikari <paulkarikari1@gmail.com>
  17. */
  18. final class Unique extends AbstractRule
  19. {
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function validate($input): bool
  24. {
  25. if (!is_array($input)) {
  26. return false;
  27. }
  28. return $input == array_unique($input, SORT_REGULAR);
  29. }
  30. }