1 | <?php |
||
16 | class FileCollection implements \Iterator, \ArrayAccess, FileInterface, \Countable |
||
17 | { |
||
18 | /** |
||
19 | * An array holding all paths in this collection |
||
20 | * @var array |
||
21 | */ |
||
22 | private $paths; |
||
23 | |||
24 | /** |
||
25 | * Current index of the iterator in the list of paths. |
||
26 | * @var int |
||
27 | */ |
||
28 | private $iteratorIndex; |
||
29 | |||
30 | /** |
||
31 | * A lightweight cache that holds instances of file objects created. |
||
32 | * @var array<FileInterface> |
||
33 | */ |
||
34 | private $instances; |
||
35 | |||
36 | /** |
||
37 | * FileCollection constructor. |
||
38 | * @param $paths |
||
39 | */ |
||
40 | 5 | public function __construct($paths) |
|
45 | |||
46 | /** |
||
47 | * Get an instance of a file object for a path in the collection. |
||
48 | * This is used whenever an item must be returned from the collection. |
||
49 | * |
||
50 | * @param $index |
||
51 | * @return mixed |
||
52 | */ |
||
53 | 5 | private function getInstance($index) |
|
64 | |||
65 | 5 | public function rewind() |
|
69 | |||
70 | 5 | public function current() |
|
74 | |||
75 | 1 | public function key() |
|
79 | |||
80 | 5 | public function next() |
|
84 | |||
85 | 5 | public function valid() |
|
89 | |||
90 | public function offsetSet($index, $path) |
||
99 | |||
100 | public function offsetExists($index) |
||
104 | |||
105 | public function offsetGet($index) |
||
109 | |||
110 | public function offsetUnset($index) |
||
114 | |||
115 | 1 | public function moveTo(string $destination, int $ovewrite = 0): void |
|
116 | { |
||
117 | 1 | foreach($this as $file) { |
|
118 | 1 | $file->moveTo($destination . DIRECTORY_SEPARATOR . basename($file), $ovewrite); |
|
119 | } |
||
120 | 1 | } |
|
121 | |||
122 | 1 | public function copyTo(string $destination, int $ovewrite = 0): void |
|
123 | { |
||
124 | 1 | foreach($this as $file) { |
|
125 | 1 | $file->copyTo($destination . DIRECTORY_SEPARATOR . basename($file), $ovewrite); |
|
126 | } |
||
127 | 1 | } |
|
128 | |||
129 | 1 | public function getSize(): int |
|
130 | { |
||
131 | 1 | return array_reduce(iterator_to_array($this), |
|
132 | function($carry, $item){ |
||
133 | 1 | return $carry + $item->getSize(); |
|
134 | 1 | }, 0); |
|
135 | } |
||
136 | |||
137 | 2 | public function delete(): void |
|
143 | |||
144 | public function getPath(): string |
||
145 | { |
||
146 | return array_reduce($this->paths, |
||
147 | function($carry, $path) { |
||
148 | $carry .= escapeshellarg($path) . " "; |
||
149 | }, ""); |
||
150 | } |
||
151 | |||
152 | 1 | public function count() |
|
156 | } |
||
157 |