From: Roland Häder Date: Sat, 28 Jun 2008 21:19:17 +0000 (+0000) Subject: Generic methods created from non-generic implementation: X-Git-Url: https://git.mxchange.org/?p=shipsimu.git;a=commitdiff_plain;h=9aa87152298f44bf6ed5a50e952fbae993a46c3a Generic methods created from non-generic implementation: - Method updateDatabaseField() is now generic and throws an exception if the class ($this) does not implement interface "Updateable" - New exception DatabaseUpdateSupportException added for above method - Method getField() is also generic now --- diff --git a/.gitattributes b/.gitattributes index dccadbf..e3d7b8c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -204,8 +204,10 @@ inc/classes/exceptions/crypto/.htaccess -text inc/classes/exceptions/crypto/class_EncryptInvalidLengthException.php -text inc/classes/exceptions/crypto/class_EncryptMissingException.php -text inc/classes/exceptions/database/.htaccess -text +inc/classes/exceptions/database/class_ -text inc/classes/exceptions/database/class_DatabaseException.php -text inc/classes/exceptions/database/general/.htaccess -text +inc/classes/exceptions/database/general/class_DatabaseUpdateSupportException.php -text inc/classes/exceptions/database/general/class_SqlException.php -text inc/classes/exceptions/database/local_file/.htaccess -text inc/classes/exceptions/database/local_file/class_SavePathIsEmptyException.php -text @@ -315,6 +317,7 @@ inc/classes/interfaces/crypto/.htaccess -text inc/classes/interfaces/crypto/class_Cryptable.php -text inc/classes/interfaces/database/.htaccess -text inc/classes/interfaces/database/class_FrameworkDatabaseInterface.php -text +inc/classes/interfaces/database/class_Updateable.php -text inc/classes/interfaces/database/frontend/.htaccess -text inc/classes/interfaces/database/frontend/class_DatabaseFrontendInterface.php -text inc/classes/interfaces/database/middleware/.htaccess -text diff --git a/inc/classes/exceptions/database/class_ b/inc/classes/exceptions/database/class_ new file mode 100644 index 0000000..1124b16 --- /dev/null +++ b/inc/classes/exceptions/database/class_ @@ -0,0 +1,39 @@ + + * @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 . + */ +class ???Exception extends DatabaseException { + /** + * The constructor + * + * @param $message Message from the exception + * @param $code Code number for the exception + * @return void + */ + public function __construct ($message, $code) { + // Just call the parent constructor + parent::__construct($message, $code); + } +} + +// [EOF] +?> diff --git a/inc/classes/exceptions/database/general/class_DatabaseUpdateSupportException.php b/inc/classes/exceptions/database/general/class_DatabaseUpdateSupportException.php new file mode 100644 index 0000000..637b962 --- /dev/null +++ b/inc/classes/exceptions/database/general/class_DatabaseUpdateSupportException.php @@ -0,0 +1,45 @@ + + * @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 . + */ +class DatabaseUpdateSupportException extends DatabaseException { + /** + * The constructor + * + * @param $class Class trying the invalid database update + * @param $code Code number for the exception + * @return void + */ + public function __construct (FrameworkInterface $class, $code) { + // Generate message + $message = sprintf("[%s:%d] Database updated not allowed for this class.", + $class->__toString(), + $this->getLine() + ); + + // Just call the parent constructor + parent::__construct($message, $code); + } +} + +// [EOF] +?> diff --git a/inc/classes/interfaces/database/class_Updateable.php b/inc/classes/interfaces/database/class_Updateable.php new file mode 100644 index 0000000..61e64e3 --- /dev/null +++ b/inc/classes/interfaces/database/class_Updateable.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 Updateable extends FrameworkInterface { +} + +// +?> diff --git a/inc/classes/main/class_BaseFrameworkSystem.php b/inc/classes/main/class_BaseFrameworkSystem.php index 0707487..ad7bfaf 100644 --- a/inc/classes/main/class_BaseFrameworkSystem.php +++ b/inc/classes/main/class_BaseFrameworkSystem.php @@ -158,6 +158,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { const EXCEPTION_FILE_NOT_FOUND = 0x036; const EXCEPTION_ASSERTION_FAILED = 0x037; const EXCEPTION_FILE_CANNOT_BE_READ = 0x038; + const EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED = 0x039; /** * In the super constructor these system classes shall be ignored or else @@ -1159,6 +1160,106 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { // Return the stamp return $readable; } + + /** + * "Getter" for databse entry + * + * @return $entry An array with database entries + * @throws NullPointerException If the database result is not found + * @throws InvalidDatabaseResultException If the database result is invalid + */ + protected final function getDatabaseEntry () { + // Is there an instance? + if (is_null($this->getResultInstance())) { + // Throw an exception here + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } // END - if + + // Rewind it + $this->getResultInstance()->rewind(); + + // Do we have an entry? + if (!$this->getResultInstance()->valid()) { + throw new InvalidDatabaseResultException(array($this, $this->getResultInstance()), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT); + } // END - if + + // Get next entry + $this->getResultInstance()->next(); + + // Fetch it + $entry = $this->getResultInstance()->current(); + + // And return it + return $entry; + } + + /** + * Getter for field name + * + * @param $fieldName Field name which we shall get + * @return $fieldValue Field value from the user + * @throws NullPointerException If the result instance is null + */ + public final function getField ($fieldName) { + // Default field value + $fieldValue = null; + + // Get result instance + $resultInstance = $this->getResultInstance(); + + // Is this instance null? + if (is_null($resultInstance)) { + // Then the user instance is no longer valid (expired cookies?) + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } // END - if + + // Get current array + $fieldArray = $resultInstance->current(); + + // Does the field exist? + if (isset($fieldArray[$fieldName])) { + // Get it + $fieldValue = $fieldArray[$fieldName]; + } // END - if + + // Return it + return $fieldValue; + } + + /** + * Updates a given field with new value + * + * @param $fieldName Field to update + * @param $fieldValue New value to store + * @return void + * @throws DatabaseUpdateSupportException If this class does not support database updates + */ + public function updateDatabaseField ($fieldName, $fieldValue) { + // Is updating database fields allowed by interface? + if (!$this instanceof Updateable) { + // Update not supported! + throw new DatabaseUpdateSupportException($this, self::EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED); + } // END - if + + // Get a critieria instance + $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); + + // Add search criteria + $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName()); + $searchInstance->setLimit(1); + + // Now get another criteria + $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class'); + + // And add our both entries + $updateInstance->addCriteria($fieldName, $fieldValue); + + // Add the search criteria for searching for the right entry + $updateInstance->setSearchInstance($searchInstance); + + // Remember the update in database result + $this->getResultInstance()->add2UpdateQueue($updateInstance); + } } // [EOF] diff --git a/inc/classes/main/user/class_BaseUser.php b/inc/classes/main/user/class_BaseUser.php index cee4fc3..715544e 100644 --- a/inc/classes/main/user/class_BaseUser.php +++ b/inc/classes/main/user/class_BaseUser.php @@ -47,38 +47,6 @@ class BaseUser extends BaseFrameworkSystem { $this->removeSystemArray(); } - /** - * "Getter" for databse entry - * - * @return $entry An array with database entries - * @throws NullPointerException If the database result is not found - * @throws InvalidDatabaseResultException If the database result is invalid - */ - private function getDatabaseEntry () { - // Is there an instance? - if (is_null($this->getResultInstance())) { - // Throw new exception - throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); - } // END - if - - // Rewind it - $this->getResultInstance()->rewind(); - - // Do we have an entry? - if (!$this->getResultInstance()->valid()) { - throw new InvalidDatabaseResultException(array($this, $this->getResultInstance()), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT); - } // END - if - - // Get next entry - $this->getResultInstance()->next(); - - // Fetch it - $entry = $this->getResultInstance()->current(); - - // And return it - return $entry; - } - /** * Setter for username * @@ -256,45 +224,12 @@ class BaseUser extends BaseFrameworkSystem { if (isset($entry['pass_hash'])) { // Get it $passHash = $entry['pass_hash']; - } + } // END - if // And return the hash return $passHash; } - /** - * Getter for field name - * - * @param $fieldName Field name which we shall get - * @return $fieldValue Field value from the user - * @throws NullPointerException If the result instance is null - */ - public final function getField ($fieldName) { - // Default field value - $fieldValue = null; - - // Get result instance - $resultInstance = $this->getResultInstance(); - - // Is this instance null? - if (is_null($resultInstance)) { - // Then the user instance is no longer valid (expired cookies?) - throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); - } // END - if - - // Get current array - $fieldArray = $resultInstance->current(); - - // Does the field exist? - if (isset($fieldArray[$fieldName])) { - // Get it - $fieldValue = $fieldArray[$fieldName]; - } // END - if - - // Return it - return $fieldValue; - } - /** * Getter for primary key value * diff --git a/inc/classes/main/user/user/class_User.php b/inc/classes/main/user/user/class_User.php index 8343c33..8380c85 100644 --- a/inc/classes/main/user/user/class_User.php +++ b/inc/classes/main/user/user/class_User.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class User extends BaseUser implements ManageableUser, Registerable { +class User extends BaseUser implements ManageableUser, Registerable, Updateable { // Exceptions const EXCEPTION_USERNAME_NOT_FOUND = 0x150; const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151; @@ -136,10 +136,11 @@ class User extends BaseUser implements ManageableUser, Registerable { * @param $column Column we want to update * @param $value New value to store in database * @return void - * @todo 0% done + * @deprecated */ public function addUpdateData ($column, $value) { - $this->partialStub("Column={$column}, value={$value}"); + $this->deprecatedMethod("Please use updateDatabaseField() instead!"); + $this->updateDatabaseField($column, $value); } /** @@ -180,34 +181,6 @@ class User extends BaseUser implements ManageableUser, Registerable { $this->getResultInstance()->add2UpdateQueue($updateInstance); } - /** - * Updates a given field with new value - * - * @param $fieldName Field to update - * @param $fieldValue New value to store - * @return void - */ - public function updateDatabaseField ($fieldName, $fieldValue) { - // Get a critieria instance - $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); - - // Add search criteria - $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName()); - $searchInstance->setLimit(1); - - // Now get another criteria - $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class'); - - // And add our both entries - $updateInstance->addCriteria($fieldName, $fieldValue); - - // Add the search criteria for searching for the right entry - $updateInstance->setSearchInstance($searchInstance); - - // Remember the update in database result - $this->getResultInstance()->add2UpdateQueue($updateInstance); - } - /** * Flushs all pending updates to the database layer *