1 | <?php |
||
26 | class File |
||
27 | { |
||
28 | /** |
||
29 | * File extension separator |
||
30 | * @const string |
||
31 | */ |
||
32 | const EXTENSION_SEPARATOR = '.'; |
||
33 | |||
34 | /** |
||
35 | * File's name including the path |
||
36 | * @var null|string |
||
37 | */ |
||
38 | private $filenameWithPath = null; |
||
39 | |||
40 | /** |
||
41 | * File's extension - For no extension a blank string will be used |
||
42 | * @var null|string |
||
43 | */ |
||
44 | private $extension = null; |
||
45 | |||
46 | /** |
||
47 | * File's name without extension |
||
48 | * @var null|string |
||
49 | */ |
||
50 | private $name = null; |
||
51 | |||
52 | /** |
||
53 | * Whether the file has an extension or if it is set by us |
||
54 | * @var bool |
||
55 | */ |
||
56 | private $hasExtension = false; |
||
57 | |||
58 | /** |
||
59 | * @var null|ContentFactory |
||
60 | */ |
||
61 | private $contentFactory = null; |
||
62 | |||
63 | /** |
||
64 | * File constructor. |
||
65 | * |
||
66 | * @param string $filenameWithPath |
||
67 | */ |
||
68 | 41 | public function __construct(string $filenameWithPath) |
|
69 | { |
||
70 | 41 | $pathHelper = new PathHelper(); |
|
71 | 41 | $this->filenameWithPath = $pathHelper->normalizePathSeparator($filenameWithPath); |
|
72 | 41 | $filename = $pathHelper->extractFilenameFromPath($this->filenameWithPath); |
|
73 | 41 | if (empty($filename) || false === $this->validatePath($this->filenameWithPath)) { |
|
74 | 2 | throw new \RuntimeException( |
|
75 | 2 | sprintf('Given path %s does not represents a file!', $filenameWithPath) |
|
76 | ); |
||
77 | } |
||
78 | 39 | list($this->name, $this->extension) = $pathHelper->splitFilename($filename); |
|
79 | 39 | $this->hasExtension = (bool)$this->extension; |
|
80 | 39 | } |
|
81 | |||
82 | /** |
||
83 | * States whether the file actually exists on disk |
||
84 | * @return bool |
||
85 | */ |
||
86 | 40 | public function exists(): bool |
|
90 | |||
91 | /** |
||
92 | * States whether the file is readable |
||
93 | * @return bool |
||
94 | */ |
||
95 | 6 | public function isReadable(): bool |
|
99 | |||
100 | /** |
||
101 | * @return bool |
||
102 | */ |
||
103 | 7 | public function isWritable(): bool |
|
112 | |||
113 | /** |
||
114 | * Write content to the current file |
||
115 | * |
||
116 | * @param \NeedleProject\FileIo\Content\ContentInterface $content |
||
117 | * @return \NeedleProject\FileIo\File |
||
118 | * @throws \NeedleProject\FileIo\Exception\PermissionDeniedException |
||
119 | */ |
||
120 | 3 | public function write(ContentInterface $content): File |
|
128 | |||
129 | /** |
||
130 | * @return \NeedleProject\FileIo\Content\ContentInterface |
||
131 | * @throws \NeedleProject\FileIo\Exception\FileNotFoundException |
||
132 | * @throws \NeedleProject\FileIo\Exception\IOException |
||
133 | * @throws \NeedleProject\FileIo\Exception\PermissionDeniedException |
||
134 | */ |
||
135 | 6 | public function getContent(): ContentInterface |
|
156 | |||
157 | /** |
||
158 | * Deletes the current file |
||
159 | * @return bool |
||
160 | * @throws \NeedleProject\FileIo\Exception\IOException |
||
161 | */ |
||
162 | 3 | public function delete(): bool |
|
172 | |||
173 | /** |
||
174 | * State existence of a file's extension |
||
175 | * @return bool |
||
176 | */ |
||
177 | 10 | public function hasExtension(): bool |
|
181 | |||
182 | /** |
||
183 | * Get file's extension |
||
184 | * @return string |
||
185 | */ |
||
186 | 4 | public function getExtension(): string |
|
190 | |||
191 | /** |
||
192 | * Get file's name without extension |
||
193 | * @return string |
||
194 | */ |
||
195 | 4 | public function getName(): string |
|
199 | |||
200 | /** |
||
201 | * Get file's name with extension |
||
202 | * @return string |
||
203 | */ |
||
204 | 5 | public function getBasename(): string |
|
211 | |||
212 | /** |
||
213 | * Returns a factory responsible for creating appropriate content |
||
214 | * @return \NeedleProject\FileIo\Factory\ContentFactory |
||
215 | */ |
||
216 | 1 | protected function getContentFactory(): ContentFactory |
|
223 | |||
224 | /** |
||
225 | * Validate if the given path is not a directory |
||
226 | * @param string $filenameWithPath |
||
227 | * @return bool |
||
228 | */ |
||
229 | 40 | private function validatePath(string $filenameWithPath): bool |
|
236 | } |
||
237 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.