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