|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of graze/data-file |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license https://github.com/graze/data-file/blob/master/LICENSE.md |
|
11
|
|
|
* @link https://github.com/graze/data-file |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\DataFile\Node; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\DataNode\NodeCollection; |
|
17
|
|
|
use InvalidArgumentException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class FileNodeCollection |
|
21
|
|
|
* |
|
22
|
|
|
* @package Graze\DataFile\Node\File |
|
23
|
|
|
*/ |
|
24
|
|
|
class FileNodeCollection extends NodeCollection implements FileNodeCollectionInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* For a given set of files, return any common prefix (i.e. directory, s3 key) |
|
28
|
|
|
* |
|
29
|
|
|
* @return string|null |
|
30
|
|
|
*/ |
|
31
|
3 |
|
public function getCommonPrefix() |
|
32
|
|
|
{ |
|
33
|
3 |
|
if ($this->count() == 0) { |
|
34
|
1 |
|
return null; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
2 |
|
$commonPath = $this->reduce(function ($commonPath, FileNodeInterface $file) { |
|
38
|
2 |
|
if (is_null($commonPath)) { |
|
39
|
2 |
|
return $file->getPath(); |
|
40
|
|
|
} |
|
41
|
2 |
|
return $this->getCommonPrefixString($commonPath, $file->getPath()); |
|
42
|
2 |
|
}); |
|
43
|
|
|
|
|
44
|
2 |
|
return (strlen($commonPath) > 0) ? $commonPath : null; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $left |
|
49
|
|
|
* @param string $right |
|
50
|
|
|
* |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
2 |
|
private function getCommonPrefixString($left, $right) |
|
54
|
|
|
{ |
|
55
|
2 |
|
for ($i = 1; $i < strlen($left); $i++) { |
|
56
|
2 |
|
if (substr_compare($left, $right, 0, $i) !== 0) { |
|
57
|
2 |
|
return substr($left, 0, $i - 1); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
1 |
|
return substr($left, 0, $i); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param mixed $value |
|
65
|
|
|
* |
|
66
|
|
|
* @return $this |
|
67
|
|
|
*/ |
|
68
|
17 |
|
public function add($value) |
|
69
|
|
|
{ |
|
70
|
17 |
|
if (!($value instanceof FileNodeInterface)) { |
|
71
|
1 |
|
throw new InvalidArgumentException("The specified value does not implement FileNodeInterface"); |
|
72
|
|
|
} |
|
73
|
16 |
|
return parent::add($value); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|