]> git.mxchange.org Git - core.git/blob - framework/main/classes/file_directories/output/raw/class_FrameworkRawFileOutputPointer.php
Continued:
[core.git] / framework / main / classes / file_directories / output / raw / class_FrameworkRawFileOutputPointer.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Filesystem\Pointer\Output;
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
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 - 2023 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 FrameworkRawFileOutputPointer 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          * @return      void
57          * @throws      InvalidArgumentException        If parameter mode is empty
58          * @throws      FileIoException                 If fopen() returns not a file resource
59          */
60         public static final function createFrameworkRawFileOutputPointer (SplFileInfo $fileInstance, string $mode) {
61                 // Is the parameter valid?
62                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('RAW-FILE-OUTPUT-POINTER: fileInstance=%s,mode=%s - CALLED!', $fileInstance->__toString(), $mode));
63                 if (empty($mode)) {
64                         // No fileInstance given
65                         throw new InvalidArgumentException('Parameter "mode" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
66                 }
67
68                 // Try to open a handler
69                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('RAW-FILE-OUTPUT-POINTER: Invoking fileInstance->openFile(%s) ...', $mode));
70                 $fileObject = $fileInstance->openFile($mode);
71
72                 // Is it valid?
73                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-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                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RAW-FILE-OUTPUT-POINTER: Creating pointer instance ...');
81                 $pointerInstance = new FrameworkRawFileOutputPointer();
82
83                 // Set file pointer and file name
84                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-OUTPUT-POINTER: pointerInstance=%s,fileObject=%s', $pointerInstance->__toString(), get_class($fileObject)));
85                 $pointerInstance->setFileObject($fileObject);
86
87                 // Return the instance
88                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('RAW-FILE-OUTPUT-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString()));
89                 return $pointerInstance;
90         }
91
92         /**
93          * Write data to a file pointer
94          *
95          * @param       $dataStream             The data stream we shall write to the file
96          * @return      mixed                   Number of writes bytes or false on error
97          * @throws      InvalidArgumentException        If a parameter is invalid
98          * @throws      NullPointerException    If the file pointer instance is not set by setFileObject()
99          * @throws      LogicException  If there is no object being set
100          */
101         public function writeToFile (string $dataStream) {
102                 // Validate parameter and class own attributes
103                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('RAW-FILE-OUTPUT-POINTER: dataStream(%d)=%s (trimmed) - CALLED!', strlen($dataStream), trim($dataStream)));
104                 if (empty($dataStream)) {
105                         // Empty data stream
106                         throw new InvalidArgumentException('Parameter "dataStream" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
107                 } elseif (is_null($this->getFileObject())) {
108                         // Pointer not initialized
109                         throw new NullPointerException($this, FrameworkInterface::EXCEPTION_IS_NULL_POINTER);
110                 } elseif (!is_object($this->getFileObject())) {
111                         // Pointer is not a valid resource!
112                         throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())), FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
113                 }
114
115                 // Write data to the file pointer and return written bytes
116                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('RAW-FILE-OUTPUT-POINTER: Invoking this->fileObject->fwrite(dataStream()=%d - EXIT!', strlen($dataStream)));
117                 return $this->getFileObject()->fwrite($dataStream);
118         }
119
120         /**
121          * Analyzes entries in index file. This will count all found (and valid)
122          * entries, mark invalid as damaged and count gaps ("fragmentation"). If
123          * only gaps are found, the file is considered as "virgin" (no entries).
124          *
125          * @return      void
126          * @throws      UnsupportedOperationException   If this method is called
127          */
128         public function analyzeFileStructure () {
129                 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
130         }
131
132         /**
133          * Writes at given position by seeking to it.
134          *
135          * @param       $seekPosition   Seek position in file
136          * @param       $data                   Data to be written
137          * @return      mixed                   Number of writes bytes or false on error
138          * @throws      UnsupportedOperationException   If this method is called
139          */
140         public function writeAtPosition (int $seedPosition, string $data) {
141                 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
142         }
143
144         /**
145          * Advances to next "block" of bytes
146          *
147          * @return      void
148          * @throws      UnsupportedOperationException   If this method is called
149          */
150         public function next () {
151                 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
152         }
153
154         /**
155          * Checks wether the current entry is valid (not at the end of the file).
156          * This method will return true if an emptied (nulled) entry has been found.
157          *
158          * @return      $isValid        Whether the next entry is valid
159          * @throws      UnsupportedOperationException   If this method is called
160          */
161         public function valid () {
162                 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
163         }
164
165         /**
166          * Gets current seek position ("key").
167          *
168          * @return      $key    Current key in iteration
169          * @throws      UnsupportedOperationException   If this method is called
170          */
171         public function key () {
172                 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
173         }
174
175 }