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

43 lines
1.1 KiB
PHP

<?php
namespace IO\Filesystem;
use IO\Exception\DirectoryNotFoundException;
use IO\Exception\DirectoryNotReadableException;
use IO\Exception\NotADirectoryException;
class Directory
{
private string $directory;
private function __construct(string $directory)
{
$this->directory = rtrim($directory, DIRECTORY_SEPARATOR);
$this->guardUnusableDirectory($directory);
$this->directory = realpath($directory);
}
public static function fromString(?string $directory): self
{
return new self($directory ?? getcwd());
}
public function __toString(): string
{
return $this->directory;
}
private function guardUnusableDirectory(string $directory): void
{
if (!file_exists($directory)) {
throw new DirectoryNotFoundException($directory);
}
if (!is_dir($directory)) {
throw new NotADirectoryException($directory);
}
if (!is_readable($directory)) {
throw new DirectoryNotReadableException($directory);
}
}
}