]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/main/result/class_DatabaseResult.php
fb02af3f1412e9f633c7da0ab028d3ca1cf8a725
[shipsimu.git] / inc / classes / main / result / class_DatabaseResult.php
1 <?php
2 /**
3  * A database result class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, SeekableIterator {
25         // Exception constants
26         const EXCEPTION_INVALID_DATABASE_RESULT = 0x0b0;
27
28         /**
29          * Current position in array
30          */
31         private $currentPos = -1;
32
33         /**
34          * Current row
35          */
36         private $currentRow = null;
37
38         /**
39          * Result array
40          */
41         private $resultArray = array();
42
43         /**
44          * Protected constructor
45          *
46          * @return      void
47          */
48         protected function __construct () {
49                 // Call parent constructor
50                 parent::__construct(__CLASS__);
51
52                 // Set part description
53                 $this->setObjectDescription("Database result");
54
55                 // Create unique ID number
56                 $this->generateUniqueId();
57
58                 // Clean up a little
59                 $this->removeNumberFormaters();
60                 $this->removeSystemArray();
61         }
62
63         /**
64          * Creates an instance of this result by a provided result array
65          *
66          * @param       $resultArray            The array holding the result from query
67          * @return      $resultInstance         An instance of this class
68          */
69         public final static function createDatabaseResult (array $resultArray) {
70                 // Get a new instance
71                 $resultInstance = new DatabaseResult();
72
73                 // Set the result array
74                 $resultInstance->setResultArray($resultArray);
75
76                 // Return the instance
77                 return $resultInstance;
78         }
79
80         /**
81          * Setter for result array
82          *
83          * @param       $resultArray    The array holding the result from query
84          * @return      void
85          */
86         protected final function setResultArray (array $resultArray) {
87                 $this->resultArray = $resultArray;
88         }
89
90         /**
91          * "Iterator" method next() to advance to the next valid entry. This method
92          * does also check if the result is invalid
93          *
94          * @return      $nextValid      Wether the next entry is valid
95          */
96         public function next () {
97                 // Default is not valid
98                 $nextValid = false;
99
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
105                                 $this->currentPos++;
106                                 $this->currentRow = $this->resultArray['rows'][$this->currentPos];
107                                 $nextValid = true;
108                         } // END - if
109                 } // END - if
110
111                 // Return the result
112                 return $nextValid;
113         }
114
115         /**
116          * Seeks for to a specified position
117          *
118          * @param       $index  Index to seek for
119          * @return      void
120          */
121         public function seek ($index) {
122                 // Rewind to beginning
123                 $this->rewind();
124
125                 // Search for the entry
126                 while ($this->currentPos < $index && ($this->valid())) {
127                         // Continue on
128                         $this->next();
129                 } // END - while
130         }
131
132         /**
133          * Gives back the current position or null if not found
134          *
135          * @return      $current        Current element to give back
136          */
137         public function current () {
138                 // Default is not found
139                 $current = null;
140
141                 // Does the current enty exist?
142                 if (isset($this->resultArray['rows'][$this->currentPos])) {
143                         // Then get it
144                         $current = $this->resultArray['rows'][$this->currentPos];
145                 } // END - if
146
147                 // Return the result
148                 return $current;
149         }
150
151         /**
152          * Checks if next() and rewind will give a valid result
153          *
154          * @return      $isValid Wether the next/rewind entry is valid
155          */
156         public function valid () {
157                 // By default nothing is valid
158                 $isValid = false;
159
160                 // Check if 
161                 if ((isset($this->resultArray['rows'][($this->currentPos + 1)])) && (isset($this->resultArray['rows'][0]))) {
162                         // All fine!
163                         $isValid = true;
164                 } // END - if
165
166                 // Return the result
167                 return $isValid;
168         }
169
170         /**
171          * Gets the current key of iteration
172          *
173          * @return      $currentPos     Key from iterator
174          */
175         public function key () {
176                 return $this->currentPos;
177         }
178
179         /**
180          * Rewind to the beginning
181          *
182          * @return      void
183          */
184         public function rewind () {
185                 $this->currentPos = -1;
186         }
187
188         /**
189          * Searches for an entry in the data result and returns it
190          *
191          * @param       $criteriaInstance       The criteria to look inside the data set
192          * @return      $result                         Found result entry
193          */
194         public function searchEntry (LocalSearchCriteria $criteriaInstance) {
195                 die(__METHOD__.": OK");
196         }
197 }
198
199 // [EOF]
200 ?>