1+ <?php
2+
3+ /**
4+ * FireFS - Easily manage your filesystem, through PHP
5+ *
6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7+ * of this software and associated documentation files (the "Software"), to deal
8+ * in the Software without restriction, including without limitation the rights
9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ * copies of the Software, and to permit persons to whom the Software is
11+ * furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in all
14+ * copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ * SOFTWARE.
23+ *
24+ * @category Library
25+ * @package FireFS
26+ * @author Axel Nana <[email protected] > 27+ * @copyright 2018 Aliens Group, Inc.
28+ * @license MIT <https://github.com/ElementaryFramework/FireFS/blob/master/LICENSE>
29+ * @version GIT: 0.0.1
30+ * @link http://firefs.na2axl.tk
31+ */
32+
33+ namespace ElementaryFramework \FireFS ;
34+
35+ use ElementaryFramework \FireFS \Exceptions \FileSystemEntityNotFoundException ;
36+
37+ abstract class FileSystemEntity
38+ {
39+ /**
40+ * @var string
41+ */
42+ protected $ _path ;
43+
44+ /**
45+ * @var FireFS
46+ */
47+ protected $ _fs ;
48+
49+ /**
50+ * FileSystemEntity constructor.
51+ *
52+ * @param string $path The path to the FS entity.
53+ * @param FireFS $fs The filesystem instance.
54+ *
55+ * @throws FileSystemEntityNotFoundException When the entity was not found on the given filesystem.
56+ */
57+ public function __construct (string $ path , FireFS &$ fs )
58+ {
59+ $ this ->_path = $ path ;
60+ $ this ->_fs =& $ fs ;
61+
62+ if (!$ this ->_fs ->exists ($ this ->_path )) {
63+ throw new FileSystemEntityNotFoundException ($ this ->_path );
64+ }
65+ }
66+
67+ /**
68+ * Gets the path to this entity.
69+ *
70+ * @return string
71+ */
72+ public function getPath (): string
73+ {
74+ return $ this ->_path ;
75+ }
76+
77+ /**
78+ * Gets the name of this entity.
79+ *
80+ * @return string
81+ */
82+ public function getName (): string
83+ {
84+ return $ this ->_fs ->basename ($ this ->_path );
85+ }
86+
87+ /**
88+ * Creates this entity on the file system.
89+ *
90+ * @return bool
91+ */
92+ public abstract function create (): bool ;
93+
94+ /**
95+ * Deletes this entity from the file system.
96+ *
97+ * @return bool
98+ */
99+ public function delete (): bool
100+ {
101+ return $ this ->_fs ->delete ($ this ->_path , true );
102+ }
103+
104+ /**
105+ * Renames the entity.
106+ *
107+ * @param string $newName The new name of the entity.
108+ *
109+ * @return bool
110+ */
111+ public function rename (string $ newName ): bool
112+ {
113+ return $ this ->_fs ->rename ($ this ->_path , $ newName );
114+ }
115+
116+ /**
117+ * Creates a copy of this entity into the given folder.
118+ *
119+ * @param Folder $folder The folder which will contain the copy;
120+ *
121+ * @return bool
122+ */
123+ public function copyToFolder (Folder $ folder ): bool
124+ {
125+ return $ this ->copyToPath ($ folder ->_path );
126+ }
127+
128+ /**
129+ * Creates a copy of this entity.
130+ *
131+ * @param string $path The path of the copy of the entity
132+ *
133+ * @return bool
134+ */
135+ public function copyToPath (string $ path ): bool
136+ {
137+ return $ this ->_fs ->copy ($ this ->_path , $ path );
138+ }
139+
140+ /**
141+ * Moves this entity into another folder.
142+ *
143+ * @param Folder $folder The new folder of the entity.
144+ *
145+ * @return bool
146+ */
147+ public function moveToFolder (Folder $ folder ): bool
148+ {
149+ return $ this ->moveToPath ($ folder ->_path );
150+ }
151+
152+ /**
153+ * Moves this entity to another path.
154+ *
155+ * @param string $path The new path of the entity
156+ *
157+ * @return bool
158+ */
159+ public function moveToPath (string $ path ): bool
160+ {
161+ return $ this ->_fs ->move ($ this ->_path , $ path );
162+ }
163+
164+ /**
165+ * Gets the internal path to this entity.
166+ *
167+ * @return string
168+ */
169+ public function getInternalPath (): string
170+ {
171+ return $ this ->_fs ->toInternalPath ($ this ->_path );
172+ }
173+
174+ /**
175+ * Gets the external path to this entity.
176+ *
177+ * @return string
178+ */
179+ public function getExternalPath (): string
180+ {
181+ return $ this ->_fs ->toExternalPath ($ this ->_path );
182+ }
183+
184+ /**
185+ * Gets the filesystem path to this entity.
186+ *
187+ * @return string
188+ */
189+ public function getFileSystemPath (): string
190+ {
191+ return $ this ->_fs ->toFileSystemPath ($ this ->_path );
192+ }
193+
194+ /**
195+ * Gets the parent folder which contains this entity.
196+ *
197+ * @throws FileSystemEntityNotFoundException
198+ *
199+ * @return Folder
200+ */
201+ public function getParent (): Folder
202+ {
203+ return new Folder ($ this ->_fs ->dirname ($ this ->_path ), $ this ->_fs );
204+ }
205+
206+ public function getSize (): int
207+ {
208+ return $ this ->_fs ->size ($ this ->_path );
209+ }
210+
211+ public function getSizeInOctets (): string
212+ {
213+ return $ this ->_fs ->sizeInOctets ($ this ->_path );
214+ }
215+
216+ public function getLastModificationTime (): int
217+ {
218+ return $ this ->_fs ->lastModTime ($ this ->_path );
219+ }
220+
221+ public function getLastAccessTime (): int
222+ {
223+ return $ this ->_fs ->lastAccessTime ($ this ->_path );
224+ }
225+
226+ /**
227+ * Checks if the current entity is stored
228+ * in a remote filesystem.
229+ *
230+ * @return bool
231+ */
232+ public function isRemote (): bool
233+ {
234+ return $ this ->_fs ->isRemote ($ this ->_path );
235+ }
236+ }
0 commit comments