47 lines
1.1 KiB
PHP
47 lines
1.1 KiB
PHP
|
<?php
|
||
|
namespace Common\Alias;
|
||
|
|
||
|
use function Safe\{fopen,fclose,fwrite};
|
||
|
use Common\Concept\File as FileInterface;
|
||
|
|
||
|
abstract class File implements FileInterface
|
||
|
{
|
||
|
protected string $filename;
|
||
|
public function setFilename(string $filename): FileInterface
|
||
|
{
|
||
|
$this->filename = $filename;
|
||
|
return $this;
|
||
|
}
|
||
|
public function getFilename(): string
|
||
|
{
|
||
|
return $this->filename;
|
||
|
}
|
||
|
|
||
|
public function isDir(): bool
|
||
|
{
|
||
|
return is_dir($this->getFilename());
|
||
|
}
|
||
|
public function isReadable(): bool
|
||
|
{
|
||
|
return is_readable($this->getFilename());
|
||
|
}
|
||
|
public function isWriteable(): bool
|
||
|
{
|
||
|
return is_writeable($this->getFilename());
|
||
|
}
|
||
|
|
||
|
public function read(?int $length = null): string
|
||
|
{
|
||
|
$fh = fopen($this->getFilename(), 'r');
|
||
|
$str = fgets($fh, $length);
|
||
|
fclose($fh);
|
||
|
return $str;
|
||
|
}
|
||
|
public function write(string $data, ?int $length = null): void
|
||
|
{
|
||
|
$fh = fopen($this->getFilename(), 'r');
|
||
|
fwrite($fh, $data, $length);
|
||
|
fclose($fh);
|
||
|
}
|
||
|
}
|