forked from projects/file-explorer
56 lines
1.1 KiB
PHP
56 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Objects;
|
|
|
|
use SplFileInfo;
|
|
|
|
class DirContent
|
|
{
|
|
private function __construct(
|
|
private readonly string $name,
|
|
private readonly int $size,
|
|
private readonly string $type,
|
|
)
|
|
{
|
|
}
|
|
|
|
public static function make(SplFileInfo $fileInfo): DirContent
|
|
{
|
|
return new self(
|
|
$fileInfo->getBasename(),
|
|
$fileInfo->getSize() ?? 0,
|
|
$fileInfo->getType() ?? 'N/A',
|
|
);
|
|
}
|
|
|
|
public function getName(): string
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
public function getSize(bool $humanReadable = true): int|string
|
|
{
|
|
if (!$humanReadable) {
|
|
return $this->size;
|
|
}
|
|
|
|
return $this->getHumanReadableSize();
|
|
}
|
|
|
|
public function getType(): string
|
|
{
|
|
return $this->type;
|
|
}
|
|
|
|
private function getHumanReadableSize()
|
|
{
|
|
$bytes = $this->size;
|
|
$size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
|
|
$factor = floor((strlen((string) $bytes) - 1) / 3);
|
|
|
|
return sprintf("%.1f %s", $bytes / (1024 ** $factor), $size[$factor]);
|
|
|
|
}
|
|
} |