]> git.mxchange.org Git - core.git/blob - framework/main/classes/file_directories/class_BaseAbstractFile.php
Continued:
[core.git] / framework / main / classes / file_directories / class_BaseAbstractFile.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Filesystem\File;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\CloseableFile;
7 use Org\Mxchange\CoreFramework\Filesystem\FilePointer;
8 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
9 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
10
11 /**
12  * An abstract file class
13  *
14  * @author              Roland Haeder <webmaster@ship-simu.org>
15  * @version             0.0.0
16  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
17  * @license             GNU GPL 3.0 or any newer version
18  * @link                http://www.ship-simu.org
19  *
20  * This program is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32  */
33 abstract class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, CloseableFile {
34         /**
35          * Counter for total entries
36          */
37         private $totalEntries = 0;
38
39         /**
40          * An instance of a file I/O pointer class (not handler)
41          */
42         private $pointerInstance = NULL;
43
44         /**
45          * Protected constructor
46          *
47          * @param       $className      Name of the class
48          * @return      void
49          */
50         protected function __construct (string $className) {
51                 // Call parent constructor
52                 parent::__construct($className);
53         }
54
55         /**
56          * Setter for FilePointer instance
57          *
58          * @param       $pointerInstance        An instance of an FilePointer class
59          * @return      void
60          */
61         protected final function setPointerInstance (FilePointer $pointerInstance = NULL) {
62                 $this->pointerInstance = $pointerInstance;
63         }
64
65         /**
66          * Getter for FilePointer instance
67          *
68          * @return      $pointerInstance        An instance of an FilePointer class
69          */
70         public final function getPointerInstance () {
71                 return $this->pointerInstance;
72         }
73
74         /**
75          * Unsets pointer instance which triggers a call of __destruct() if the
76          * instance is still there. This is surely not fatal on already "closed"
77          * file pointer instances.
78          *
79          * I don't want to mess around with above setter by giving it a default
80          * value NULL as setter should always explicitly only set (existing) object
81          * instances and NULL is NULL.
82          *
83          * @return      void
84          */
85         protected final function unsetPointerInstance () {
86                 // Simply invoke setter with no parameter
87                 $this->setPointerInstance();
88         }
89
90         /**
91          * Destructor for cleaning purposes, etc
92          *
93          * @return      void
94          */
95         public final function __destruct() {
96                 // Try to close a file
97                 $this->closeFile();
98
99                 // Call the parent destructor
100                 parent::__destruct();
101         }
102
103         /**
104          * "Getter" for abstracted file size
105          *
106          * @return      $fileSize       Size of abstracted file
107          */
108         public function getFileSize () {
109                 // Call pointer instanze
110                 return $this->getPointerInstance()->getFileSize();
111         }
112
113         /**
114          * Getter for total entries
115          *
116          * @return      $totalEntries   Total entries in this file
117          */
118         public final function getCounter () {
119                 // Get it
120                 return $this->totalEntries;
121         }
122
123         /**
124          * Setter for total entries
125          *
126          * @param       $totalEntries   Total entries in this file
127          * @return      void
128          */
129         protected final function setCounter (int $counter) {
130                 // Set it
131                 $this->totalEntries = $counter;
132         }
133
134         /**
135          * Increment counter
136          *
137          * @return      void
138          */
139         protected final function incrementCounter () {
140                 // Get it
141                 $this->totalEntries++;
142         }
143
144         /**
145          * Getter for the file object
146          *
147          * @return      $fileObject             An instance of a SplFileObject
148          */
149         public final function getFileObject () {
150                 return $this->getPointerInstance()->getFileObject();
151         }
152
153         /**
154          * Getter for file's name
155          */
156         public final function getFilename () {
157                 // Invole file object's method
158                 return $this->getFileObject()->getFilename();
159         }
160
161         /**
162          * Close a file source and set it's instance to null and the file name
163          * to empty
164          *
165          * @return      void
166          */
167         public function closeFile () {
168                 // Close down pointer instance as well by unsetting it
169                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-ABSTRACT-FILE: CALLED!');
170                 $this->unsetPointerInstance();
171
172                 // Trace message
173                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-ABSTRACT-FILE: EXIT!');
174         }
175
176         /**
177          * Size of this file
178          *
179          * @return      $size   Size (in bytes) of file
180          * @todo        Handle seekStatus
181          */
182         public function size () {
183                 // Call pointer instance
184                 return $this->getPointerInstance()->size();
185         }
186
187         /**
188          * Read data a file pointer
189          *
190          * @return      mixed   The result of fread()
191          * @throws      NullPointerException    If the file pointer instance
192          *                                                                      is not set by setFileObject()
193          * @throws      InvalidResourceException        If there is being set
194          */
195         public function readFromFile () {
196                 // Call pointer instance
197                 return $this->getPointerInstance()->readFromFile();
198         }
199
200         /**
201          * Write data to a file pointer
202          *
203          * @param       $dataStream             The data stream we shall write to the file
204          * @return      mixed                   Number of writes bytes or false on error
205          * @throws      NullPointerException    If the file pointer instance
206          *                                                                      is not set by setFileObject()
207          * @throws      InvalidResourceException        If there is being set
208          *                                                                                      an invalid file resource
209          */
210         public function writeToFile (string $dataStream) {
211                 // Call pointer instance
212                 return $this->getPointerInstance()->writeToFile($dataStream);
213         }
214
215         /**
216          * Determines whether the EOF has been reached
217          *
218          * @return      $isEndOfFileReached             Whether the EOF has been reached
219          */
220         public final function isEndOfFileReached () {
221                 // Call pointer instance
222                 return $this->getPointerInstance()->isEndOfFileReached();
223         }
224
225 }