3 * A database result class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, SeekableIterator {
25 // Exception constants
26 const EXCEPTION_INVALID_DATABASE_RESULT = 0x0b0;
29 * Current position in array
31 private $currentPos = -1;
36 private $currentRow = null;
41 private $resultArray = array();
44 * Protected constructor
48 protected function __construct () {
49 // Call parent constructor
50 parent::__construct(__CLASS__);
52 // Set part description
53 $this->setObjectDescription("Database result");
55 // Create unique ID number
56 $this->generateUniqueId();
59 $this->removeNumberFormaters();
60 $this->removeSystemArray();
64 * Creates an instance of this result by a provided result array
66 * @param $resultArray The array holding the result from query
67 * @return $resultInstance An instance of this class
69 public final static function createDatabaseResult (array $resultArray) {
71 $resultInstance = new DatabaseResult();
73 // Set the result array
74 $resultInstance->setResultArray($resultArray);
76 // Return the instance
77 return $resultInstance;
81 * Setter for result array
83 * @param $resultArray The array holding the result from query
86 protected final function setResultArray (array $resultArray) {
87 $this->resultArray = $resultArray;
91 * "Iterator" method next() to advance to the next valid entry. This method
92 * does also check if the result is invalid
94 * @return $nextValid Wether the next entry is valid
96 public function next () {
97 // Default is not valid
100 // Is the result valid?
101 if ($this->resultArray['status'] === "ok") {
102 // The status is fine so let's have a look for the next entry
103 if ($this->valid()) {
104 // Next entry found, so count one up and cache it
106 $this->currentRow = $this->resultArray['rows'][$this->currentPos];
116 * Seeks for to a specified position
118 * @param $index Index to seek for
121 public function seek ($index) {
122 // Rewind to beginning
125 // Search for the entry
126 while ($this->currentPos < $index && ($this->valid())) {
133 * Gives back the current position or null if not found
135 * @return $current Current element to give back
137 public function current () {
138 // Default is not found
141 // Does the current enty exist?
142 if (isset($this->resultArray['rows'][$this->currentPos])) {
144 $current = $this->resultArray['rows'][$this->currentPos];
152 * Checks if next() and rewind will give a valid result
154 * @return $isValid Wether the next/rewind entry is valid
156 public function valid () {
157 // By default nothing is valid
161 if ((isset($this->resultArray['rows'][($this->currentPos + 1)])) && (isset($this->resultArray['rows'][0]))) {
171 * Gets the current key of iteration
173 * @return $currentPos Key from iterator
175 public function key () {
176 return $this->currentPos;
180 * Rewind to the beginning
184 public function rewind () {
185 $this->currentPos = -1;
189 * Searches for an entry in the data result and returns it
191 * @param $criteriaInstance The criteria to look inside the data set
192 * @return $result Found result entry
194 public function searchEntry (LocalSearchCriteria $criteriaInstance) {