Continued:
[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  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
21  * @license             GNU GPL 3.0 or any newer version
22  * @link                http://www.shipsimu.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 class FrameworkTextFileOutputPointer extends BaseFileIo implements OutputPointer {
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         private function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46         }
47
48         /**
49          * Create a file pointer based on the given file. The file will also
50          * be verified here.
51          *
52          * @param       $fileInstance   An instance of a SplFileInfo class
53          * @param       $mode           The output mode ('w', 'a' are valid)
54          * @throws      InvalidArgumentException        If mode is empty
55          * @throws      FileIoException                 If fopen() returns not a file resource
56          * @return      void
57          */
58         public static final function createFrameworkTextFileOutputPointer (SplFileInfo $fileInstance, $mode) {
59                 // Some pre-sanity checks...
60                 if (empty($mode)) {
61                         // No filename given
62                         throw new InvalidArgumentException('Parameter "mode" is empty');
63                 }
64
65                 // Try to open a handler
66                 $fileObject = $fileInstance->openFile($mode);
67
68                 // Is it valid?
69                 if ((is_null($fileObject)) || ($fileObject === false)) {
70                         // Something bad happend
71                         throw new FileIoException($fileInstance, self::EXCEPTION_FILE_POINTER_INVALID);
72                 }
73
74                 // Create new instance
75                 $pointerInstance = new FrameworkTextFileOutputPointer();
76
77                 // Set file object
78                 $pointerInstance->setFileObject($fileObject);
79
80                 // Return the instance
81                 return $pointerInstance;
82         }
83
84         /**
85          * Write data to a file pointer
86          *
87          * @param       $dataStream             The data stream we shall write to the file
88          * @return      mixed                   Number of writes bytes or false on error
89          * @throws      NullPointerException    If the file pointer instance is not set by setFileObject()
90          * @throws      LogicException  If there is no object being set
91          */
92         public function writeToFile (string $dataStream) {
93                 if (is_null($this->getFileObject())) {
94                         // Pointer not initialized
95                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
96                 } elseif (!is_object($this->getFileObject())) {
97                         // Pointer is not a valid object!
98                         throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
99                 }
100
101                 // Write data to the file pointer and return written bytes
102                 return $this->getFileObject()->fwrite($dataStream);
103         }
104
105         /**
106          * Analyzes entries in index file. This will count all found (and valid)
107          * entries, mark invalid as damaged and count gaps ("fragmentation"). If
108          * only gaps are found, the file is considered as "virgin" (no entries).
109          *
110          * @return      void
111          * @throws      UnsupportedOperationException   If this method is called
112          */
113         public function analyzeFileStructure () {
114                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
115         }
116
117         /**
118          * Writes at given position by seeking to it.
119          *
120          * @param       $seekPosition   Seek position in file
121          * @param       $data                   Data to be written
122          * @return      mixed                   Number of writes bytes or false on error
123          * @throws      UnsupportedOperationException   If this method is called
124          */
125         public function writeAtPosition (int $seedPosition, string $data) {
126                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
127         }
128
129         /**
130          * Advances to next "block" of bytes
131          *
132          * @return      void
133          * @throws      UnsupportedOperationException   If this method is called
134          */
135         public function next () {
136                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
137         }
138
139         /**
140          * Checks wether the current entry is valid (not at the end of the file).
141          * This method will return true if an emptied (nulled) entry has been found.
142          *
143          * @return      $isValid        Whether the next entry is valid
144          * @throws      UnsupportedOperationException   If this method is called
145          */
146         public function valid () {
147                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
148         }
149
150         /**
151          * Gets current seek position ("key").
152          *
153          * @return      $key    Current key in iteration
154          * @throws      UnsupportedOperationException   If this method is called
155          */
156         public function key () {
157                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
158         }
159
160 }