29517f425c0b0c942cf38585d882d9b9c4572f14
[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 - 2017 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          * Protected constructor
41          *
42          * @param       $className      Name of the class
43          * @return      void
44          */
45         protected function __construct ($className) {
46                 // Call parent constructor
47                 parent::__construct($className);
48         }
49
50         /**
51          * Destructor for cleaning purposes, etc
52          *
53          * @return      void
54          */
55         public final function __destruct() {
56                 // Try to close a file
57                 $this->closeFile();
58
59                 // Call the parent destructor
60                 parent::__destruct();
61         }
62
63         /**
64          * "Getter" for abstracted file size
65          *
66          * @return      $fileSize       Size of abstracted file
67          */
68         public function getFileSize () {
69                 // Call pointer instanze
70                 return $this->getPointerInstance()->getFileSize();
71         }
72
73         /**
74          * Getter for total entries
75          *
76          * @return      $totalEntries   Total entries in this file
77          */
78         public final function getCounter () {
79                 // Get it
80                 return $this->totalEntries;
81         }
82
83         /**
84          * Setter for total entries
85          *
86          * @param       $totalEntries   Total entries in this file
87          * @return      void
88          */
89         protected final function setCounter ($counter) {
90                 // Set it
91                 $this->totalEntries = $counter;
92         }
93
94         /**
95          * Increment counter
96          *
97          * @return      void
98          */
99         protected final function incrementCounter () {
100                 // Get it
101                 $this->totalEntries++;
102         }
103
104         /**
105          * Getter for the file object
106          *
107          * @return      $fileObject             An instance of a SplFileObject
108          */
109         public final function getFileObject () {
110                 return $this->getPointerInstance()->getFileObject();
111         }
112
113         /**
114          * Close a file source and set it's instance to null and the file name
115          * to empty
116          *
117          * @return      void
118          */
119         public function closeFile () {
120                 // Debug message
121                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: CALLED!', __METHOD__, __LINE__));
122
123                 // Close down pointer instance as well by unsetting it
124                 $this->unsetPointerInstance();
125
126                 // Debug message
127                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
128         }
129
130         /**
131          * Size of this file
132          *
133          * @return      $size   Size (in bytes) of file
134          * @todo        Handle seekStatus
135          */
136         public function size () {
137                 // Call pointer instance
138                 return $this->getPointerInstance()->size();
139         }
140
141         /**
142          * Read data a file pointer
143          *
144          * @return      mixed   The result of fread()
145          * @throws      NullPointerException    If the file pointer instance
146          *                                                                      is not set by setFileObject()
147          * @throws      InvalidResourceException        If there is being set
148          */
149         public function readFromFile () {
150                 // Call pointer instance
151                 return $this->getPointerInstance()->readFromFile();
152         }
153
154         /**
155          * Write data to a file pointer
156          *
157          * @param       $dataStream             The data stream we shall write to the file
158          * @return      mixed                   Number of writes bytes or false on error
159          * @throws      NullPointerException    If the file pointer instance
160          *                                                                      is not set by setFileObject()
161          * @throws      InvalidResourceException        If there is being set
162          *                                                                                      an invalid file resource
163          */
164         public function writeToFile ($dataStream) {
165                 // Call pointer instance
166                 return $this->getPointerInstance()->writeToFile($dataStream);
167         }
168
169         /**
170          * Determines whether the EOF has been reached
171          *
172          * @return      $isEndOfFileReached             Whether the EOF has been reached
173          */
174         public final function isEndOfFileReached () {
175                 // Call pointer instance
176                 return $this->getPointerInstance()->isEndOfFileReached();
177         }
178
179 }