76 lines
1.6 KiB
PHP
76 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Objects;
|
|
|
|
use SplFileInfo;
|
|
|
|
readonly class DirContent
|
|
{
|
|
private function __construct(
|
|
private string $name,
|
|
private int $size,
|
|
private string $type,
|
|
private string $path,
|
|
private string $content,
|
|
private string $mimeType,
|
|
) {
|
|
}
|
|
|
|
public static function make(SplFileInfo $fileInfo, string $content = ''): DirContent
|
|
{
|
|
return new self(
|
|
$fileInfo->getBasename(),
|
|
$fileInfo->getSize() ?? 0,
|
|
$fileInfo->getType() ?? 'N/A',
|
|
$fileInfo->getPath(),
|
|
$content,
|
|
mime_content_type($fileInfo->getFilename()),
|
|
);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public function getPath(): string
|
|
{
|
|
return $this->path;
|
|
}
|
|
|
|
public function getContent(): string
|
|
{
|
|
return $this->content;
|
|
}
|
|
|
|
public function getMimeType(): string
|
|
{
|
|
return $this->mimeType;
|
|
}
|
|
|
|
private function getHumanReadableSize(): string
|
|
{
|
|
$bytes = $this->size;
|
|
$size = ['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]);
|
|
|
|
}
|
|
} |