ListWith.php
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace League\Flysystem\Plugin;
class ListWith extends AbstractPlugin
{
/**
* Get the method name.
*
* @return string
*/
public function getMethod()
{
return 'listWith';
}
/**
* List contents with metadata.
*
* @param string[] $keys
* @param string $directory
* @param bool $recursive
*
* @return array listing with metadata
*/
public function handle(array $keys = [], $directory = '', $recursive = false)
{
$contents = $this->filesystem->listContents($directory, $recursive);
foreach ($contents as $index => $object) {
if ($object['type'] === 'file') {
$missingKeys = array_diff($keys, array_keys($object));
$contents[$index] = array_reduce($missingKeys, [$this, 'getMetadataByName'], $object);
}
}
return $contents;
}
/**
* Get a meta-data value by key name.
*
* @param array $object
* @param string $key
*
* @return array
*/
protected function getMetadataByName(array $object, $key)
{
$method = 'get' . ucfirst($key);
if ( ! method_exists($this->filesystem, $method)) {
throw new \InvalidArgumentException('Could not get meta-data for key: ' . $key);
}
$object[$key] = $this->filesystem->{$method}($object['path']);
return $object;
}
}