StringVal.php 654 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_object;
  9. use function is_scalar;
  10. use function method_exists;
  11. /**
  12. * Validates whether the input can be used as a string.
  13. *
  14. * @author Danilo Correa <danilosilva87@gmail.com>
  15. * @author Henrique Moody <henriquemoody@gmail.com>
  16. */
  17. final class StringVal extends AbstractRule
  18. {
  19. /**
  20. * {@inheritDoc}
  21. */
  22. public function validate($input): bool
  23. {
  24. return is_scalar($input) || (is_object($input) && method_exists($input, '__toString'));
  25. }
  26. }