3 namespace Org\Mxchange\CoreFramework\Stack;
8 * @author Roland Haeder <webmaster@shipsimu.org>
10 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
11 * @license GNU GPL 3.0 or any newer version
12 * @link http://www.shipsimu.org
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 class FiLoStacker extends BaseStacker implements Stackable {
29 * Protected constructor
33 private function __construct () {
34 // Call parent constructor
35 parent::__construct(__CLASS__);
39 * Creates an instance of the class Stacker and prepares it for usage
41 * @return $stackInstance An instance of FiLoStacker
43 public static final function createFiLoStacker () {
45 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILO-FILE-STACK: CALLED!');
46 $stackInstance = new FiLoStacker();
48 // Init the generic stacker
49 $stackInstance->initStack('generic');
51 // Return the prepared instance
52 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: stackInstance=%s - EXIT!', $stackInstance->__toString()));
53 return $stackInstance;
57 * Pushs a value on a named stacker
59 * @param $stackerName Name of the stack
60 * @param $value Value to push on it
62 * @throws InvalidArgumentException If a parameter is invalid
63 * @throws StackerFullException If the stack is full
65 public function pushNamed (string $stackerName, $value) {
67 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value)));
68 if (empty($stackerName)) {
69 // No empty stack name
70 throw new InvalidArgumentException('Parameter "stackerName" is empty');
73 // Call the protected method
74 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: Calling parent::addValueToStack(%s,%s) ...', $stackerName, gettype($value)));
75 parent::addValueToStack($stackerName, $value);
78 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILO-FILE-STACK: EXIT!');
82 * 'Pops' a value from a named stacker and returns it's value
84 * @param $stackerName Name of the stack
85 * @return $value Value of the current stack entry
86 * @throws InvalidArgumentException If a parameter is invalid
87 * @throws BadMethodCallException If the named stacker was not found
88 * @throws BadMethodCallException If the named stacker is empty
90 public function popNamed (string $stackerName) {
92 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
93 if (empty($stackerName)) {
94 // No empty stack name
95 throw new InvalidArgumentException('Parameter "stackerName" is empty');
99 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: this->getNamed(%s) ...', $stackerName));
100 $value = $this->getNamed($stackerName);
102 // Call the protected method
103 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: parent::popLast(%s) ...', $stackerName));
104 parent::popLast($stackerName);
107 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
112 * Get value from named stacker
114 * @param $stackerName Name of the stack
115 * @return $value Value of last added value
116 * @throws InvalidArgumentException If a parameter is invalid
117 * @throws BadMethodCallException If the named stacker was not found
118 * @throws BadMethodCallException If the named stacker is empty
120 public function getNamed (string $stackerName) {
121 // Validate parameter
122 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
123 if (empty($stackerName)) {
124 // No empty stack name
125 throw new InvalidArgumentException('Parameter "stackerName" is empty');
128 // Call the protected method
129 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: parent::getLastValue(%s) ...', $stackerName));
130 $value = parent::getLastValue($stackerName);
133 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));