3 namespace Org\Mxchange\CoreFramework\Stack;
5 // Import framework-specific stuff
6 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
9 use \InvalidArgumentException;
12 * A FiFo Stacker class
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class FiFoStacker extends BaseStacker implements Stackable {
35 * Protected constructor
39 private function __construct () {
40 // Call parent constructor
41 parent::__construct(__CLASS__);
45 * Creates an instance of the class Stacker and prepares it for usage
47 * @return $stackInstance An instance of FiFoStacker
49 public static final function createFiFoStacker () {
51 $stackInstance = new FiFoStacker();
53 // Init generic stacker
54 $stackInstance->initStack('generic');
56 // Return the prepared instance
57 return $stackInstance;
61 * Pushs a value on a named stacker
63 * @param $stackerName Name of the stack
64 * @param $value Value to push on it
66 * @throws InvalidArgumentException If a parameter is invalid
67 * @throws StackerFullException If the stack is full
69 public function pushNamed (string $stackerName, $value) {
71 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value)));
72 if (empty($stackerName)) {
73 // No empty stack name
74 throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
75 } elseif (is_resource($value) || is_array($value) || is_object($value)) {
76 // Unsupported variable type
77 throw new InvalidArgumentException(sprintf('value[]=%s is not supported', gettype($value)), FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
80 // Call the protected method
81 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Invoking parent::addValueToStack(%s,%s) ...', $stackerName, gettype($value)));
82 parent::addValueToStack($stackerName, $value);
85 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-STACKER: EXIT!');
89 * 'Pops' a value from a named stacker and returns it's value
91 * @param $stackerName Name of the stack
92 * @return $value Value of the current stack entry
93 * @throws InvalidArgumentException If a parameter is invalid
94 * @throws BadMethodCallException If the named stacker was not found
95 * @throws BadMethodCallException If the named stacker is empty
97 public function popNamed (string $stackerName) {
99 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName));
100 if (empty($stackerName)) {
101 // No empty stack name
102 throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
106 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Invoking this->getNamed(%s) ...', $stackerName));
107 $value = $this->getNamed($stackerName);
109 // Call the protected method
110 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Invoking parent::popFirst(%s) ...', $stackerName));
111 parent::popFirst($stackerName);
114 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value)));
119 * Get value from named stacker
121 * @param $stackerName Name of the stack
122 * @return $value Value of last added value
123 * @throws InvalidArgumentException If a parameter is invalid
124 * @throws BadMethodCallException If the named stacker was not found
125 * @throws BadMethodCallException If the named stacker is empty
127 public function getNamed (string $stackerName) {
128 // Validate parameter
129 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName));
130 if (empty($stackerName)) {
131 // No empty stack name
132 throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
135 // Call the protected method
136 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Invoking parent::getFirstValue(%s) ...', $stackerName));
137 $value = parent::getFirstValue($stackerName);
140 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value)));