]> git.mxchange.org Git - core.git/blob - framework/main/classes/file_directories/class_BaseAbstractFile.php
d9cb807a6b0e81f5048d207e9316e88f9cbde556
[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 <<<<<<< HEAD:framework/main/classes/file_directories/class_BaseAbstractFile.php
17  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 =======
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
20 >>>>>>> Some updates::inc/main/classes/file_directories/class_BaseAbstractFile.php
21  * @license             GNU GPL 3.0 or any newer version
22  * @link                http://www.ship-simu.org
23  *
24  * This program is free software: you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation, either version 3 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36  */
37 abstract class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, CloseableFile {
38         /**
39          * Counter for total entries
40          */
41         private $totalEntries = 0;
42
43         /**
44          * Protected constructor
45          *
46          * @param       $className      Name of the class
47          * @return      void
48          */
49         protected function __construct ($className) {
50                 // Call parent constructor
51                 parent::__construct($className);
52         }
53
54         /**
55          * Destructor for cleaning purposes, etc
56          *
57          * @return      void
58          */
59         public final function __destruct() {
60                 // Try to close a file
61                 $this->closeFile();
62
63                 // Call the parent destructor
64                 parent::__destruct();
65         }
66
67         /**
68          * "Getter" for abstracted file size
69          *
70          * @return      $fileSize       Size of abstracted file
71          */
72         public function getFileSize () {
73                 // Call pointer instanze
74                 return $this->getPointerInstance()->getFileSize();
75         }
76
77         /**
78          * Getter for total entries
79          *
80          * @return      $totalEntries   Total entries in this file
81          */
82         public final function getCounter () {
83                 // Get it
84                 return $this->totalEntries;
85         }
86
87         /**
88          * Setter for total entries
89          *
90          * @param       $totalEntries   Total entries in this file
91          * @return      void
92          */
93         protected final function setCounter ($counter) {
94                 // Set it
95                 $this->totalEntries = $counter;
96         }
97
98         /**
99          * Increment counter
100          *
101          * @return      void
102          */
103         protected final function incrementCounter () {
104                 // Get it
105                 $this->totalEntries++;
106         }
107
108         /**
109          * Getter for the file object
110          *
111          * @return      $fileObject             An instance of a SplFileObject
112          * @throws      UnsupportedOperationException   If this method is called
113          */
114         public final function getFileObject () {
115                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
116         }
117
118         /**
119          * Close a file source and set it's instance to null and the file name
120          * to empty
121          *
122          * @return      void
123          */
124         public function closeFile () {
125                 // Debug message
126                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: CALLED!', __METHOD__, __LINE__));
127
128                 // Close down pointer instance as well by unsetting it
129                 $this->unsetPointerInstance();
130
131                 // Debug message
132                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
133         }
134
135         /**
136          * Size of this file
137          *
138          * @return      $size   Size (in bytes) of file
139          * @todo        Handle seekStatus
140          */
141         public function size () {
142                 // Call pointer instance
143                 return $this->getPointerInstance()->size();
144         }
145
146         /**
147          * Read data a file pointer
148          *
149          * @return      mixed   The result of fread()
150          * @throws      NullPointerException    If the file pointer instance
151          *                                                                      is not set by setPointer()
152          * @throws      InvalidResourceException        If there is being set
153          */
154         public function readFromFile () {
155                 // Call pointer instance
156                 return $this->getPointerInstance()->readFromFile();
157         }
158
159         /**
160          * Write data to a file pointer
161          *
162          * @param       $dataStream             The data stream we shall write to the file
163          * @return      mixed                   Number of writes bytes or false on error
164          * @throws      NullPointerException    If the file pointer instance
165          *                                                                      is not set by setPointer()
166          * @throws      InvalidResourceException        If there is being set
167          *                                                                                      an invalid file resource
168          */
169         public function writeToFile ($dataStream) {
170                 // Call pointer instance
171                 return $this->getPointerInstance()->writeToFile($dataStream);
172         }
173
174         /**
175          * Determines whether the EOF has been reached
176          *
177          * @return      $isEndOfFileReached             Whether the EOF has been reached
178          */
179         public final function isEndOfFileReached () {
180                 // Call pointer instance
181                 return $this->getPointerInstance()->isEndOfFileReached();
182         }
183
184 }