Slug.php 885 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 is_string;
  9. use function mb_strstr;
  10. use function preg_match;
  11. /**
  12. * Validates whether the input is a valid slug.
  13. *
  14. * @author Carlos André Ferrari <caferrari@gmail.com>
  15. * @author Danilo Correa <danilosilva87@gmail.com>
  16. * @author Henrique Moody <henriquemoody@gmail.com>
  17. * @author Nick Lombard <github@jigsoft.co.za>
  18. */
  19. final class Slug extends AbstractRule
  20. {
  21. /**
  22. * {@inheritDoc}
  23. */
  24. public function validate($input): bool
  25. {
  26. if (!is_string($input) || mb_strstr($input, '--')) {
  27. return false;
  28. }
  29. if (!preg_match('@^[0-9a-z\-]+$@', $input)) {
  30. return false;
  31. }
  32. return preg_match('@^-|-$@', $input) === 0;
  33. }
  34. }