]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/stacker/file/class_BaseFileStack.php
Continued:
[core.git] / framework / main / classes / stacker / file / class_BaseFileStack.php
index d990a375f07525492bbb8f9c84fa98afe85bef0d..a5a2eaa69d76f30d7ff437ed9586919073a832d9 100644 (file)
@@ -1,16 +1,17 @@
 <?php
 // Own namespace
-namespace Org\Mxchange\CoreFramework\Stacker\Filesystem;
+namespace Org\Mxchange\CoreFramework\Stack\File;
 
 // Import framework stuff
-use Org\Mxchange\CoreFramework\Factory\Filesystem\Stack\FileStackIndexFactory;
+use Org\Mxchange\CoreFramework\Factory\Stack\File\FileStackIndexFactory;
 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
-use Org\Mxchange\CoreFramework\FileStack\InvalidMagicException;
 use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
 use Org\Mxchange\CoreFramework\Index\Indexable;
 use Org\Mxchange\CoreFramework\Iterator\Filesystem\SeekableWritableFileIterator;
-use Org\Mxchange\CoreFramework\Stacker\BaseStacker;
+use Org\Mxchange\CoreFramework\Stack\BaseStacker;
+use Org\Mxchange\CoreFramework\Stack\File\InvalidMagicException;
+use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
 use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
 
@@ -40,33 +41,13 @@ use \UnexpectedValueException;
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-abstract class BaseFileStack extends BaseStacker {
+abstract class BaseFileStack extends BaseStacker implements StackableFile {
        // Load traits
        use IteratorTrait;
 
        // Exception codes
        const EXCEPTION_BAD_MAGIC = 0xe100;
 
-       /**
-        * Magic for this stack
-        */
-       const STACK_MAGIC = 'STACKv0.1';
-
-       /**
-        * Name of array index for gap position
-        */
-       const ARRAY_INDEX_GAP_POSITION = 'gap';
-
-       /**
-        * Name of array index for hash
-        */
-       const ARRAY_INDEX_HASH = 'hash';
-
-       /**
-        * Name of array index for length of raw data
-        */
-       const ARRAY_INDEX_DATA_LENGTH = 'length';
-
        /**
         * An instance of an Indexable class
         */
@@ -154,9 +135,6 @@ abstract class BaseFileStack extends BaseStacker {
                 */
                $header = explode(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), $data);
 
-               // Set header here
-               $this->getIteratorInstance()->setHeader($header);
-
                // Check if the array has only 3 elements
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: header(%d)=%s', count($header), print_r($header, true)));
                if (count($header) != 3) {
@@ -165,7 +143,7 @@ abstract class BaseFileStack extends BaseStacker {
                                $data,
                                count($header)
                        ));
-               } elseif ($header[0] != self::STACK_MAGIC) {
+               } elseif ($header[0] != StackableFile::STACK_MAGIC) {
                        // Bad magic
                        throw new InvalidMagicException($data, self::EXCEPTION_BAD_MAGIC);
                }
@@ -191,6 +169,9 @@ abstract class BaseFileStack extends BaseStacker {
                $header[1] = hex2bin($header[1]);
                $header[2] = hex2bin($header[2]);
 
+               // Set header here
+               $this->getIteratorInstance()->setHeader($header);
+
                // Trace message
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: EXIT!', __METHOD__, __LINE__));
        }
@@ -205,7 +186,7 @@ abstract class BaseFileStack extends BaseStacker {
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: CALLED!', __METHOD__, __LINE__));
                $header = sprintf('%s%s%s%s%s%s',
                        // Magic
-                       self::STACK_MAGIC,
+                       StackableFile::STACK_MAGIC,
 
                        // Separator magic<->count
                        chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
@@ -251,7 +232,7 @@ abstract class BaseFileStack extends BaseStacker {
 
                // Calculate header size
                $this->getIteratorInstance()->setHeaderSize(
-                       strlen(self::STACK_MAGIC) +
+                       strlen(StackableFile::STACK_MAGIC) +
                        strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
                        BaseBinaryFile::LENGTH_COUNT +
                        strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
@@ -322,13 +303,13 @@ abstract class BaseFileStack extends BaseStacker {
         *
         * @param       $stackerName    Name of the stack
         * @return      $value                  Value of last added value
-        * @throws      EmptyStackerException   If the stack is empty
+        * @throws      BadMethodCallException  If the stack is empty
         */
        protected function getLastValue (string $stackerName) {
                // Is the stack not yet initialized or full?
                if ($this->isStackEmpty($stackerName)) {
                        // Throw an exception
-                       throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
+                       throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
                } // END - if
 
                // Now get the last value
@@ -344,13 +325,13 @@ abstract class BaseFileStack extends BaseStacker {
         *
         * @param       $stackerName    Name of the stack
         * @return      $value                  Value of last added value
-        * @throws      EmptyStackerException   If the stack is empty
+        * @throws      BadMethodCallException  If the stack is empty
         */
        protected function getFirstValue (string $stackerName) {
                // Is the stack not yet initialized or full?
                if ($this->isStackEmpty($stackerName)) {
                        // Throw an exception
-                       throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
+                       throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
                } // END - if
 
                // Now get the first value
@@ -366,13 +347,13 @@ abstract class BaseFileStack extends BaseStacker {
         *
         * @param       $stackerName    Name of the stack
         * @return      $value                  Value "poped" from array
-        * @throws      EmptyStackerException   If the stack is empty
+        * @throws      BadMethodCallException  If the stack is empty
         */
        protected function popLast (string $stackerName) {
                // Is the stack not yet initialized or full?
                if ($this->isStackEmpty($stackerName)) {
                        // Throw an exception
-                       throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
+                       throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
                } // END - if
 
                // Now, remove the last entry, we don't care about the return value here, see elseif() block above
@@ -385,13 +366,13 @@ abstract class BaseFileStack extends BaseStacker {
         *
         * @param       $stackerName    Name of the stack
         * @return      $value                  Value "shifted" from array
-        * @throws      EmptyStackerException   If the named stacker is empty
+        * @throws      BadMethodCallException  If the named stacker is empty
         */
        protected function popFirst (string $stackerName) {
                // Is the stack not yet initialized or full?
                if ($this->isStackEmpty($stackerName)) {
                        // Throw an exception
-                       throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
+                       throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
                } // END - if
 
                // Now, remove the last entry, we don't care about the return value here, see elseif() block above
@@ -668,9 +649,9 @@ abstract class BaseFileStack extends BaseStacker {
                // Return gap position, hash and length of raw data
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,rawData()=%d - EXIT!', $groupId, $hash, strlen($rawData)));
                return [
-                       self::ARRAY_INDEX_GAP_POSITION => $gapPosition,
-                       self::ARRAY_INDEX_HASH         => $hash,
-                       self::ARRAY_INDEX_DATA_LENGTH  => strlen($rawData),
+                       self::ARRAY_NAME_GAP_POSITION => $gapPosition,
+                       self::ARRAY_NAME_HASH         => $hash,
+                       self::ARRAY_NAME_DATA_LENGTH  => strlen($rawData),
                ];
        }