* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class FiFoStacker extends BaseStacker implements Stackable { /** * Protected constructor * * @return void */ private function __construct () { // Call parent constructor parent::__construct(__CLASS__); } /** * Creates an instance of the class Stacker and prepares it for usage * * @return $stackInstance An instance of FiFoStacker */ public static final function createFiFoStacker () { // Get a new instance $stackInstance = new FiFoStacker(); // Init generic stacker $stackInstance->initStack('generic'); // Return the prepared instance return $stackInstance; } /** * Pushs a value on a named stacker * * @param $stackerName Name of the stack * @param $value Value to push on it * @return void * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the stack is full */ public function pushNamed (string $stackerName, $value) { // Validate parameter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value))); if (empty($stackerName)) { // No empty stack name throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Call the protected method //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Invoking parent::addValueToStack(%s,%s) ...', $stackerName, gettype($value))); parent::addValueToStack($stackerName, $value); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-STACKER: EXIT!'); } /** * 'Pops' a value from a named stacker and returns it's value * * @param $stackerName Name of the stack * @return $value Value of the current stack entry * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the named stacker was not found * @throws BadMethodCallException If the named stacker is empty */ public function popNamed (string $stackerName) { // Validate parameter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName)); if (empty($stackerName)) { // No empty stack name throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Get the value //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Invoking this->getNamed(%s) ...', $stackerName)); $value = $this->getNamed($stackerName); // Call the protected method //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Invoking parent::popFirst(%s) ...', $stackerName)); parent::popFirst($stackerName); // Return the value //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value))); return $value; } /** * Get value from named stacker * * @param $stackerName Name of the stack * @return $value Value of last added value * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the named stacker was not found * @throws BadMethodCallException If the named stacker is empty */ public function getNamed (string $stackerName) { // Validate parameter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName)); if (empty($stackerName)) { // No empty stack name throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Call the protected method //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Invoking parent::getFirstValue(%s) ...', $stackerName)); $value = parent::getFirstValue($stackerName); // Return value //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value))); return $value; } }