X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fstacker%2Fclass_BaseStacker.php;h=e329f60059a311da7eb1f31b00f7e0f46fe27681;hp=9a6cf4b3cb19a6639e8785d0a86a200ff91967ac;hb=8e6b15818d226f36bc57cc4e04448b262863bca9;hpb=08330903ee5bd8ea967622ff7473d13dff19beb5 diff --git a/inc/classes/main/stacker/class_BaseStacker.php b/inc/classes/main/stacker/class_BaseStacker.php index 9a6cf4b3..e329f600 100644 --- a/inc/classes/main/stacker/class_BaseStacker.php +++ b/inc/classes/main/stacker/class_BaseStacker.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -28,11 +28,6 @@ class BaseStacker extends BaseFrameworkSystem { const EXCEPTION_NO_STACKER_FOUND = 0x052; const EXCEPTION_STACKER_IS_EMPTY = 0x053; - /** - * An array holding all stacks - */ - private $stacks = array(); - /** * Protected constructor * @@ -52,18 +47,28 @@ class BaseStacker extends BaseFrameworkSystem { * @return void * @throws AlreadyInitializedStackerException If the stack is already initialized */ - public final function initStacker ($stackerName, $forceReInit = false) { + public final function initStacker ($stackerName, $forceReInit = FALSE) { // Is the stack already initialized? - if (($forceReInit === false) && ($this->isStackInitialized($stackerName))) { + if (($forceReInit === FALSE) && ($this->isStackInitialized($stackerName))) { // Then throw the exception throw new AlreadyInitializedStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_ALREADY_INITIALIZED); } // END - if // Initialize the given stack - $this->stacks[$stackerName] = array( - 'max_size' => $this->getConfigInstance()->getConfigEntry('stacker_' . $stackerName . '_max_size'), - 'entries' => array() - ); + $this->initGenericArrayKey('stacks', $stackerName, 'entries', $forceReInit); + } + + /** + * Initializes all stackers + * + * @return void + */ + public function initStackers (array $stacks) { + // "Walk" through all (more will be added as needed + foreach ($stacks as $stackerName) { + // Init this stack + $this->initStacker($stackerName); + } // END - foreach } /** @@ -74,7 +79,7 @@ class BaseStacker extends BaseFrameworkSystem { */ public final function isStackInitialized ($stackerName) { // Is is there? - $isInitialized = ((isset($this->stacks[$stackerName])) && (is_array($this->stacks[$stackerName]))); + $isInitialized = ($this->isValidGenericArrayKey('stacks', $stackerName, 'entries')); // Return result return $isInitialized; @@ -137,7 +142,7 @@ class BaseStacker extends BaseFrameworkSystem { } // END - if // Now, count the array of entries - $count = count($this->stacks[$stackerName]['entries']); + $count = $this->countGenericArrayElements('stacks', $stackerName, 'entries'); // Return result return $count; @@ -162,7 +167,7 @@ class BaseStacker extends BaseFrameworkSystem { } // Now add the value to the stack - array_push($this->stacks[$stackerName]['entries'], $value); + $this->pushValueToGenericArrayKey('stacks', $stackerName, 'entries', $value); } /** @@ -179,12 +184,12 @@ class BaseStacker extends BaseFrameworkSystem { // Throw an exception throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } elseif ($this->isStackEmpty($stackerName)) { - //Throw an exception + // Throw an exception throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY); } // Now get the last value - $value = $this->stacks[$stackerName]['entries'][$this->getStackCount($stackerName) - 1]; + $value = $this->getGenericArrayElement('stacks', $stackerName, 'entries', $this->getStackCount($stackerName) - 1); // Return it return $value; @@ -204,12 +209,12 @@ class BaseStacker extends BaseFrameworkSystem { // Throw an exception throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } elseif ($this->isStackEmpty($stackerName)) { - //Throw an exception + // Throw an exception throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY); } - // Now get the last value - $value = $this->stacks[$stackerName]['entries'][0]; + // Now get the first value + $value = $this->getGenericArrayElement('stacks', $stackerName, 'entries', 0); // Return it return $value; @@ -219,7 +224,7 @@ class BaseStacker extends BaseFrameworkSystem { * "Pops" last entry from stack * * @param $stackerName Name of the stack - * @return void + * @return $value Value "poped" from array * @throws NoStackerException If the named stacker was not found * @throws EmptyStackerException If the named stacker is empty */ @@ -229,19 +234,19 @@ class BaseStacker extends BaseFrameworkSystem { // Throw an exception throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } elseif ($this->isStackEmpty($stackerName)) { - //Throw an exception + // Throw an exception throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY); } // Now, remove the last entry, we don't care about the return value here, see elseif() block above - array_pop($this->stacks[$stackerName]['entries']); + return $this->popGenericArrayElement('stacks', $stackerName, 'entries'); } /** * "Pops" first entry from stack * * @param $stackerName Name of the stack - * @return void + * @return $value Value "shifted" from array * @throws NoStackerException If the named stacker was not found * @throws EmptyStackerException If the named stacker is empty */ @@ -251,12 +256,12 @@ class BaseStacker extends BaseFrameworkSystem { // Throw an exception throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } elseif ($this->isStackEmpty($stackerName)) { - //Throw an exception + // Throw an exception throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY); } // Now, remove the last entry, we don't care about the return value here, see elseif() block above - array_shift($this->stacks[$stackerName]['entries']); + return $this->shiftGenericArrayElement('stacks', $stackerName, 'entries'); } }