-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFileDescriptor.php
100 lines (89 loc) · 2.06 KB
/
FileDescriptor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\file;
use phootwork\file\exception\FileException;
use Stringable;
/**
* Class FileDescriptor
*/
class FileDescriptor implements Stringable {
use FileOperationTrait;
public function __construct(string|Stringable $filename) {
$this->pathname = (string) $filename;
}
/**
* Creates a new FileDescriptor from SplFileInfo
*
* @param \SplFileInfo $fileInfo
*
* @return FileDescriptor
*/
public static function fromFileInfo(\SplFileInfo $fileInfo): self {
return new self($fileInfo->getPathname());
}
/**
* Tells whether this is a regular file
*
* @return bool Returns TRUE if the filename exists and is a regular file, FALSE otherwise.
*/
public function isFile(): bool {
return is_file($this->pathname);
}
/**
* Tells whether the filename is a '.' or '..'
*
* @return bool
*/
public function isDot(): bool {
return $this->getFilename() == '.' || $this->getFilename() == '..';
}
/**
* Tells whether this is a directory
*
* @return bool Returns TRUE if the filename exists and is a directory, FALSE otherwise.
*/
public function isDir(): bool {
return is_dir($this->pathname);
}
/**
* Converts this file descriptor into a file object
*
* @return File
*/
public function toFile(): File {
return new File($this->pathname);
}
/**
* Converts this file descriptor into a directory object
*
* @return Directory
*/
public function toDirectory(): Directory {
return new Directory($this->pathname);
}
/**
* Deletes the file
*
* @throws FileException
*/
public function delete(): void {
if ($this->isDir()) {
$this->toDirectory()->delete();
} else {
$this->toFile()->delete();
}
}
/**
* String representation of this file as pathname
*/
public function __toString(): string {
return $this->pathname;
}
}