| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPIDE\App\Services\Storage; |
| 4 |
|
| 5 |
use WPIDE\App\Utils\Collection; |
| 6 |
|
| 7 |
class DirectoryCollection implements \JsonSerializable |
| 8 |
{ |
| 9 |
use Collection; |
| 10 |
|
| 11 |
protected $location; |
| 12 |
|
| 13 |
public function __construct($location) |
| 14 |
{ |
| 15 |
$this->location = $location; |
| 16 |
} |
| 17 |
|
| 18 |
public function addFile(string $type, array $dir_info, string $path, string $dir, string $mime, string $name, int $size, int $timestamp) |
| 19 |
{ |
| 20 |
if (! in_array($type, ['dir', 'file', 'back'])) { |
| 21 |
throw new \Exception('Invalid file type.'); |
| 22 |
} |
| 23 |
|
| 24 |
$ext = pathinfo($path, PATHINFO_EXTENSION); |
| 25 |
|
| 26 |
$entry = [ |
| 27 |
'id' => md5($path), |
| 28 |
'type' => $type, |
| 29 |
'path' => $path, |
| 30 |
'dir' => $dir, |
| 31 |
'ext' => $ext, |
| 32 |
'mime' => $mime, |
| 33 |
'name' => $name, |
| 34 |
'size' => $size, |
| 35 |
'time' => $timestamp, |
| 36 |
]; |
| 37 |
|
| 38 |
if($type === 'dir') { |
| 39 |
$entry = array_merge($entry, $dir_info); |
| 40 |
} |
| 41 |
|
| 42 |
$this->add($entry); |
| 43 |
} |
| 44 |
|
| 45 |
public function resetTimestamps($timestamp = 0) |
| 46 |
{ |
| 47 |
foreach ($this->items as &$item) { |
| 48 |
$item['time'] = $timestamp; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
public function jsonSerialize(): array |
| 53 |
{ |
| 54 |
$this->sortByValue('type'); |
| 55 |
|
| 56 |
return [ |
| 57 |
'location' => $this->location, |
| 58 |
'files' => $this->items, |
| 59 |
]; |
| 60 |
} |
| 61 |
} |
| 62 |
|