1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace ntentan\utils\filesystem; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use ntentan\utils\Filesystem; |
8
|
|
|
|
9
|
|
|
class FileCollection implements \Iterator, \ArrayAccess, FileInterface |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
private $paths; |
12
|
|
|
private $iteratorIndex; |
13
|
|
|
private $instances; |
14
|
|
|
|
15
|
|
|
public function __construct($paths) |
16
|
|
|
{ |
17
|
|
|
$this->paths = $paths; |
18
|
|
|
$this->iteratorIndex = 0; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
private function getInstance($index) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
if(!isset($this->instances[$index])) { |
24
|
|
|
if(is_dir($this->paths[$index])) { |
25
|
|
|
$this->instances[$index] = Filesystem::directory($this->paths[$index]); |
26
|
|
|
} else { |
27
|
|
|
$this->instances[$index] = Filesystem::file($this->paths[$index]); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
return $this->instances[$index]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function rewind() |
34
|
|
|
{ |
35
|
|
|
$this->iteratorIndex = 0; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function current() |
39
|
|
|
{ |
40
|
|
|
return $this->getInstance($this->iteratorIndex); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function key() |
44
|
|
|
{ |
45
|
|
|
return $this->iteratorIndex; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function next() |
49
|
|
|
{ |
50
|
|
|
$this->iteratorIndex++; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function valid() |
54
|
|
|
{ |
55
|
|
|
return isset($this->paths[$this->iteratorIndex]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function offsetSet($index, $path) |
59
|
|
|
{ |
60
|
|
|
if(is_null()) { |
61
|
|
|
$this->paths[] = $path; |
62
|
|
|
} else { |
63
|
|
|
$this->paths[$index] = $path; |
64
|
|
|
unset($this->instances[$index]); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function offsetExists($index) |
69
|
|
|
{ |
70
|
|
|
return isset($this->paths[$index]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function offsetGet($index) |
74
|
|
|
{ |
75
|
|
|
return isset($this->paths[$index]) ? $this->paths[$index] : null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function offsetUnset($index) |
79
|
|
|
{ |
80
|
|
|
unset($this->paths[$index]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function moveTo(string $destination): void |
84
|
|
|
{ |
85
|
|
|
foreach($this as $file) { |
86
|
|
|
$file->moveTo($destination); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getSize(): int |
91
|
|
|
{ |
92
|
|
|
return array_reduce($this, |
93
|
|
|
function($carry, $item){ |
94
|
|
|
$carry += $item->getSize(); |
95
|
|
|
}, 0); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function copyTo(string $destination): void |
99
|
|
|
{ |
100
|
|
|
foreach($this as $file) { |
101
|
|
|
$this->copyTo($destination); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |