]> git.mxchange.org Git - hub.git/blob - application/hub/main/resolver/state/class_BaseStateResolver.php
'public static final' is the right thing, some variables renamed to make clear what...
[hub.git] / application / hub / main / resolver / state / class_BaseStateResolver.php
1 <?php
2 /**
3  * A generic state resolver class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class BaseStateResolver extends BaseResolver {
25         /**
26          * Prefix for local, remote or other resolver
27          */
28         private $statePrefix = '';
29
30         /**
31          * Validated state name
32          */
33         private $stateName = '';
34
35         /**
36          * Protected constructor
37          *
38          * @param       $className      Name of the real class
39          * @return      void
40          */
41         protected function __construct ($className) {
42                 // Call parent constructor
43                 parent::__construct($className);
44         }
45
46         /**
47          * Setter for state prefix
48          *
49          * @param       $statePrefix    Last validated statePrefix
50          * @return      void
51          */
52         protected final function setStatePrefix ($statePrefix) {
53                 $this->statePrefix = $statePrefix;
54         }
55
56         /**
57          * Getter for state prefix
58          *
59          * @param       $statePrefix    Last validated statePrefix
60          * @return      void
61          */
62         protected final function getStatePrefix () {
63                 return $this->statePrefix;
64         }
65
66         /**
67          * Setter for state name
68          *
69          * @param       $stateName              Last validated state name
70          * @return      void
71          */
72         protected final function setStateName ($stateName) {
73                 $this->stateName = $stateName;
74         }
75
76         /**
77          * Getter for state name
78          *
79          * @return      $stateName      Last validated state name
80          */
81         public final function getStateName () {
82                 return $this->stateName;
83         }
84
85         /**
86          * "Loads" a given state and instances it if not yet cached. If the
87          * state was not found an InvalidStateException is thrown
88          *
89          * @param       $stateName                      A state name we shall look for
90          * @return      $stateInstance          A loaded state instance
91          * @throws      InvalidStateException   Thrown if even the requested
92          *                                                                      state class is missing (bad!)
93          */
94         protected function loadState ($stateName) {
95                 // Init state instance
96                 $stateInstance = null;
97
98                 // Create state class name
99                 $className = $this->getStatePrefix() . '' . $this->convertToClassName($stateName) . 'State';
100
101                 // ... and set it
102                 $this->setClassName($className);
103
104                 // Is this class loaded?
105                 if (!class_exists($this->getClassName())) {
106                         // Throw an exception here
107                         throw new InvalidStateException(array($this, $stateName), self::EXCEPTION_INVALID_STATE);
108                 } // END - if
109
110                 // Initialize the state
111                 $stateInstance = ObjectFactory::createObjectByName(
112                         $this->getClassName(),
113                         array($this)
114                 );
115
116                 // Return the result
117                 return $stateInstance;
118         }
119
120         /**
121          * Checks wether the given state is valid
122          *
123          * @param       $stateName                              The default state we shall execute
124          * @return      $isValid                                Wether the given state is valid
125          * @throws      EmptyVariableException  Thrown if given state is not set
126          * @throws      DefaultStateException   Thrown if default state was not found
127          */
128         public function isStateValid ($stateName) {
129                 // By default nothing shall be valid
130                 $isValid = false;
131
132                 // Is a state set?
133                 if (empty($stateName)) {
134                         // Then thrown an exception here
135                         throw new EmptyVariableException(array($this, 'stateName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
136                 } // END - if
137
138                 // Create class name
139                 $className = $this->statePrefix . $this->convertToClassName($stateName) . 'State';
140
141                 // Now, let us create the full name of the state class
142                 $this->setClassName($className);
143
144                 // Try it hard to get an state
145                 while ($isValid === false) {
146                         // Is this class already loaded?
147                         if (class_exists($this->getClassName())) {
148                                 // This class does exist. :-)
149                                 $isValid = true;
150                         } elseif ($this->getClassName() != $this->statePrefix.'DefaultNewsState') {
151                                 // Set default state
152                                 $this->setClassName($this->statePrefix . 'DefaultNewsState');
153                         } else {
154                                 // All is tried, give it up here
155                                 throw new DefaultStateException($this, self::EXCEPTION_DEFAULT_STATE_GONE);
156                         }
157                 } // END - while
158
159                 // Return the result
160                 return $isValid;
161         }
162 }
163
164 // [EOF]
165 ?>