More code merged from ship-simu
[mailer.git] / inc / classes / main / result / class_DatabaseResult.php
diff --git a/inc/classes/main/result/class_DatabaseResult.php b/inc/classes/main/result/class_DatabaseResult.php
new file mode 100644 (file)
index 0000000..3d48861
--- /dev/null
@@ -0,0 +1,114 @@
+<?php
+/**
+ * A database result class
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.3.0
+ * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.mxchange.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 DatabaseResult extends BaseFrameworkSystem {
+       /**
+        * Current position in array
+        */
+       private $currentPos = -1;
+
+       /**
+        * Current row
+        */
+       private $currentRow = null;
+
+       /**
+        * Result array
+        */
+       private $resultArray = array();
+
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+
+               // Set part description
+               $this->setObjectDescription("Database result");
+
+               // Create unique ID number
+               $this->generateUniqueId();
+
+               // Clean up a little
+               $this->removeNumberFormaters();
+               $this->removeSystemArray();
+       }
+
+       /**
+        * Creates an instance of this result by a provided result array
+        *
+        * @param       $resultArray            The array holding the result from query
+        * @return      $resultInstance         An instance of this class
+        */
+       public final static function createDatabaseResult (array $resultArray) {
+               // Get a new instance
+               $resultInstance = new DatabaseResult();
+
+               // Set the result array
+               $resultInstance->setResultArray($resultArray);
+
+               // Return the instance
+               return $resultInstance;
+       }
+
+       /**
+        * Setter for result array
+        *
+        * @param       $resultArray    The array holding the result from query
+        * @return      void
+        */
+       protected final function setResultArray (array $resultArray) {
+               $this->resultArray = $resultArray;
+       }
+
+       /**
+        * "Iterator" method next() to advance to the next valid entry. This method
+        * does also check if the result is invalid
+        *
+        * @return      $nextValid      Wether the next entry is valid
+        */
+       public function next () {
+               // Default is not valid
+               $nextValid = false;
+
+               // Is the result valid?
+               if ($this->resultArray['status'] === "ok") {
+                       // The status is fine so let's have a look for the next entry
+                       if (isset($this->resultArray['rows'][($this->currentPos + 1)])) {
+                               // Next entry found, so count one up and cache it
+                               $this->currentPos++;
+                               $this->currentRow = $this->resultArray['rows'][$this->currentPos];
+                               $nextValid = true;
+                       } // END - if
+               } // END - if
+
+               // Return the result
+               return $nextValid;
+       }
+}
+
+// [EOF]
+?>