]> git.mxchange.org Git - hub.git/blobdiff - inc/classes/main/filter/validator/class_UserNameValidatorFilter.php
Code merged from ship-simu repository
[hub.git] / inc / classes / main / filter / validator / class_UserNameValidatorFilter.php
index 1c7c54afc820da98a0e2332fafd82737f416aa5a..8d0445feb4696d5fe43ea9b68edeb3729a358104 100644 (file)
@@ -1,13 +1,12 @@
 <?php
 /**
  * A concrete filter for validating the username. This filter may intercept the
- * filter chain if no username is given or if the supplied username has an
- * invalid form. It could also intercept the filter chain if the username is
- * already taken.
+ * filter chain if no username is given or if supplied username has an invalid
+ * form. It could also intercept our filter chain if username is already taken.
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, this is free software
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
  * 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 UserNameValidatorFilter extends BaseFrameworkSystem implements Filterable {
+class UserNameValidatorFilter extends BaseFilter implements Filterable {
        /**
-        * Private constructor
+        * Protected constructor
         *
         * @return      void
         */
        protected function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
-
-               // Set part description
-               $this->setObjectDescription("A filter for username validation");
-
-               // Create unique ID number
-               $this->createUniqueID();
-
-               // Clean up a little
-               $this->removeNumberFormaters();
-               $this->removeSystemArray();
        }
 
        /**
         * Creates an instance of this filter class
         *
-        * @return      $filterInstance         An instance of this filter class
+        * @param       $controllerInstance             An instance of a Controller class
+        * @return      $filterInstance                 An instance of this filter class
         */
-       public final static function createUserNameValidatorFilter () {
+       public final static function createUserNameValidatorFilter (Controller $controllerInstance) {
                // Get a new instance
                $filterInstance = new UserNameValidatorFilter();
 
+               // Set the controller
+               $filterInstance->setControllerInstance($controllerInstance);
+
                // Return the instance
                return $filterInstance;
        }
@@ -74,7 +67,7 @@ class UserNameValidatorFilter extends BaseFrameworkSystem implements Filterable
                        // Not found in form so stop the filtering process
                        $requestInstance->requestIsValid(false);
 
-                       // Set a message for the response
+                       // Add a message to the response
                        $responseInstance->addFatalMessage('username_unset');
 
                        // Abort here
@@ -83,7 +76,7 @@ class UserNameValidatorFilter extends BaseFrameworkSystem implements Filterable
                        // Empty field!
                        $requestInstance->requestIsValid(false);
 
-                       // Set a message for the response
+                       // Add a message to the response
                        $responseInstance->addFatalMessage('username_empty');
 
                        // Abort here
@@ -92,7 +85,7 @@ class UserNameValidatorFilter extends BaseFrameworkSystem implements Filterable
                        // Username is already taken
                        $requestInstance->requestIsValid(false);
 
-                       // Set a message for the response
+                       // Add a message to the response
                        $responseInstance->addFatalMessage('username_taken');
 
                        // Abort here
@@ -110,18 +103,36 @@ class UserNameValidatorFilter extends BaseFrameworkSystem implements Filterable
                // Default is already taken
                $alreadyTaken = true;
 
-               // Try to create a user instance
-               try {
+               // Initialize instance
+               $userInstance = null;
+
+               // Get a registry instance
+               $registry = Registry::getRegistry();
+
+               // Is the user already there?
+               if ($registry->instanceExists('user')) {
+                       // Use the instance for checking for the email
+                       $userInstance = $registry->getInstance('user');
+                       $userInstance->setUserName($userName);
+               } else {
                        // If this instance is created then the username *does* exist
-                       $userInstance = User::createUserByUsername($userName);
-               } catch (UsernameMissingException $e) {
-                       // Okay, this user is missing!
-                       $alreadyTaken = false;
-               } catch (FrameworkException $e) {
-                       // Something bad happend
-                       ApplicationEntryPoint::app_die(sprintf("Exception: %s", $e->__toString()));
+                       try {
+                               // Get a new instance
+                               $userInstance = call_user_func_array(array($this->getConfigInstance()->readConfig('user_class'), 'createMemberByUsername'), array($userName));
+
+                               // Remember this user instance in our registry for later usage
+                               $registry->addInstance('user', $userInstance);
+                       } catch (UsernameMissingException $e) {
+                               // User was not found
+                       }
                }
 
+               // Does the username exist?
+               if ((is_null($userInstance)) || ($userInstance->ifUsernameExists() === false)) {
+                       // This username is still available
+                       $alreadyTaken = false;
+               } // END - if
+
                // Return the result
                return $alreadyTaken;
        }