drupol /
phpvfs
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace drupol\phpvfs\Node; |
||
| 6 | |||
| 7 | use drupol\phpvfs\Utils\Path; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Class File. |
||
| 11 | */ |
||
| 12 | class File extends FilesystemNode implements FileInterface |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * File constructor. |
||
| 16 | * |
||
| 17 | * @param array $attributes |
||
| 18 | */ |
||
| 19 | 19 | public function __construct( |
|
| 20 | array $attributes = [] |
||
| 21 | ) { |
||
| 22 | $attributes += [ |
||
| 23 | 19 | 'content' => '', |
|
| 24 | 'position' => 0, |
||
| 25 | ]; |
||
| 26 | |||
| 27 | 19 | parent::__construct($attributes, null); |
|
| 28 | 19 | } |
|
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $id |
||
| 32 | * @param string $content |
||
| 33 | * @param array $attributes |
||
| 34 | * |
||
| 35 | * @throws \Exception |
||
| 36 | * |
||
| 37 | * @return \drupol\phpvfs\Node\File |
||
| 38 | */ |
||
| 39 | 17 | public static function create(string $id, string $content = '', array $attributes = []) |
|
| 40 | { |
||
| 41 | 17 | $path = Path::fromString($id); |
|
| 42 | |||
| 43 | 17 | $dirname = $path->dirname(); |
|
| 44 | |||
| 45 | 17 | $basedir = null; |
|
| 46 | |||
| 47 | 17 | if ('' !== $dirname && '.' !== $dirname) { |
|
| 48 | 15 | $basedir = Directory::create($dirname); |
|
| 49 | } |
||
| 50 | |||
| 51 | $attributes = [ |
||
| 52 | 17 | 'id' => $path->basename(), |
|
| 53 | 17 | 'content' => $content, |
|
| 54 | 17 | ] + $attributes; |
|
| 55 | |||
| 56 | 17 | $file = new self($attributes); |
|
| 57 | |||
| 58 | 17 | if (null !== $basedir) { |
|
| 59 | 15 | $basedir->add($file); |
|
| 60 | } |
||
| 61 | |||
| 62 | 17 | return $file; |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | 3 | public function read(): string |
|
| 69 | { |
||
| 70 | 3 | return $this->getAttribute('content'); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param string $data |
||
| 75 | * |
||
| 76 | * @return \drupol\phpvfs\Node\FileInterface |
||
| 77 | */ |
||
| 78 | 1 | public function write(string $data): FileInterface |
|
| 79 | { |
||
| 80 | 1 | $this->setAttribute('content', $data); |
|
| 81 | |||
| 82 | 1 | return $this; |
|
| 83 | } |
||
| 84 | } |
||
| 85 |