56c502f477bc0cfc8ed7a1b3da22a8a5d4cac165
[core.git] / framework / main / classes / file_directories / output / text / class_FrameworkTextFileOutputPointer.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Filesystem\Pointer\Text;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\FileSystem\BaseFileIo;
7 use Org\Mxchange\CoreFramework\Filesystem\Pointer\OutputPointer;
8 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
9 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
10
11 // Import SPL stuff
12 use \InvalidArgumentException;
13 use \SplFileInfo;
14
15 /**
16  * A class for writing files
17  *
18  * @author              Roland Haeder <webmaster@shipsimu.org>
19  * @version             0.0.0
20 <<<<<<< HEAD:framework/main/classes/file_directories/output/text/class_FrameworkTextFileOutputPointer.php
21  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
22 =======
23  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
24 >>>>>>> Some updates::inc/main/classes/file_directories/output/text/class_FrameworkTextFileOutputPointer.php
25  * @license             GNU GPL 3.0 or any newer version
26  * @link                http://www.shipsimu.org
27  *
28  * This program is free software: you can redistribute it and/or modify
29  * it under the terms of the GNU General Public License as published by
30  * the Free Software Foundation, either version 3 of the License, or
31  * (at your option) any later version.
32  *
33  * This program is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36  * GNU General Public License for more details.
37  *
38  * You should have received a copy of the GNU General Public License
39  * along with this program. If not, see <http://www.gnu.org/licenses/>.
40  */
41 class FrameworkTextFileOutputPointer extends BaseFileIo implements OutputPointer {
42         /**
43          * Protected constructor
44          *
45          * @return      void
46          */
47         protected function __construct () {
48                 // Call parent constructor
49                 parent::__construct(__CLASS__);
50         }
51
52         /**
53          * Create a file pointer based on the given file. The file will also
54          * be verified here.
55          *
56          * @param       $fileInstance   An instance of a SplFileInfo class
57          * @param       $mode           The output mode ('w', 'a' are valid)
58          * @throws      InvalidArgumentException        If mode is empty
59          * @throws      FileIoException                 If fopen() returns not a file resource
60          * @return      void
61          */
62         public static final function createFrameworkTextFileOutputPointer (SplFileInfo $fileInstance, $mode) {
63                 // Some pre-sanity checks...
64                 if (empty($mode)) {
65                         // No filename given
66                         throw new InvalidArgumentException('Parameter "mode" is empty');
67                 } // END - if
68
69                 // Try to open a handler
70                 $fileObject = $fileInstance->openFile($mode);
71
72                 // Is it valid?
73                 if ((is_null($fileObject)) || ($fileObject === false)) {
74                         // Something bad happend
75                         throw new FileIoException($fileInstance, self::EXCEPTION_FILE_POINTER_INVALID);
76                 } // END - if
77
78                 // Create new instance
79                 $pointerInstance = new FrameworkTextFileOutputPointer();
80
81                 // Set file object
82                 $pointerInstance->setFileObject($fileObject);
83
84                 // Return the instance
85                 return $pointerInstance;
86         }
87
88         /**
89          * Write data to a file pointer
90          *
91          * @param       $dataStream             The data stream we shall write to the file
92          * @return      mixed                   Number of writes bytes or false on error
93          * @throws      NullPointerException    If the file pointer instance is not set by setPointer()
94          * @throws      LogicException  If there is no object being set
95          */
96         public function writeToFile ($dataStream) {
97                 if (is_null($this->getFileObject())) {
98                         // Pointer not initialized
99                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
100                 } elseif (!is_object($this->getFileObject())) {
101                         // Pointer is not a valid object!
102                         throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
103                 }
104
105                 // Write data to the file pointer and return written bytes
106                 return $this->getFileObject()->fwrite($dataStream);
107         }
108
109         /**
110          * Analyzes entries in index file. This will count all found (and valid)
111          * entries, mark invalid as damaged and count gaps ("fragmentation"). If
112          * only gaps are found, the file is considered as "virgin" (no entries).
113          *
114          * @return      void
115          * @throws      UnsupportedOperationException   If this method is called
116          */
117         public function analyzeFile () {
118                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
119         }
120
121         /**
122          * Writes at given position by seeking to it.
123          *
124          * @param       $seekPosition   Seek position in file
125          * @param       $data                   Data to be written
126          * @return      mixed                   Number of writes bytes or false on error
127          * @throws      UnsupportedOperationException   If this method is called
128          */
129         public function writeAtPosition ($seedPosition, $data) {
130                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
131         }
132
133         /**
134          * Advances to next "block" of bytes
135          *
136          * @return      void
137          * @throws      UnsupportedOperationException   If this method is called
138          */
139         public function next () {
140                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
141         }
142
143         /**
144          * Checks wether the current entry is valid (not at the end of the file).
145          * This method will return true if an emptied (nulled) entry has been found.
146          *
147          * @return      $isValid        Whether the next entry is valid
148          * @throws      UnsupportedOperationException   If this method is called
149          */
150         public function valid () {
151                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
152         }
153
154         /**
155          * Gets current seek position ("key").
156          *
157          * @return      $key    Current key in iteration
158          * @throws      UnsupportedOperationException   If this method is called
159          */
160         public function key () {
161                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
162         }
163
164 }