Readable.php 851 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 Psr\Http\Message\StreamInterface;
  9. use SplFileInfo;
  10. use function is_readable;
  11. use function is_string;
  12. /**
  13. * Validates if the given data is a file exists and is readable.
  14. *
  15. * @author Danilo Correa <danilosilva87@gmail.com>
  16. * @author Henrique Moody <henriquemoody@gmail.com>
  17. */
  18. final class Readable extends AbstractRule
  19. {
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function validate($input): bool
  24. {
  25. if ($input instanceof SplFileInfo) {
  26. return $input->isReadable();
  27. }
  28. if ($input instanceof StreamInterface) {
  29. return $input->isReadable();
  30. }
  31. return is_string($input) && is_readable($input);
  32. }
  33. }