]> git.mxchange.org Git - core.git/blob - framework/main/classes/stacker/filo/class_FiLoStacker.php
Continued:
[core.git] / framework / main / classes / stacker / filo / class_FiLoStacker.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Stack;
4
5 /**
6  * A FiLo Stacker class
7  *
8  * @author              Roland Haeder <webmaster@shipsimu.org>
9  * @version             0.0.0
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
13  *
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.
18  *
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.
23  *
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/>.
26  */
27 class FiLoStacker extends BaseStacker implements Stackable {
28         /**
29          * Protected constructor
30          *
31          * @return      void
32          */
33         private function __construct () {
34                 // Call parent constructor
35                 parent::__construct(__CLASS__);
36         }
37
38         /**
39          * Creates an instance of the class Stacker and prepares it for usage
40          *
41          * @return      $stackInstance  An instance of FiLoStacker
42          */
43         public static final function createFiLoStacker () {
44                 // Get a new instance
45                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILO-FILE-STACK: CALLED!');
46                 $stackInstance = new FiLoStacker();
47
48                 // Init the generic stacker
49                 $stackInstance->initStack('generic');
50
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;
54         }
55
56         /**
57          * Pushs a value on a named stacker
58          *
59          * @param       $stackerName    Name of the stack
60          * @param       $value                  Value to push on it
61          * @return      void
62          * @throws      InvalidArgumentException If a parameter is invalid
63          * @throws      StackerFullException    If the stack is full
64          */
65         public function pushNamed (string $stackerName, $value) {
66                 // Validate parameter
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');
71                 }
72
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);
76
77                 // Trace message
78                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILO-FILE-STACK: EXIT!');
79         }
80
81         /**
82          * 'Pops' a value from a named stacker and returns it's value
83          *
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
89          */
90         public function popNamed (string $stackerName) {
91                 // Validate parameter
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');
96                 }
97
98                 // Get the value
99                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: this->getNamed(%s) ...', $stackerName));
100                 $value = $this->getNamed($stackerName);
101
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);
105
106                 // Return the value
107                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
108                 return $value;
109         }
110
111         /**
112          * Get value from named stacker
113          *
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
119          */
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');
126                 }
127
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);
131
132                 // Return the value
133                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
134                 return $value;
135         }
136
137 }