]> git.mxchange.org Git - core.git/blob - framework/main/classes/stacker/fifo/class_FiFoStacker.php
cf590a76e9b959a0ed614045ca42bb5e2514ba58
[core.git] / framework / main / classes / stacker / fifo / class_FiFoStacker.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Stack;
4
5 // Import framework-specific stuff
6 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
7
8 // Import SPL stuff
9 use \InvalidArgumentException;
10
11 /**
12  * A FiFo Stacker class
13  *
14  * @author              Roland Haeder <webmaster@shipsimu.org>
15  * @version             0.0.0
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
19  *
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.
24  *
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.
29  *
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/>.
32  */
33 class FiFoStacker extends BaseStacker implements Stackable {
34         /**
35          * Protected constructor
36          *
37          * @return      void
38          */
39         private function __construct () {
40                 // Call parent constructor
41                 parent::__construct(__CLASS__);
42         }
43
44         /**
45          * Creates an instance of the class Stacker and prepares it for usage
46          *
47          * @return      $stackInstance  An instance of FiFoStacker
48          */
49         public static final function createFiFoStacker () {
50                 // Get a new instance
51                 $stackInstance = new FiFoStacker();
52
53                 // Init generic stacker
54                 $stackInstance->initStack('generic');
55
56                 // Return the prepared instance
57                 return $stackInstance;
58         }
59
60         /**
61          * Pushs a value on a named stacker
62          *
63          * @param       $stackerName    Name of the stack
64          * @param       $value                  Value to push on it
65          * @return      void
66          * @throws      InvalidArgumentException        If a parameter is invalid
67          * @throws      StackerFullException    If the stack is full
68          */
69         public function pushNamed (string $stackerName, $value) {
70                 // Validate parameter
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);
78                 }
79
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);
83
84                 // Trace message
85                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-STACKER: EXIT!');
86         }
87
88         /**
89          * 'Pops' a value from a named stacker and returns it's value
90          *
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
96          */
97         public function popNamed (string $stackerName) {
98                 // Validate parameter
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);
103                 }
104
105                 // Get the value
106                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Invoking this->getNamed(%s) ...', $stackerName));
107                 $value = $this->getNamed($stackerName);
108
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);
112
113                 // Return the value
114                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value)));
115                 return $value;
116         }
117
118         /**
119          * Get value from named stacker
120          *
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
126          */
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);
133                 }
134
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);
138
139                 // Return value
140                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value)));
141                 return $value;
142         }
143
144 }