Fixed more generic array handling, introduced unsetGenericArrayKey()
[core.git] / inc / classes / main / class_BaseFrameworkSystem.php
index 679e5e4af81ee8c9586a6afcdb22c62702362ebc..62b79bda0d49440bd0827c1d076fd0ff5a8312e3 100644 (file)
@@ -186,13 +186,18 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        /**
         * Socket resource
         */
-       private $socketResource = false;
+       private $socketResource = FALSE;
 
        /**
         * Package data
         */
        private $packageData = array();
 
+       /**
+        * Generic array
+        */
+       private $genericArray = array();
+
        /***********************
         * Exception codes.... *
         ***********************/
@@ -343,10 +348,13 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                        $this->setRealClass('DestructedObject');
                } elseif ((defined('DEBUG_DESTRUCTOR')) && (is_object($this->getDebugInstance()))) {
                        // Already destructed object
-                       self::createDebugInstance(__CLASS__)->debugOutput(sprintf("[%s:] The object <span class=\"object_name\">%s</span> is already destroyed.",
+                       self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:] The object <span class="object_name">%s</span> is already destroyed.',
                                __CLASS__,
                                $this->__toString()
                        ));
+               } else {
+                       // Do not call this twice
+                       trigger_error(__METHOD__ . ': Called twice.');
                }
        }
 
@@ -395,10 +403,10 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                                } elseif (is_array($arg)) {
                                        // .. or size if array
                                        $argsString .= ', ' . count($arg);
-                               } elseif ($arg === true) {
+                               } elseif ($arg === TRUE) {
                                        // ... is boolean 'true'
                                        $argsString .= ', true';
-                               } elseif ($arg === false) {
+                               } elseif ($arg === FALSE) {
                                        // ... is boolean 'true'
                                        $argsString .= ', false';
                                }
@@ -783,7 +791,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         * @param       $fileIoInstance         An instance to the file I/O sub-system
         * @return      void
         */
