]> git.mxchange.org Git - core.git/blob - framework/main/classes/file_directories/class_BaseAbstractFile.php
69b15b06e72c62750a7cef99579a1ed09b66a95b
[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) {
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 it to NULL
87                 $this->pointerInstance = NULL;
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 ($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          * Close a file source and set it's instance to null and the file name
155          * to empty
156          *
157          * @return      void
158          */
159         public function closeFile () {
160                 // Debug message
161                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: CALLED!', __METHOD__, __LINE__));
162
163                 // Close down pointer instance as well by unsetting it
164                 $this->unsetPointerInstance();
165
166                 // Debug message
167                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
168         }
169
170         /**
171          * Size of this file
172          *
173          * @return      $size   Size (in bytes) of file
174          * @todo        Handle seekStatus
175          */
176         public function size () {
177                 // Call pointer instance
178                 return $this->getPointerInstance()->size();
179         }
180
181         /**
182          * Read data a file pointer
183          *
184          * @return      mixed   The result of fread()
185          * @throws      NullPointerException    If the file pointer instance
186          *                                                                      is not set by setFileObject()
187          * @throws      InvalidResourceException        If there is being set
188          */
189         public function readFromFile () {
190                 // Call pointer instance
191                 return $this->getPointerInstance()->readFromFile();
192         }
193
194         /**
195          * Write data to a file pointer
196          *
197          * @param       $dataStream             The data stream we shall write to the file
198          * @return      mixed                   Number of writes bytes or false on error
199          * @throws      NullPointerException    If the file pointer instance
200          *                                                                      is not set by setFileObject()
201          * @throws      InvalidResourceException        If there is being set
202          *                                                                                      an invalid file resource
203          */
204         public function writeToFile ($dataStream) {
205                 // Call pointer instance
206                 return $this->getPointerInstance()->writeToFile($dataStream);
207         }
208
209         /**
210          * Determines whether the EOF has been reached
211          *
212          * @return      $isEndOfFileReached             Whether the EOF has been reached
213          */
214         public final function isEndOfFileReached () {
215                 // Call pointer instance
216                 return $this->getPointerInstance()->isEndOfFileReached();
217         }
218
219 }