Uploaded.php 943 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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\UploadedFileInterface;
  9. use SplFileInfo;
  10. use function is_scalar;
  11. use function is_uploaded_file;
  12. /**
  13. * Validates if the given data is a file that was uploaded via HTTP POST.
  14. *
  15. * @author Henrique Moody <henriquemoody@gmail.com>
  16. * @author Paul Karikari <paulkarikari1@gmail.com>
  17. */
  18. final class Uploaded extends AbstractRule
  19. {
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function validate($input): bool
  24. {
  25. if ($input instanceof SplFileInfo) {
  26. return $this->validate($input->getPathname());
  27. }
  28. if ($input instanceof UploadedFileInterface) {
  29. return true;
  30. }
  31. if (!is_scalar($input)) {
  32. return false;
  33. }
  34. return is_uploaded_file((string) $input);
  35. }
  36. }