]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/resolver/state/class_BaseStateResolver.php
Task handler added, a lot rewrites
[hub.git] / application / hub / main / resolver / state / class_BaseStateResolver.php
diff --git a/application/hub/main/resolver/state/class_BaseStateResolver.php b/application/hub/main/resolver/state/class_BaseStateResolver.php
new file mode 100644 (file)
index 0000000..a7dbc97
--- /dev/null
@@ -0,0 +1,165 @@
+<?php
+/**
+ * A generic state resolver class
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.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 <http://www.gnu.org/licenses/>.
+ */
+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 InvalidStateException is thrown
+        *
+        * @param       $stateName                      A state name we shall look for
+        * @return      $stateInstance          A loaded state instance
+        * @throws      InvalidStateException   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() . '' . $this->convertToClassName($stateName) . 'State';
+
+               // ... and set it
+               $this->setClassName($className);
+
+               // Is this class loaded?
+               if (!class_exists($this->getClassName())) {
+                       // Throw an exception here
+                       throw new InvalidStateException(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 wether the given state is valid
+        *
+        * @param       $stateName                              The default state we shall execute
+        * @return      $isValid                                Wether 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 . $this->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.'DefaultNewsState') {
+                               // Set default state
+                               $this->setClassName($this->statePrefix . 'DefaultNewsState');
+                       } 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]
+?>