00001 <?php 00026 class UserNameValidatorFilter extends BaseFilter implements Filterable { 00032 protected function __construct () { 00033 // Call parent constructor 00034 parent::__construct(__CLASS__); 00035 } 00036 00042 public final static function createUserNameValidatorFilter () { 00043 // Get a new instance 00044 $filterInstance = new UserNameValidatorFilter(); 00045 00046 // Return the instance 00047 return $filterInstance; 00048 } 00049 00057 public function execute (Requestable $requestInstance, Responseable $responseInstance) { 00058 // Get username from request 00059 $userName = $requestInstance->getRequestElement('username'); 00060 00061 // Is the username set? 00062 if (is_null($userName)) { 00063 // Not found in form so stop the filtering process 00064 $requestInstance->requestIsValid(false); 00065 00066 // Add a message to the response 00067 $responseInstance->addFatalMessage('username_unset'); 00068 00069 // Abort here 00070 return false; 00071 } elseif (empty($userName)) { 00072 // Empty field! 00073 $requestInstance->requestIsValid(false); 00074 00075 // Add a message to the response 00076 $responseInstance->addFatalMessage('username_empty'); 00077 00078 // Abort here 00079 return false; 00080 } elseif ($this->ifUserNameIsTaken($userName)) { 00081 // Username is already taken 00082 $requestInstance->requestIsValid(false); 00083 00084 // Add a message to the response 00085 $responseInstance->addFatalMessage('username_taken'); 00086 00087 // Abort here 00088 return false; 00089 } 00090 } 00091 00098 private function ifUserNameIsTaken ($userName) { 00099 // Default is already taken 00100 $alreadyTaken = true; 00101 00102 // Initialize instance 00103 $userInstance = null; 00104 00105 // Get a registry instance 00106 $registry = Registry::getRegistry(); 00107 00108 // Is the user already there? 00109 if ($registry->instanceExists('user')) { 00110 // Use the instance for checking for the email 00111 $userInstance = $registry->getInstance('user'); 00112 $userInstance->setUserName($userName); 00113 } else { 00114 // If this instance is created then the username *does* exist 00115 try { 00116 // Get a new instance 00117 $userInstance = call_user_func_array(array($this->getConfigInstance()->readConfig('user_class'), 'createMemberByUsername'), array($userName)); 00118 00119 // Remember this user instance in our registry for later usage 00120 $registry->addInstance('user', $userInstance); 00121 } catch (UsernameMissingException $e) { 00122 // User was not found 00123 } 00124 } 00125 00126 // Does the username exist? 00127 if ((is_null($userInstance)) || ($userInstance->ifUsernameExists() === false)) { 00128 // This username is still available 00129 $alreadyTaken = false; 00130 } // END - if 00131 00132 // Return the result 00133 return $alreadyTaken; 00134 } 00135 } 00136 00137 // [EOF] 00138 ?>
1.5.6