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