GetWithMetadata.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace League\Flysystem\Plugin;
  3. use InvalidArgumentException;
  4. use League\Flysystem\FileNotFoundException;
  5. class GetWithMetadata extends AbstractPlugin
  6. {
  7. /**
  8. * Get the method name.
  9. *
  10. * @return string
  11. */
  12. public function getMethod()
  13. {
  14. return 'getWithMetadata';
  15. }
  16. /**
  17. * Get metadata for an object with required metadata.
  18. *
  19. * @param string $path path to file
  20. * @param string[] $metadata metadata keys
  21. *
  22. * @throws InvalidArgumentException
  23. * @throws FileNotFoundException
  24. *
  25. * @return array|false metadata
  26. */
  27. public function handle($path, array $metadata)
  28. {
  29. $object = $this->filesystem->getMetadata($path);
  30. if ( ! $object) {
  31. return false;
  32. }
  33. $keys = array_diff($metadata, array_keys($object));
  34. foreach ($keys as $key) {
  35. if ( ! method_exists($this->filesystem, $method = 'get' . ucfirst($key))) {
  36. throw new InvalidArgumentException('Could not fetch metadata: ' . $key);
  37. }
  38. $object[$key] = $this->filesystem->{$method}($path);
  39. }
  40. return $object;
  41. }
  42. }