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