Generic methods created from non-generic implementation:
authorRoland Häder <roland@mxchange.org>
Sat, 28 Jun 2008 21:19:17 +0000 (21:19 +0000)
committerRoland Häder <roland@mxchange.org>
Sat, 28 Jun 2008 21:19:17 +0000 (21:19 +0000)
- 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

.gitattributes
inc/classes/exceptions/database/class_ [new file with mode: 0644]
inc/classes/exceptions/database/general/class_DatabaseUpdateSupportException.php [new file with mode: 0644]
inc/classes/interfaces/database/class_Updateable.php [new file with mode: 0644]
inc/classes/main/class_BaseFrameworkSystem.php
inc/classes/main/user/class_BaseUser.php
inc/classes/main/user/user/class_User.php

index dccadbfe87970bb1c548de91d4c1ef1cfc4951ce..e3d7b8cc61fa77ecb0c68e0e76881cf8ffc2442d 100644 (file)
@@ -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/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/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
 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/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
 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 (file)
index 0000000..1124b16
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+/**
+ * 
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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 (file)
index 0000000..637b962
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+/**
+ * An exception thrown when a class tries an unallowed database update
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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 (file)
index 0000000..61e64e3
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/**
+ * An interface for classes which are allowed to update database records
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+interface Updateable extends FrameworkInterface {
+}
+
+//
+?>
index 0707487b3c3ae76f659fa89f8277d0aaf078ac68..ad7bfafd588c9bd05c5dc4805705741b039b1185 100644 (file)
@@ -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_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
 
        /**
         * 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;
        }
                // 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]
 }
 
 // [EOF]
index cee4fc31d628d8b2c00cb39414c7554f95a1e8a7..715544eb65bb635dd29c71dcd09a61288ed3c561 100644 (file)
@@ -47,38 +47,6 @@ class BaseUser extends BaseFrameworkSystem {
                $this->removeSystemArray();
        }
 
                $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
         *
        /**
         * Setter for username
         *
@@ -256,45 +224,12 @@ class BaseUser extends BaseFrameworkSystem {
                if (isset($entry['pass_hash'])) {
                        // Get it
                        $passHash = $entry['pass_hash'];
                if (isset($entry['pass_hash'])) {
                        // Get it
                        $passHash = $entry['pass_hash'];
-               }
+               } // END - if
 
                // And return the hash
                return $passHash;
        }
 
 
                // 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
         *
        /**
         * Getter for primary key value
         *
index 8343c33ebda6dc624c13d3c214a19e02cc7b3a31..8380c857273acd5dc8d750025755ffbf20da2e25 100644 (file)
@@ -21,7 +21,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
  * 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 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;
        // 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
         * @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) {
         */
        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);
        }
 
                $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
         *
        /**
         * Flushs all pending updates to the database layer
         *