]> git.mxchange.org Git - core.git/blob - framework/main/classes/file_directories/binary/stack/class_StackFile.php
Refacuring / possible WIP:
[core.git] / framework / main / classes / file_directories / binary / stack / class_StackFile.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Stack\File;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\Stack\FileStacker;
7 use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
8 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
9 use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
10 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
11
12 // Import SPL stuff
13 use \InvalidArgumentException;
14 use \SplFileInfo;
15
16 /**
17  * A stack file class
18  *
19  * @author              Roland Haeder <webmaster@ship-simu.org>
20  * @version             0.0.0
21  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
22  * @license             GNU GPL 3.0 or any newer version
23  * @link                http://www.ship-simu.org
24  *
25  * This program is free software: you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation, either version 3 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program. If not, see <http://www.gnu.org/licenses/>.
37  */
38 class StackFile extends BaseBinaryFile implements FileStacker {
39         /**
40          * Protected constructor
41          *
42          * @return      void
43          */
44         protected function __construct () {
45                 // Call parent constructor
46                 parent::__construct(__CLASS__);
47         }
48
49         /**
50          * Creates an instance of this File class and prepares it for usage
51          *
52          * @param       $infoInstance   An instance of a SplFileInfo class
53          * @param       $stackInstance  An instance of a StackableFile class
54          * @return      $fileInstance   An instance of this File class
55          */
56         public final static function createStackFile (SplFileInfo $infoInstance, StackableFile $stackInstance) {
57                 // Get a new instance
58                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: infoInstance[%s]=%s,stackInstance=%s - CALLED!', get_class($infoInstance), $infoInstance, $stackInstance->__toString()));
59                 $fileInstance = new StackFile();
60
61                 // Set stack instance here for callbacks
62                 $fileInstance->setStackInstance($stackInstance);
63
64                 // Init this abstract file
65                 $fileInstance->initFile($infoInstance);
66
67                 // Return the prepared instance
68                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: fileInstance=%s - EXIT!', $fileInstance->__toString()));
69                 return $fileInstance;
70         }
71
72         /**
73          * Writes given value to the file and returns a hash and gap position for it
74          *
75          * @param       $stackName      Group identifier
76          * @param       $value          Value to be added to the stack
77          * @return      $data           Hash and gap position
78          * @throws      InvalidArgumentException        If a parameter is not valid
79          */
80         public function writeValueToFile (string $stackName, $value) {
81                 // Validate parameter
82                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STACK-FILE: stackName=%s,value[]=%s - CALLED!', $stackName, gettype($value)));
83                 if (empty($stackName)) {
84                         // Throw IAE
85                         throw new InvalidArgumentException('Parameter "stackName" is empty');
86                 } elseif (is_object($value) || is_resource($value)) {
87                         // Not wanted here
88                         throw new InvalidArgumentException(sprintf('value[]=%s is not stackable in files', gettype($value)));
89                 }
90
91                 // Encode/convert the value into a "binary format"
92                 $encoded = StringUtils::encodeData($value);
93
94                 // Get a strong hash for the "encoded" data
95                 $hash = self::hash($encoded);
96
97                 // Then write it to the next free gap
98                 $data = $this->getStackInstance()->writeDataToFreeGap($stackName, $hash, $encoded);
99
100                 // Return info
101                 return $data;
102         }
103
104         /**
105          * Writes given raw data to the file and returns a gap position and length
106          *
107          * @param       $stackName      Group identifier
108          * @param       $hash           Hash from encoded value
109          * @param       $encoded        Encoded value to be written to the file
110          * @return      $data           Gap position and length of the raw data
111          * @throws      UnsupportedOperationException   If this method is called
112          */
113         public function writeDataToFreeGap (string $stackName, string $hash, string $encoded) {
114                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('STACK-FILE: stackName=' . $stackName . ',hash=' . $hash . ',encoded()=' . strlen($encoded));
115                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
116         }
117
118 }