3 * A generic state resolver class
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class BaseStateResolver extends BaseResolver {
26 * Prefix for local, remote or other resolver
28 private $statePrefix = '';
31 * Validated state name
33 private $stateName = '';
36 * Protected constructor
38 * @param $className Name of the real class
41 protected function __construct ($className) {
42 // Call parent constructor
43 parent::__construct($className);
47 * Setter for state prefix
49 * @param $statePrefix Last validated statePrefix
52 protected final function setStatePrefix ($statePrefix) {
53 $this->statePrefix = $statePrefix;
57 * Getter for state prefix
59 * @param $statePrefix Last validated statePrefix
62 protected final function getStatePrefix () {
63 return $this->statePrefix;
67 * Setter for state name
69 * @param $stateName Last validated state name
72 protected final function setStateName ($stateName) {
73 $this->stateName = $stateName;
77 * Getter for state name
79 * @return $stateName Last validated state name
81 public final function getStateName () {
82 return $this->stateName;
86 * "Loads" a given state and instances it if not yet cached. If the
87 * state was not found an InvalidStateException is thrown
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!)
94 protected function loadState ($stateName) {
95 // Init state instance
96 $stateInstance = null;
98 // Create state class name
99 $className = $this->getStatePrefix() . '' . $this->convertToClassName($stateName) . 'State';
102 $this->setClassName($className);
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);
110 // Initialize the state
111 $stateInstance = ObjectFactory::createObjectByName(
112 $this->getClassName(),
117 return $stateInstance;
121 * Checks wether the given state is valid
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
128 public function isStateValid ($stateName) {
129 // By default nothing shall be valid
133 if (empty($stateName)) {
134 // Then thrown an exception here
135 throw new EmptyVariableException(array($this, 'stateName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
139 $className = $this->statePrefix . $this->convertToClassName($stateName) . 'State';
141 // Now, let us create the full name of the state class
142 $this->setClassName($className);
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. :-)
150 } elseif ($this->getClassName() != $this->statePrefix.'DefaultNewsState') {
152 $this->setClassName($this->statePrefix . 'DefaultNewsState');
154 // All is tried, give it up here
155 throw new DefaultStateException($this, self::EXCEPTION_DEFAULT_STATE_GONE);