This commit is contained in:
Constantin Simonis 2024-12-05 12:15:07 +01:00
parent a574c3a89a
commit e8e45a4040
Signed by: csimonis
GPG Key ID: 758DD9C506603183

View File

@ -6,14 +6,14 @@ namespace App\Objects;
use SplFileInfo; use SplFileInfo;
class DirContent readonly class DirContent
{ {
private function __construct( private function __construct(
private readonly string $name, private string $name,
private readonly int $size, private int $size,
private readonly string $type, private string $type,
) private string $path,
{ ) {
} }
public static function make(SplFileInfo $fileInfo): DirContent public static function make(SplFileInfo $fileInfo): DirContent
@ -22,6 +22,7 @@ class DirContent
$fileInfo->getBasename(), $fileInfo->getBasename(),
$fileInfo->getSize() ?? 0, $fileInfo->getSize() ?? 0,
$fileInfo->getType() ?? 'N/A', $fileInfo->getType() ?? 'N/A',
$fileInfo->getPath(),
); );
} }
@ -44,10 +45,15 @@ class DirContent
return $this->type; return $this->type;
} }
private function getHumanReadableSize() public function getPath(): string
{
return $this->path;
}
private function getHumanReadableSize(): string
{ {
$bytes = $this->size; $bytes = $this->size;
$size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
$factor = floor((strlen((string) $bytes) - 1) / 3); $factor = floor((strlen((string) $bytes) - 1) / 3);
return sprintf("%.1f %s", $bytes / (1024 ** $factor), $size[$factor]); return sprintf("%.1f %s", $bytes / (1024 ** $factor), $size[$factor]);