fs
1<?php
2
3declare(strict_types=1);
4
5namespace Upside\Std\Fs;
6
7final class File implements \Stringable
8{
9private string $path;
10
11public function __construct(string $path)
12{
13$this->path = $path;
14}
15
16public function __toString(): string
17{
18return $this->path;
19}
20
21public static function from(string $path): self
22{
23return new self($path);
24}
25
26public function rename(string $to): File
27{
28$new_file = self::from($to);
29
30if (!\rename($this->path, $to)) {
31throw new \RuntimeException(\sprintf('File "%s" was not renamed to "%s"', $this->path, $to));
32}
33
34return $new_file;
35}
36
37public function remove(): self
38{
39return $this->unlink($this->path);
40}
41
42private function unlink(string $file): self
43{
44if (!\unlink($file)) {
45throw new \RuntimeException(\sprintf('File "%s" was not removed', $file));
46}
47
48return $this;
49}
50
51public function dump(mixed $data): self
52{
53if(\file_put_contents($this->path, $data) === false){
54throw new \RuntimeException(\sprintf('Can\'t to write to the file %s.', $this->path));
55}
56
57return $this;
58}
59}