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 {
26 * Current position in array
28 private $currentPos = -1;
33 private $currentRow = null;
38 private $resultArray = array();
41 * Protected constructor
45 protected function __construct () {
46 // Call parent constructor
47 parent::__construct(__CLASS__);
49 // Set part description
50 $this->setObjectDescription("Database result");
52 // Create unique ID number
53 $this->generateUniqueId();
56 $this->removeNumberFormaters();
57 $this->removeSystemArray();
61 * Creates an instance of this result by a provided result array
63 * @param $resultArray The array holding the result from query
64 * @return $resultInstance An instance of this class
66 public final static function createDatabaseResult (array $resultArray) {
68 $resultInstance = new DatabaseResult();
70 // Set the result array
71 $resultInstance->setResultArray($resultArray);
73 // Return the instance
74 return $resultInstance;
78 * Setter for result array
80 * @param $resultArray The array holding the result from query
83 protected final function setResultArray (array $resultArray) {
84 $this->resultArray = $resultArray;
88 * "Iterator" method next() to advance to the next valid entry. This method
89 * does also check if the result is invalid
91 * @return $nextValid Wether the next entry is valid
93 public function next () {
94 // Default is not valid
97 // Is the result valid?
98 if ($this->resultArray['status'] === "ok") {
99 // The status is fine so let's have a look for the next entry
100 if ($this->valid()) {
101 // Next entry found, so count one up and cache it
103 $this->currentRow = $this->resultArray['rows'][$this->currentPos];
113 * Seeks for to a specified position
115 * @param $index Index to seek for
118 public function seek ($index) {
119 // Rewind to beginning
122 // Search for the entry
123 while ($this->currentPos < $index && ($this->valid())) {
130 * Gives back the current position or null if not found
132 * @return $current Current element to give back
134 public function current () {
135 // Default is not found
138 // Does the current enty exist?
139 if (isset($this->resultArray['rows'][$this->currentPos])) {
141 $current = $this->resultArray['rows'][$this->currentPos];
149 * Checks if next() and rewind will give a valid result
151 * @return $isValid Wether the next/rewind entry is valid
153 public function valid () {
154 // By default nothing is valid
158 if ((isset($this->resultArray['rows'][($this->currentPos + 1)])) && (isset($this->resultArray['rows'][0]))) {
168 * Gets the current key of iteration
170 * @return $currentPos Key from iterator
172 public function key () {
173 return $this->currentPos;
177 * Rewind to the beginning
181 public function rewind () {
182 $this->currentPos = 0;
186 * Searches for an entry in the data result and returns it
188 * @param $criteriaInstance The criteria to look inside the data set
189 * @return $result Found result entry
191 public function searchEntry (LocalSearchCriteria $criteriaInstance) {