TODO: We should find something better than BaseFrameworkSystem as a type-hint
[shipsimu.git] / ship-simu / inc / classes / main / io / class_FrameworkFileOutputPointer.php
1 <?php
2 /**
3  * A class for writing files
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 class FrameworkFileOutputPointer extends BaseFrameworkSystem {
24         /**
25          * The current file we are working in
26          */
27         private $fileName = "";
28
29         /**
30          * The file pointer
31          */
32         private $filePointer = null;
33
34         /**
35          * Private constructor
36          */
37         private final function __construct () {
38                 // Call parent constructor
39                 parent::constructor(__CLASS__);
40
41                 // Set part description
42                 $this->setPartDescr("Dateiausgabe-Handler");
43
44                 // Create unique ID
45                 $this->createUniqueID();
46
47                 // Clean-up a little
48                 $this->removeNumberFormaters();
49         }
50
51         /**
52          * Destructor for cleaning purposes, etc
53          */
54         public final function __destruct() {
55                 // Is there a resource pointer? Then we have to close the file here!
56                 if (is_resource($this->getPointer())) {
57                         // Try to close a file
58                         $this->closeFile();
59                 }
60
61                 // Call the parent destructor
62                 parent::__destruct();
63         }
64
65         /**
66          * Create a file pointer based on the given file. The file will also
67          * be verified here.
68          *
69          * @param               $fileName                               The file name we shall pass
70          *                                                              to fopen()
71          * @param               $mode                           The output mode ('w', 'a' are valid)
72          * @throws      FileIsEmptyException    If the provided file name is empty.
73          * @throws      FilePointerNotOpened    If fopen() returns not a file
74          *                                                              resource
75          * @return      void
76          */
77         public final static function createFrameworkFileOutputPointer ($fileName, $mode) {
78                 // Some pre-sanity checks...
79                 if (is_null($fileName)) {
80                         // No filename given
81                         throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
82                 }
83
84                 // Try to open a handler
85                 $filePointer = @fopen($fileName, $mode);
86                 if (($filePointer === null) || ($filePointer === false)) {
87                         // Something bad happend
88                         throw new FilePointerNotOpenedException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
89                 }
90
91                 // Create new instance
92                 $pointerInstance = new FrameworkFileOutputPointer();
93
94                 // Set file pointer and file name
95                 $pointerInstance->setPointer($filePointer);
96                 $pointerInstance->setFileName($fileName);
97
98                 // Return the instance
99                 return $pointerInstance;
100         }
101
102         /**
103          * Write data to a file pointer
104          *
105          * @param               $dataStream     The data stream we shall write to the file
106          * @return      mixed           The result of fwrite()
107          * @throws      NullPointerException    If the file pointer instance
108          *                                                              is not set by setPointer()
109          * @throws      InvalidFileResourceException    If there is being set
110          *                                                                      an invalid file resource
111          */
112         public function writeToFile ($dataStream) {
113                 if (is_null($this->getPointer())) {
114                         // Pointer not initialized
115                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
116                 } elseif (!is_resource($this->getPointer())) {
117                         // Pointer is not a valid resource!
118                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
119                 }
120
121                 // Read data from the file pointer and return it
122                 return fwrite($this->getPointer(), $dataStream);
123         }
124
125         /**
126          * Close a file source and set it's instance to null and the file name
127          * to empty
128          *
129          * @return      void
130          * @throws      NullPointerException    If the file pointer instance
131          *                                                              is not set by setPointer()
132          * @throws      InvalidFileResourceException    If there is being set
133          */
134         public function closeFile () {
135                 if (is_null($this->getPointer())) {
136                         // Pointer not initialized
137                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
138                 } elseif (!is_resource($this->getPointer())) {
139                         // Pointer is not a valid resource!
140                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
141                 }
142
143                 // Close the file pointer and reset the instance variable
144                 @fclose($this->getPointer());
145                 $this->setPointer(null);
146                 $this->setFileName("");
147         }
148
149         /**
150          * Setter for the file pointer
151          *
152          * @param               $filePointer    File resource
153          * @return      void
154          */
155         public final function setPointer ($filePointer) {
156                 // Sanity-check if the pointer is a valid file resource
157                 if (is_resource($filePointer) || is_null($filePointer)) {
158                         // Is a valid resource
159                         $this->filePointer = $filePointer;
160                 } else {
161                         // Throw exception
162                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
163                 }
164         }
165
166         /**
167          * Getter for the file pointer
168          *
169          * @return      $filePointer    The file pointer which shall be a valid
170          *                                              file resource
171          */
172         public final function getPointer () {
173                 return $this->filePointer;
174         }
175
176         /**
177          * Setter for file name
178          *
179          * @param               $fileName               The new file name
180          * @return      void
181          */
182         public final function setFileName ($fileName) {
183                 $fileName = (string) $fileName;
184                 $this->fileName = $fileName;
185         }
186
187         /**
188          * Getter for file name
189          *
190          * @return      $fileName               The current file name
191          */
192         public final function getFileName () {
193                 return $this->fileName;
194         }
195 }
196
197 // [EOF]
198 ?>