]> git.mxchange.org Git - core.git/blob - framework/main/classes/stacker/fifo/class_FiFoStacker.php
Rewrite:
[core.git] / framework / main / classes / stacker / fifo / class_FiFoStacker.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Stack;
4
5 // Import SPL stuff
6 use \InvalidArgumentException;
7
8 /**
9  * A FiFo Stacker class
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  * @link                http://www.shipsimu.org
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  */
30 class FiFoStacker extends BaseStacker implements Stackable {
31         /**
32          * Protected constructor
33          *
34          * @return      void
35          */
36         private function __construct () {
37                 // Call parent constructor
38                 parent::__construct(__CLASS__);
39         }
40
41         /**
42          * Creates an instance of the class Stacker and prepares it for usage
43          *
44          * @return      $stackInstance  An instance of FiFoStacker
45          */
46         public static final function createFiFoStacker () {
47                 // Get a new instance
48                 $stackInstance = new FiFoStacker();
49
50                 // Init generic stacker
51                 $stackInstance->initStack('generic');
52
53                 // Return the prepared instance
54                 return $stackInstance;
55         }
56
57         /**
58          * Pushs a value on a named stacker
59          *
60          * @param       $stackerName    Name of the stack
61          * @param       $value                  Value to push on it
62          * @return      void
63          * @throws      InvalidArgumentException        If a parameter is invalid
64          * @throws      StackerFullException    If the stack is full
65          */
66         public function pushNamed (string $stackerName, $value) {
67                 // Validate parameter
68                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value)));
69                 if (empty($stackerName)) {
70                         // No empty stack name
71                         throw new InvalidArgumentException('Parameter "stackerName" is empty');
72                 }
73
74                 // Call the protected method
75                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Calling parent::addValueToStack(%s,%s) ...', $stackerName, gettype($value)));
76                 parent::addValueToStack($stackerName, $value);
77
78                 // Trace message
79                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-STACKER: EXIT!');
80         }
81
82         /**
83          * 'Pops' a value from a named stacker and returns it's value
84          *
85          * @param       $stackerName    Name of the stack
86          * @return      $value                  Value of the current stack entry
87          * @throws      InvalidArgumentException        If a parameter is invalid
88          * @throws      BadMethodCallException  If the named stacker was not found
89          * @throws      BadMethodCallException  If the named stacker is empty
90          */
91         public function popNamed (string $stackerName) {
92                 // Validate parameter
93                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName));
94                 if (empty($stackerName)) {
95                         // No empty stack name
96                         throw new InvalidArgumentException('Parameter "stackerName" is empty');
97                 }
98
99                 // Get the value
100                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Calling this->getNamed(%s) ...', $stackerName));
101                 $value = $this->getNamed($stackerName);
102
103                 // Call the protected method
104                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Calling parent::popFirst(%s) ...', $stackerName));
105                 parent::popFirst($stackerName);
106
107                 // Return the value
108                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value)));
109                 return $value;
110         }
111
112         /**
113          * Get value from named stacker
114          *
115          * @param       $stackerName    Name of the stack
116          * @return      $value                  Value of last added value
117          * @throws      InvalidArgumentException        If a parameter is invalid
118          * @throws      BadMethodCallException  If the named stacker was not found
119          * @throws      BadMethodCallException  If the named stacker is empty
120          */
121         public function getNamed (string $stackerName) {
122                 // Validate parameter
123                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName));
124                 if (empty($stackerName)) {
125                         // No empty stack name
126                         throw new InvalidArgumentException('Parameter "stackerName" is empty');
127                 }
128
129                 // Call the protected method
130                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Calling parent::getFirstValue(%s) ...', $stackerName));
131                 $value = parent::getFirstValue($stackerName);
132
133                 // Return value
134                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value)));
135                 return $value;
136         }
137
138 }