56
src/Objects/DirContent.php
Normal file
56
src/Objects/DirContent.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?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]);
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user