use Org\Mxchange\CoreFramework\Filesystem\File\BinaryFile;
use Org\Mxchange\CoreFramework\Index\BaseIndex;
use Org\Mxchange\CoreFramework\Index\Indexable;
+use Org\Mxchange\CoreFramework\Utils\Arrays\ArrayUtils;
use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
// Import SPL stuff
*/
$header = explode(chr(BinaryFile::SEPARATOR_HEADER_DATA), $data);
- // Check if the array has only 3 elements
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: header()=%d', count($header)));
+ // Map numeric entries to associative (alpha-numeric) elements
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: HEADER_INDEX_ELEMENT_COUNT=%d,header()=%d', BinaryFile::HEADER_INDEX_ELEMENT_COUNT, count($header)));
//* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: header(%d)=%s', count($header), print_r($header, true)));
- if (count($header) != 2) {
+ $header = ArrayUtils::mapNumericKeysToAssociative($header, [
+ BinaryFile::HEADER_NAME_MAGIC,
+ BinaryFile::HEADER_NAME_TOTAL_ENTRIES,
+ ]);
+
+ // Check if the array has only 2 elements
+ if (count($header) != BinaryFile::HEADER_INDEX_ELEMENT_COUNT) {
// Bad header
- throw new UnexpectedValueException(sprintf('header()=%d is not expected value 2', count($header)));
- } elseif ($header[0] !== Indexable::INDEX_MAGIC) {
+ throw new UnexpectedValueException(sprintf('header()=%d is not expected value %d', count($header), BinaryFile::HEADER_INDEX_ELEMENT_COUNT));
+ } elseif ($header[BinaryFile::HEADER_NAME_MAGIC] !== Indexable::INDEX_MAGIC) {
// Magic must be in first element
- throw new UnexpectedValueException(sprintf('header[0]=%s is not the expected magic (%s)', $header[0], Indexable::INDEX_MAGIC));
- } elseif (strlen($header[1]) != BinaryFile::LENGTH_COUNT) {
+ throw new UnexpectedValueException(sprintf('header[%s]=%s is not the expected magic (%s)', BinaryFile::HEADER_NAME_MAGIC, $header[BinaryFile::HEADER_NAME_MAGIC], Indexable::INDEX_MAGIC));
+ } elseif (strlen($header[BinaryFile::HEADER_NAME_TOTAL_ENTRIES]) != BinaryFile::LENGTH_COUNT) {
// Length of total entries not matching
- throw new UnexpectedValueException(sprintf('header[1](%d)=%s does not have expected length %d', strlen($header[1]), $header[1], BinaryFile::LENGTH_COUNT));
+ throw new UnexpectedValueException(sprintf('header[%s](%d)=%s does not have expected length %d', BinaryFile::HEADER_NAME_TOTAL_ENTRIES, strlen($header[BinaryFile::HEADER_NAME_TOTAL_ENTRIES]), $header[BinaryFile::HEADER_NAME_TOTAL_ENTRIES], BinaryFile::LENGTH_COUNT));
}
// Decode count
- $header[1] = hex2bin($header[1]);
+ $header[BinaryFile::HEADER_NAME_TOTAL_ENTRIES] = hex2bin($header[BinaryFile::HEADER_NAME_TOTAL_ENTRIES]);
// Set it here
$this->getIteratorInstance()->getBinaryFileInstance()->setHeader($header);
use Org\Mxchange\CoreFramework\Stack\BaseStacker;
use Org\Mxchange\CoreFramework\Traits\Index\IndexableTrait;
use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
-use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
use Org\Mxchange\CoreFramework\Utils\Arrays\ArrayUtils;
+use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
// Import SPL stuff
use \InvalidArgumentException;
]);
// Check if the array has only 3 elements
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: HEADER_ELEMENT_COUNT=%d,header(%d)=%s', BinaryFile::HEADER_ELEMENT_COUNT, count($header), print_r($header, true)));
- if (count($header) != BinaryFile::HEADER_ELEMENT_COUNT) {
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: HEADER_STACK_ELEMENT_COUNT=%d,header(%d)=%s', BinaryFile::HEADER_STACK_ELEMENT_COUNT, count($header), print_r($header, true)));
+ if (count($header) != BinaryFile::HEADER_STACK_ELEMENT_COUNT) {
// Header array count is not expected
throw new UnexpectedValueException(sprintf('data=%s has %d elements, but expected is %d',
$data,
count($header),
- BinaryFile::HEADER_ELEMENT_COUNT
+ BinaryFile::HEADER_STACK_ELEMENT_COUNT
));
} elseif ($header[BinaryFile::HEADER_NAME_MAGIC] != StackableFile::STACK_MAGIC) {
// Bad magic
throw new InvalidMagicException($data, self::EXCEPTION_BAD_MAGIC);
- }
-
- // Check length of count and seek position
- if (strlen($header[BinaryFile::HEADER_NAME_TOTAL_ENTRIES]) != BinaryFile::LENGTH_COUNT) {
+ } elseif (strlen($header[BinaryFile::HEADER_NAME_TOTAL_ENTRIES]) != BinaryFile::LENGTH_COUNT) {
// Count length not valid
throw new UnexpectedValueException(sprintf('header[%s](%d)=%s is not expected %d length',
BinaryFile::HEADER_NAME_TOTAL_ENTRIES,