From aae7c6497b271b3479c6540f31319955fd675e1b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 2 Jun 2008 18:13:56 +0000 Subject: [PATCH] Registerable instances now possible, some fixes in filters --- .gitattributes | 3 ++ .../web/class_WebShipsimuRegisterCommand.php | 2 +- inc/classes/interfaces/registry/.htaccess | 1 + .../interfaces/registry/class_Register.php | 35 ++++++++++++ .../registry/class_Registerable.php | 28 ++++++++++ .../main/class_BaseFrameworkSystem.php | 11 ++-- .../validator/class_EmailValidatorFilter.php | 11 ++-- .../class_UserNameValidatorFilter.php | 17 ++++-- inc/classes/main/registry/class_Registry.php | 32 ++++++++++- inc/classes/main/user/class_User.php | 54 ++++++++++++++++--- 10 files changed, 173 insertions(+), 21 deletions(-) create mode 100644 inc/classes/interfaces/registry/.htaccess create mode 100644 inc/classes/interfaces/registry/class_Register.php create mode 100644 inc/classes/interfaces/registry/class_Registerable.php diff --git a/.gitattributes b/.gitattributes index 056bd05..7a53fb0 100644 --- a/.gitattributes +++ b/.gitattributes @@ -256,6 +256,9 @@ inc/classes/interfaces/io/output/.htaccess -text inc/classes/interfaces/io/output/class_OutputStreamer.php -text inc/classes/interfaces/language/.htaccess -text inc/classes/interfaces/language/class_ManageableLanguage.php -text +inc/classes/interfaces/registry/.htaccess -text +inc/classes/interfaces/registry/class_Register.php -text +inc/classes/interfaces/registry/class_Registerable.php -text inc/classes/interfaces/request/.htaccess -text inc/classes/interfaces/request/class_Requestable.php -text inc/classes/interfaces/resolver/.htaccess -text diff --git a/application/ship-simu/main/commands/web/class_WebShipsimuRegisterCommand.php b/application/ship-simu/main/commands/web/class_WebShipsimuRegisterCommand.php index a419393..29a6da4 100644 --- a/application/ship-simu/main/commands/web/class_WebShipsimuRegisterCommand.php +++ b/application/ship-simu/main/commands/web/class_WebShipsimuRegisterCommand.php @@ -59,8 +59,8 @@ class WebShipsimuRegisterCommand extends BaseCommand implements Commandable { $controllerInstance = $resolverInstance->getControllerInstance(); // @TODO Add some more pre/post filters to the controller - $controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('username_validator')); $controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('email_validator')); + $controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('username_validator')); // Return the prepared instance return $commandInstance; diff --git a/inc/classes/interfaces/registry/.htaccess b/inc/classes/interfaces/registry/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/inc/classes/interfaces/registry/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/interfaces/registry/class_Register.php b/inc/classes/interfaces/registry/class_Register.php new file mode 100644 index 0000000..770755e --- /dev/null +++ b/inc/classes/interfaces/registry/class_Register.php @@ -0,0 +1,35 @@ + + * @version 0.0.0 + * @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 + * + * 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 . + */ +interface Register extends FrameworkInterface { + /** + * Checks wether an instance key was found + * + * @param $instanceKey The key holding an instance in the registry + * @return $exists Wether the key exists in the registry + */ + function instanceExists ($instanceKey); +} + +// +?> diff --git a/inc/classes/interfaces/registry/class_Registerable.php b/inc/classes/interfaces/registry/class_Registerable.php new file mode 100644 index 0000000..f1d60f2 --- /dev/null +++ b/inc/classes/interfaces/registry/class_Registerable.php @@ -0,0 +1,28 @@ + + * @version 0.0.0 + * @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 + * + * 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 . + */ +interface Registerable extends FrameworkInterface { +} + +// +?> diff --git a/inc/classes/main/class_BaseFrameworkSystem.php b/inc/classes/main/class_BaseFrameworkSystem.php index 2862772..0d82132 100644 --- a/inc/classes/main/class_BaseFrameworkSystem.php +++ b/inc/classes/main/class_BaseFrameworkSystem.php @@ -249,6 +249,9 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { } elseif (is_string($arg)) { // String $argsString .= "\"".$arg."\"(string)"; + } elseif (is_null($arg)) { + // Null + $argsString .= "(null)"; } else { // Unknown type (please report!) $argsString .= $arg."(unknown!)"; @@ -629,10 +632,10 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { * Formats computer generated price values into human-understandable formats * with thousand and decimal seperators. * - * @param $value The in computer format value for a price - * @param $currency The currency symbol (use HTML-valid characters!) - * @param $decNum Number of decimals after commata - * @return $price The for the current language formated price string + * @param $value The in computer format value for a price + * @param $currency The currency symbol (use HTML-valid characters!) + * @param $decNum Number of decimals after commata + * @return $price The for the current language formated price string * @throws MissingDecimalsThousandsSeperatorException If decimals or * thousands seperator * is missing diff --git a/inc/classes/main/filter/validator/class_EmailValidatorFilter.php b/inc/classes/main/filter/validator/class_EmailValidatorFilter.php index a2f5628..8d9117b 100644 --- a/inc/classes/main/filter/validator/class_EmailValidatorFilter.php +++ b/inc/classes/main/filter/validator/class_EmailValidatorFilter.php @@ -135,20 +135,23 @@ class EmailValidatorFilter extends BaseFrameworkSystem implements Filterable { // Default is already taken $alreadyTaken = true; + // Initialize instance + $userInstance = null; + // Get a registry instance $registry = Registry::getInstance(); // Is the user already there? - if ($registry->keyExists('userInstance')) { + if ($registry->instanceExists('user')) { // Use the instance for checking for the email - $userInstance = $registry->getInstance('userInstance'); + $userInstance = $registry->getInstance('user'); $userInstance->setEmailAddress($email); } else { // If this instance is created then the username *does* exist - $userInstance = User::createUserByEmail($userName); + $userInstance = User::createUserByEmail($email); // Remember this user instance in our registry for later usage - $registry->addInstance('userInstance', $userInstance); + $registry->addInstance('user', $userInstance); } // Does the email exist? diff --git a/inc/classes/main/filter/validator/class_UserNameValidatorFilter.php b/inc/classes/main/filter/validator/class_UserNameValidatorFilter.php index cb82296..38471cd 100644 --- a/inc/classes/main/filter/validator/class_UserNameValidatorFilter.php +++ b/inc/classes/main/filter/validator/class_UserNameValidatorFilter.php @@ -110,6 +110,9 @@ class UserNameValidatorFilter extends BaseFrameworkSystem implements Filterable // Default is already taken $alreadyTaken = true; + // Initialize instance + $userInstance = null; + // Get a registry instance $registry = Registry::getInstance(); @@ -119,14 +122,18 @@ class UserNameValidatorFilter extends BaseFrameworkSystem implements Filterable $userInstance = $registry->getInstance('user'); } else { // If this instance is created then the username *does* exist - $userInstance = User::createUserByUsername($userName); - - // Remember this user instance in our registry for later usage - $registry->addInstance('user', $userInstance); + try { + $userInstance = User::createUserByUsername($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 (!$userInstance->ifUsernameExists()) { + if ((is_null($userInstance)) || (!$userInstance->ifUsernameExists())) { // This username is still available $alreadyTaken = false; } diff --git a/inc/classes/main/registry/class_Registry.php b/inc/classes/main/registry/class_Registry.php index 8c69c2d..0ceda2c 100644 --- a/inc/classes/main/registry/class_Registry.php +++ b/inc/classes/main/registry/class_Registry.php @@ -21,12 +21,17 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class Registry extends BaseFrameworkSystem { +class Registry extends BaseFrameworkSystem implements Register { /** * Instance of this class */ private static $selfInstance = null; + /** + * Instance registry + */ + private $instanceRegistry = array(); + /** * Protected constructor * @@ -62,6 +67,31 @@ class Registry extends BaseFrameworkSystem { // Return the instance return self::$selfInstance; } + + /** + * Checks wether an instance key was found + * + * @param $instanceKey The key holding an instance in the registry + * @return $exists Wether the key exists in the registry + */ + public function instanceExists ($instanceKey) { + // Does this key exists? + $exists = (isset($this->instanceRegistry[$instanceKey])); + + // Return the result + return $exists; + } + + /** + * Adds/overwrites a new instance to the registry at the given key + * + * @param $instanceKey The key to identify the instance + * @param $objectInstance An instance we shall store + * @return void + */ + public function addInstance ($instanceKey, Registerable $objectInstance) { + $this->instanceRegistry[$instanceKey] = $objectInstance; + } } // [EOF] diff --git a/inc/classes/main/user/class_User.php b/inc/classes/main/user/class_User.php index 7d1d060..05afd6d 100644 --- a/inc/classes/main/user/class_User.php +++ b/inc/classes/main/user/class_User.php @@ -21,11 +21,16 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class User extends BaseFrameworkSystem implements ManageableUser { +class User extends BaseFrameworkSystem implements ManageableUser, Registerable { /** - * Username + * Username of current user */ - private $username = ""; + private $userName = ""; + + /** + * Email of current user + */ + private $email = ""; // Exceptions const EXCEPTION_USERNAME_NOT_FOUND = 0xd00; @@ -58,7 +63,7 @@ class User extends BaseFrameworkSystem implements ManageableUser { * factory method will check if the username is already taken and if not * so it will throw an exception. * - * @param $username Username we need a class instance for + * @param $userName Username we need a class instance for * @return $userInstance An instance of this user class * @throws UsernameMissingException If the username does not exist */ @@ -79,6 +84,24 @@ class User extends BaseFrameworkSystem implements ManageableUser { return $userInstance; } + /** + * Creates an instance of this user class by a provided email address. This + * factory method will not check if the email address is there. + * + * @param $email Email address of the user + * @return $userInstance An instance of this user class + */ + public final static function createUserByEmail ($email) { + // Get a new instance + $userInstance = new User(); + + // Set the username + $userInstance->setEmail($email); + + // Return the instance + return $userInstance; + } + /** * Setter for username * @@ -86,7 +109,17 @@ class User extends BaseFrameworkSystem implements ManageableUser { * @return void */ protected final function setUsername ($userName) { - $this->userNane = $userName; + $this->UserName = $userName; + } + + /** + * Setter for email + * + * @param $email The email to set + * @return void + */ + protected final function setEmail ($email) { + $this->userName = $email; } /** @@ -95,7 +128,16 @@ class User extends BaseFrameworkSystem implements ManageableUser { * @return $userName The username to get */ public final function getUsername () { - return $this->userNane; + return $this->userName; + } + + /** + * Getter for email + * + * @return $email The email to get + */ + public final function getEmail () { + return $this->email; } /** -- 2.39.2