PhpLabel.php 755 B

12345678910111213141516171819202122232425262728293031
  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 preg_match;
  10. /**
  11. * Validates if a value is considered a valid PHP Label, so that it can be used as a variable, function or class name.
  12. *
  13. * @author Danilo Correa <danilosilva87@gmail.com>
  14. * @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
  15. * @author Henrique Moody <henriquemoody@gmail.com>
  16. */
  17. final class PhpLabel extends AbstractRule
  18. {
  19. /**
  20. * {@inheritDoc}
  21. */
  22. public function validate($input): bool
  23. {
  24. return is_string($input) && preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $input);
  25. }
  26. }