NoWhitespace.php 899 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_null;
  9. use function is_scalar;
  10. use function preg_match;
  11. /**
  12. * Validates whether a string contains no whitespace (spaces, tabs and line breaks).
  13. *
  14. * @author Alexandre Gomes Gaigalas <alganet@gmail.com>
  15. * @author Augusto Pascutti <augusto@phpsp.org.br>
  16. * @author Danilo Benevides <danilobenevides01@gmail.com>
  17. * @author Henrique Moody <henriquemoody@gmail.com>
  18. */
  19. final class NoWhitespace extends AbstractRule
  20. {
  21. /**
  22. * {@inheritDoc}
  23. */
  24. public function validate($input): bool
  25. {
  26. if (is_null($input)) {
  27. return true;
  28. }
  29. if (is_scalar($input) === false) {
  30. return false;
  31. }
  32. return !preg_match('#\s#', (string) $input);
  33. }
  34. }