* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class BaseStateResolver extends BaseResolver { /** * Prefix for local, remote or other resolver */ private $statePrefix = ''; /** * Validated state name */ private $stateName = ''; /** * Protected constructor * * @param $className Name of the real class * @return void */ protected function __construct ($className) { // Call parent constructor parent::__construct($className); } /** * Setter for state prefix * * @param $statePrefix Last validated statePrefix * @return void */ protected final function setStatePrefix ($statePrefix) { $this->statePrefix = $statePrefix; } /** * Getter for state prefix * * @param $statePrefix Last validated statePrefix * @return void */ protected final function getStatePrefix () { return $this->statePrefix; } /** * Setter for state name * * @param $stateName Last validated state name * @return void */ protected final function setStateName ($stateName) { $this->stateName = $stateName; } /** * Getter for state name * * @return $stateName Last validated state name */ public final function getStateName () { return $this->stateName; } /** * "Loads" a given state and instances it if not yet cached. If the * state was not found an UnresolveableStateException is thrown * * @param $stateName A state name we shall look for * @return $stateInstance A loaded state instance * @throws UnresolveableStateException Thrown if even the requested * state class is missing (bad!) */ protected function loadState ($stateName) { // Init state instance $stateInstance = NULL; // Create state class name $className = $this->getStatePrefix() . '' . self::convertToClassName($stateName) . 'State'; // ... and set it $this->setClassName($className); // Is this class loaded? if (!class_exists($this->getClassName())) { // Throw an exception here throw new UnresolveableStateException(array($this, $stateName), self::EXCEPTION_INVALID_STATE); } // END - if // Initialize the state $stateInstance = ObjectFactory::createObjectByName( $this->getClassName(), array($this) ); // Return the result return $stateInstance; } /** * Checks whether the given state is valid * * @param $stateName The default state we shall execute * @return $isValid Whether the given state is valid * @throws EmptyVariableException Thrown if given state is not set * @throws DefaultStateException Thrown if default state was not found */ public function isStateValid ($stateName) { // By default nothing shall be valid $isValid = FALSE; // Is a state set? if (empty($stateName)) { // Then thrown an exception here throw new EmptyVariableException(array($this, 'stateName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING); } // END - if // Create class name $className = $this->statePrefix . self::convertToClassName($stateName) . 'State'; // Now, let us create the full name of the state class $this->setClassName($className); // Try it hard to get an state while ($isValid === FALSE) { // Is this class already loaded? if (class_exists($this->getClassName())) { // This class does exist. :-) $isValid = TRUE; } elseif ($this->getClassName() != $this->statePrefix . 'DefaultState') { // Set default state $this->setClassName($this->statePrefix . 'DefaultState'); } else { // All is tried, give it up here throw new DefaultStateException($this, self::EXCEPTION_DEFAULT_STATE_GONE); } } // END - while // Return the result return $isValid; } } // [EOF] ?>