pdf-finder/src/IO/Filesystem/File.php

45 lines
939 B
PHP

<?php
namespace IO\Filesystem;
use IO\Exception\FileNotFoundException;
use IO\Exception\FileNotReadableException;
use SplFileInfo;
class File
{
private SplFileInfo $info;
private function __construct(string $filepath)
{
$this->guardUnusableFile($filepath);
$filepath = realpath($filepath);
$this->info = new SplFileInfo($filepath);
}
public static function fromString(string $filepath): self
{
return new self($filepath);
}
public function getInfo(): SplFileInfo
{
return $this->info;
}
public function __toString(): string
{
return (string)$this->getInfo();
}
private function guardUnusableFile(string $file): void
{
if (!file_exists($file)) {
throw new FileNotFoundException($file);
}
if (!is_readable($file)) {
throw new FileNotReadableException($file);
}
}
}