-       public final function setFileIoInstance (FileIoHandler $fileIoInstance) {
+       public final function setFileIoInstance (IoHandler $fileIoInstance) {
                $this->fileIoInstance = $fileIoInstance;
        }
 
@@ -1424,7 +1432,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                print('</pre>');
 
                // Exit program?
-               if ($doExit === true) {
+               if ($doExit === TRUE) {
                        exit();
                } // END - if
        }
@@ -1465,7 +1473,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         * @paran       $stripTags      Whether to strip tags (default: false)
         * @return      void
         */
-       public function debugOutput ($message, $doPrint = true, $stripTags = false) {
+       public function debugOutput ($message, $doPrint = TRUE, $stripTags = FALSE) {
                // Set debug instance to NULL
                $debugInstance = NULL;
 
@@ -1482,7 +1490,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                        // Use debug output handler
                        $debugInstance->output($message, $stripTags);
 
-                       if ($doPrint === false) {
+                       if ($doPrint === FALSE) {
                                // Die here if not printed
                                exit();
                        } // END - if
@@ -1494,7 +1502,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                        } // END - if
 
                        // Put directly out
-                       if ($doPrint === true) {
+                       if ($doPrint === TRUE) {
                                // Print message
                                print($message . chr(10));
                        } else {
@@ -1663,7 +1671,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                $this->getResultInstance()->rewind();
 
                // Do we have an entry?
-               if ($this->getResultInstance()->valid() === false) {
+               if ($this->getResultInstance()->valid() === FALSE) {
                        throw new InvalidDatabaseResultException(array($this, $this->getResultInstance()), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
                } // END - if
 
@@ -1798,7 +1806,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         */
        public function idle ($milliSeconds) {
                // Sleep is fine by default
-               $hasSlept = true;
+               $hasSlept = TRUE;
 
                // Idle so long with found function
                if (function_exists('time_sleep_until')) {
@@ -1964,7 +1972,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         */
        protected function isBase64Encoded ($encodedData) {
                // Determine it
-               $isBase64 = (@base64_decode($encodedData, true) !== false);
+               $isBase64 = (@base64_decode($encodedData, true) !== FALSE);
 
                // Return it
                return $isBase64;
@@ -2055,18 +2063,18 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         * @param       $assertMismatch         Whether to assert mismatches
         * @return      $ret            The (hopefully) secured numbered value
         */
-       public function bigintval ($num, $castValue = true, $assertMismatch = false) {
+       public function bigintval ($num, $castValue = TRUE, $assertMismatch = FALSE) {
                // Filter all numbers out
                $ret = preg_replace('/[^0123456789]/', '', $num);
 
                // Shall we cast?
-               if ($castValue === true) {
+               if ($castValue === TRUE) {
                        // Cast to biggest numeric type
                        $ret = (double) $ret;
                } // END - if
 
                // Assert only if requested
-               if ($assertMismatch === true) {
+               if ($assertMismatch === TRUE) {
                        // Has the whole value changed?
                        assert(('' . $ret . '' != '' . $num . '') && (!is_null($num)));
                } // END - if
@@ -2082,12 +2090,12 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         * @param       $assertMismatch         Whether to assert mismatches
         * @return      $ret    The (hopefully) secured hext-numbered value
         */
-       public function hexval ($num, $assertMismatch = false) {
+       public function hexval ($num, $assertMismatch = FALSE) {
                // Filter all numbers out
                $ret = preg_replace('/[^0123456789abcdefABCDEF]/', '', $num);
 
                // Assert only if requested
-               if ($assertMismatch === true) {
+               if ($assertMismatch === TRUE) {
                        // Has the whole value changed?
                        assert(('' . $ret . '' != '' . $num . '') && (!is_null($num)));
                } // END - if
@@ -2109,6 +2117,402 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
                // ... and return it
                return $isset;
        }
+
+       /**
+        * Determines if an element is set in the generic array
+        *
+        * @param       $keyGroup       Main group for the key
+        * @param       $subGroup       Sub group for the key
+        * @param       $key            Key to check
+        * @param       $element        Element to check
+        * @return      $isset          Whether the given key is set
+        */
+       protected final function isGenericArrayElementSet ($keyGroup, $subGroup, $key, $element) {
+               // Is it there?
+               $isset = isset($this->genericArray[$keyGroup][$subGroup][$key][$element]);
+
+               // Return it
+               return $isset;
+       }
+       /**
+        * Determines if a key is set in the generic array
+        *
+        * @param       $keyGroup       Main group for the key
+        * @param       $subGroup       Sub group for the key
+        * @param       $key            Key to check
+        * @return      $isset          Whether the given key is set
+        */
+       protected final function isGenericArrayKeySet ($keyGroup, $subGroup, $key) {
+               // Is it there?
+               $isset = isset($this->genericArray[$keyGroup][$subGroup][$key]);
+
+               // Return it
+               return $isset;
+       }
+
+
+       /**
+        * Determines if a group is set in the generic array
+        *
+        * @param       $keyGroup       Main group
+        * @param       $subGroup       Sub group
+        * @return      $isset          Whether the given group is set
+        */
+       protected final function isGenericArrayGroupSet ($keyGroup, $subGroup) {
+               // Is it there?
+               $isset = isset($this->genericArray[$keyGroup][$subGroup]);
+
+               // Return it
+               return $isset;
+       }
+
+       /**
+        * Getter for sub key group
+        *
+        * @param       $keyGroup       Main key group
+        * @param       $subGroup       Sub key group
+        * @return      $array          An array with all array elements
+        */
+       protected final function getGenericSubArray ($keyGroup, $subGroup) {
+               // Is it there?
+               if (!$this->isGenericArrayGroupSet($keyGroup, $subGroup)) {
+                       // No, then abort here
+                       trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
+               } // END - if
+
+               // Return it
+               //* DEBUG: */ print(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' returning!' . PHP_EOL);
+               return $this->genericArray[$keyGroup][$subGroup];
+       }
+
+       /**
+        * Unsets a given key in generic array
+        *
+        * @param       $keyGroup       Main group for the key
+        * @param       $subGroup       Sub group for the key
+        * @param       $key            Key to unset
+        * @return      void
+        */
+       protected final function unsetGenericArrayKey ($keyGroup, $subGroup, $key) {
+               // Remove it
+               //* DEBUG: */ print(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' unset!' . PHP_EOL);
+               unset($this->genericArray[$keyGroup][$subGroup][$key]);
+       }
+
+       /**
+        * Unsets a given element in generic array
+        *
+        * @param       $keyGroup       Main group for the key
+        * @param       $subGroup       Sub group for the key
+        * @param       $key            Key to unset
+        * @param       $element        Element to unset
+        * @return      void
+        */
+       protected final function unsetGenericArrayElement ($keyGroup, $subGroup, $key, $element) {
+               // Remove it
+               //* DEBUG: */ print(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' unset!' . PHP_EOL);
+               unset($this->genericArray[$keyGroup][$subGroup][$key][$element]);
+       }
+
+       /**
+        * Append a string to a given generic array key
+        *
+        * @param       $keyGroup       Main group for the key
+        * @param       $subGroup       Sub group for the key
+        * @param       $key            Key to unset
+        * @param       $value          Value to add/append
+        * @return      void
+        */
+       protected final function appendStringToGenericArrayElement ($keyGroup, $subGroup, $key, $value, $appendGlue = '') {
+               // Is it already there?
+               if ($this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
+                       // Append it
+                       $this->genericArray[$keyGroup][$subGroup][$key] .= $appendGlue . (string) $value;
+               } else {
+                       // Add it
+                       $this->genericArray[$keyGroup][$subGroup][$key] = (string) $value;
+               }
+       }
+
+       /**
+        * Initializes given generic array
+        *
+        * @param       $keyGroup       Main group for the key
+        * @param       $subGroup       Sub group for the key
+        * @param       $key            Key to use
+        * @param       $forceInit      Optionally force initialization
+        * @return      void
+        */
+       protected final function initGenericArrayKey ($keyGroup, $subGroup, $key, $forceInit = FALSE) {
+               // Is it already set?
+               if (($forceInit === FALSE) && ($this->isGenericArrayKeySet($keyGroup, $subGroup, $key))) {
+                       // Already initialized
+                       trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' already initialized.');
+               } // END - if
+
+               // Initialize it
+               //* DEBUG: */ print(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . PHP_EOL);
+               $this->genericArray[$keyGroup][$subGroup][$key] = array();
+       }
+
+       /**
+        * Pushes an element to a generic key
+        *
+        * @param       $keyGroup       Main group for the key
+        * @param       $subGroup       Sub group for the key
+        * @param       $key            Key to use
+        * @param       $value          Value to add/append
+        * @return      $count          Number of array elements
+        */
+       protected final function pushValueToGenericArrayElement ($keyGroup, $subGroup, $key, $value) {
+               // Is it set?
+               if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
+                       // Initialize array
+                       $this->initGenericArrayKey($keyGroup, $subGroup, $key);
+               } // END - if
+
+               // Then push it
+               //* DEBUG: */ print(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',value[' . gettype($value) . ']=' . print_r($value, TRUE) . PHP_EOL);
+               $count = array_push($this->genericArray[$keyGroup][$subGroup][$key], $value);
+
+               // Return count
+               //* DEBUG: */ print(__METHOD__ . ': genericArray=' . print_r($this->genericArray[$keyGroup][$subGroup][$key], TRUE));
+               //* DEBUG: */ print(__METHOD__ . ': count=' . $count . PHP_EOL);
+               return $count;
+       }
+
+       /**
+        * Pops an element from  a generic group
+        *
+        * @param       $keyGroup       Main group for the key
+        * @param       $subGroup       Sub group for the key
+        * @param       $key            Key to unset
+        * @return      $value          Last "popped" value
+        */
+       protected final function popGenericArrayElement ($keyGroup, $subGroup, $key) {
+               // Is it set?
+               if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
+                       // Not found
+                       trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' not found.');
+               } // END - if
+
+               // Then "pop" it
+               //* DEBUG: */ print(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' pop-ing entry ...' . PHP_EOL);
+               $value = array_pop($this->genericArray[$keyGroup][$subGroup][$key]);
+
+               // Return value
+               //* DEBUG: */ print(__METHOD__ . ': genericArray=' . print_r($this->genericArray[$keyGroup][$subGroup][$key], TRUE));
+               //* DEBUG: */ print(__METHOD__ . ': value[' . gettype($value) . ']=' . print_r($value, TRUE) . PHP_EOL);
+               return $value;
+       }
+
+       /**
+        * Shifts an element from  a generic group
+        *
+        * @param       $keyGroup       Main group for the key
+        * @param       $subGroup       Sub group for the key
+        * @param       $key            Key to unset
+        * @return      $value          Last "popped" value
+        */
+       protected final function shiftGenericArrayElement ($keyGroup, $subGroup, $key) {
+               // Is it set?
+               if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
+                       // Not found
+                       trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' not found.');
+               } // END - if
+
+               // Then "shift" it
+               //* DEBUG: */ print(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' shifting entry ...' . PHP_EOL);
+               $value = array_shift($this->genericArray[$keyGroup][$subGroup][$key]);
+
+               // Return value
+               //* DEBUG: */ print(__METHOD__ . ': genericArray=' . print_r($this->genericArray[$keyGroup][$subGroup][$key], TRUE));
+               //* DEBUG: */ print(__METHOD__ . ': value[' . gettype($value) . ']=' . $value . PHP_EOL);
+               return $value;
+       }
+
+       /**
+        * Count generic array group
+        *
+        * @param       $keyGroup       Main group for the key
+        * @return      $count          Count of given group
+        */
+       protected final function countGenericArray ($keyGroup) {
+               // Is it there?
+               if (!isset($this->genericArray[$keyGroup])) {
+                       // Abort here
+                       trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ' not found.');
+               } // END - if
+
+               // Then count it
+               $count = count($this->genericArray[$keyGroup]);
+
+               // Return it
+               return $count;
+       }
+
+       /**
+        * Count generic array sub group
+        *
+        * @param       $keyGroup       Main group for the key
+        * @param       $subGroup       Sub group for the key
+        * @return      $count          Count of given group
+        */
+       protected final function countGenericArrayGroup ($keyGroup, $subGroup) {
+               // Is it there?
+               if (!$this->isGenericArrayGroupSet($keyGroup, $subGroup)) {
+                       // Abort here
+                       trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
+               } // END - if
+
+               // Then count it
+               $count = count($this->genericArray[$keyGroup][$subGroup]);
+
+               // Return it
+               return $count;
+       }
+
+       /**
+        * Count generic array elements
+        *
+        * @param       $keyGroup       Main group for the key
+        * @param       $subGroup       Sub group for the key
+        * @para        $key            Key to count
+        * @return      $count          Count of given key
+        */
+       protected final function countGenericArrayElements ($keyGroup, $subGroup, $key) {
+               // Is it there?
+               if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
+                       // Abort here
+                       trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
+               } elseif (!$this->isValidGenericArrayGroup($keyGroup, $subGroup)) {
+                       // Not valid
+                       trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' is not an array.');
+               }
+
+               // Then count it
+               $count = count($this->genericArray[$keyGroup][$subGroup][$key]);
+               //* DEBUG: */ print(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',count=' . $count . PHP_EOL);
+
+               // Return it
+               return $count;
+       }
+
+       /**
+        * Getter for whole generic group array
+        *
+        * @param       $keyGroup       Key group to get
+        * @return      $array          Whole generic array group
+        */
+       protected final function getGenericArray ($keyGroup) {
+               // Is it there?
+               if (!isset($this->genericArray[$keyGroup])) {
+                       // Then abort here
+                       trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ' does not exist.');
+               } // END - if
+
+               // Return it
+               return $this->genericArray[$keyGroup];
+       }
+
+       /**
+        * Setter for generic array key
+        *
+        * @param       $keyGroup       Key group to get
+        * @param       $subGroup       Sub group for the key
+        * @param       $key            Key to unset
+        * @param       $value          Mixed value from generic array element
+        * @return      void
+        */
+       protected final function setGenericArrayKey ($keyGroup, $subGroup, $key, $value) {
+               // Set value here
+               $this->genericArray[$keyGroup][$subGroup][$key] = $value;
+       }
+
+       /**
+        * Getter for generic array key
+        *
+        * @param       $keyGroup       Key group to get
+        * @param       $subGroup       Sub group for the key
+        * @param       $key            Key to unset
+        * @return      $value          Mixed value from generic array element
+        */
+       protected final function getGenericArrayKey ($keyGroup, $subGroup, $key) {
+               // Is it there?
+               if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
+                       // Then abort here
+                       trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' does not exist.');
+               } // END - if
+
+               // Return it
+               return $this->genericArray[$keyGroup][$subGroup][$key];
+       }
+
+       /**
+        * Sets a value in given generic array key/element
+        *
+        * @param       $keyGroup       Main group for the key
+        * @param       $subGroup       Sub group for the key
+        * @param       $key            Key to set
+        * @param       $element        Element to set
+        * @param       $value          Value to set
+        * @return      void
+        */
+       protected final function setGenericArrayElement ($keyGroup, $subGroup, $key, $element, $value) {
+               // Then set it
+               $this->genericArray[$keyGroup][$subGroup][$key][$element] = $value;
+       }
+
+       /**
+        * Getter for generic array element
+        *
+        * @param       $keyGroup       Key group to get
+        * @param       $subGroup       Sub group for the key
+        * @param       $key            Key to look for
+        * @param       $element        Element to look for
+        * @return      $value          Mixed value from generic array element
+        */
+       protected final function getGenericArrayElement ($keyGroup, $subGroup, $key, $element) {
+               // Is it there?
+               if (!$this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element)) {
+                       // Then abort here
+                       trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ' does not exist.');
+               } // END - if
+
+               // Return it
+               return $this->genericArray[$keyGroup][$subGroup][$key][$element];
+       }
+
+       /**
+        * Checks if a given sub group is valid (array)
+        *
+        * @param       $keyGroup       Key group to get
+        * @param       $subGroup       Sub group for the key
+        * @return      $isValid        Whether given sub group is valid
+        */
+       protected final function isValidGenericArrayGroup ($keyGroup, $subGroup) {
+               // Determine it
+               $isValid = (($this->isGenericArrayGroupSet($keyGroup, $subGroup)) && (is_array($this->getGenericSubArray($keyGroup, $subGroup))));
+
+               // Return it
+               return $isValid;
+       }
+
+       /**
+        * Checks if a given key is valid (array)
+        *
+        * @param       $keyGroup       Key group to get
+        * @param       $subGroup       Sub group for the key
+        * @param       $key            Key to check
+        * @return      $isValid        Whether given sub group is valid
+        */
+       protected final function isValidGenericArrayKey ($keyGroup, $subGroup, $key) {
+               // Determine it
+               $isValid = (($this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) && (is_array($this->getGenericArrayKey($keyGroup, $subGroup, $key))));
+
+               // Return it
+               return $isValid;
+       }
 }
 
 // [EOF]