[12] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | // ----------------------------------------------------------------------------------
|
---|
| 4 | // Class: RdqlResultIterator
|
---|
| 5 | // ----------------------------------------------------------------------------------
|
---|
| 6 |
|
---|
| 7 | /**
|
---|
| 8 | * Iterator for traversing Rdql results.
|
---|
| 9 | * This class can be used for iterating forward and backward trough Rdql results.
|
---|
| 10 | * It should be instanced using the rdqlQueryAsIterator() method of a MemModel or a DBModel.
|
---|
| 11 | *
|
---|
| 12 | * @version $Id: RdqlResultIterator.php 268 2006-05-15 05:28:09Z tgauss $
|
---|
| 13 | * @author Daniel Westphal <mail@d-westphal.de>, Chris Bizer <chris@bizer.de>
|
---|
| 14 | *
|
---|
| 15 | * @package rdql
|
---|
| 16 | * @access public
|
---|
| 17 | *
|
---|
| 18 | */
|
---|
| 19 | class RdqlResultIterator extends Object {
|
---|
| 20 |
|
---|
| 21 | /**
|
---|
| 22 | * Reference to the Rdql result
|
---|
| 23 | * @var array RdqlResult
|
---|
| 24 | * @access private
|
---|
| 25 | */
|
---|
| 26 | var $RdqlResult;
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * Current position
|
---|
| 30 | * RdqlResultIterator does not use the build in PHP array iterator,
|
---|
| 31 | * so you can use serveral iterators on a single Rdql result.
|
---|
| 32 | * @var integer
|
---|
| 33 | * @access private
|
---|
| 34 | */
|
---|
| 35 | var $position;
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * Constructor
|
---|
| 40 | *
|
---|
| 41 | * @param object RdqlResult
|
---|
| 42 | * @access public
|
---|
| 43 | */
|
---|
| 44 | function RdqlResultIterator(&$RdqlResult) {
|
---|
| 45 |
|
---|
| 46 | $noResult = TRUE;
|
---|
| 47 | foreach($RdqlResult[0] as $value)
|
---|
| 48 | if ($value != NULL) {
|
---|
| 49 | $noResult = FALSE;
|
---|
| 50 | break;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | if($noResult)
|
---|
| 54 | $this->RdqlResult = NULL;
|
---|
| 55 | else
|
---|
| 56 | $this->RdqlResult = $RdqlResult;
|
---|
| 57 |
|
---|
| 58 | $this->position = -1;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * Returns the labels of the result as array.
|
---|
| 63 | * @return array of strings with the result labels OR null if there are no results.
|
---|
| 64 | * @access public
|
---|
| 65 | */
|
---|
| 66 | function getResultLabels() {
|
---|
| 67 | if(count($this->RdqlResult)>0) {
|
---|
| 68 | return array_keys($this->RdqlResult[0]);
|
---|
| 69 | } else return null;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * Returns the number of results.
|
---|
| 74 | * @return integer
|
---|
| 75 | * @access public
|
---|
| 76 | */
|
---|
| 77 | function countResults() {
|
---|
| 78 |
|
---|
| 79 | return count($this->RdqlResult);
|
---|
| 80 |
|
---|
| 81 | }
|
---|
| 82 | /**
|
---|
| 83 | * Returns TRUE if there are more results.
|
---|
| 84 | * @return boolean
|
---|
| 85 | * @access public
|
---|
| 86 | */
|
---|
| 87 | function hasNext() {
|
---|
| 88 | if ($this->position < count($this->RdqlResult) - 1 ) {
|
---|
| 89 | return TRUE;
|
---|
| 90 | } else {
|
---|
| 91 | return FALSE;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | * Returns TRUE if the first result has not been reached.
|
---|
| 97 | * @return boolean
|
---|
| 98 | * @access public
|
---|
| 99 | */
|
---|
| 100 | function hasPrevious() {
|
---|
| 101 | if ($this->position > 0) {
|
---|
| 102 | return TRUE;
|
---|
| 103 | } else {
|
---|
| 104 | return FALSE;
|
---|
| 105 | } }
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
| 108 | * Returns the next result array.
|
---|
| 109 | * @param integer $element
|
---|
| 110 | * @return result array OR single result if $element was specified OR NULL if there is no next result.
|
---|
| 111 | * @access public
|
---|
| 112 | */
|
---|
| 113 | function next($element = null) {
|
---|
| 114 | if ($this->position < count($this->RdqlResult) - 1) {
|
---|
| 115 | $this->position++;
|
---|
| 116 | if ($element) {return $this->RdqlResult[$this->position][$element];}
|
---|
| 117 | else return $this->RdqlResult[$this->position];
|
---|
| 118 | } else {
|
---|
| 119 | return NULL;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /**
|
---|
| 124 | * Returns the previous result.
|
---|
| 125 | * @param integer $element
|
---|
| 126 | * @return result array OR single result if $element was specified OR NULL if there is no next result.
|
---|
| 127 | * @access public
|
---|
| 128 | */
|
---|
| 129 | function previous($element = null) {
|
---|
| 130 | if ($this->position > 0) {
|
---|
| 131 | $this->position--;
|
---|
| 132 | if ($element) {return $this->RdqlResult[$this->position][$element];}
|
---|
| 133 | else return $this->RdqlResult[$this->position];
|
---|
| 134 | } else {
|
---|
| 135 | return NULL;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /**
|
---|
| 140 | * Returns the current result.
|
---|
| 141 | * @param integer $element
|
---|
| 142 | * @return result array OR single result if $element was specified OR NULL if there is no next result.
|
---|
| 143 | * @access public
|
---|
| 144 | */
|
---|
| 145 | function current($element = null) {
|
---|
| 146 | if (($this->position >= 0) && ($this->position < count($this->RdqlResult))) {
|
---|
| 147 | if ($element) {return $this->RdqlResult[$this->position][$element];}
|
---|
| 148 | else return $this->RdqlResult[$this->position];
|
---|
| 149 | } else {
|
---|
| 150 | return NULL;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /**
|
---|
| 155 | * Moves the pointer to the first result.
|
---|
| 156 | * @return void
|
---|
| 157 | * @access public
|
---|
| 158 | */
|
---|
| 159 | function moveFirst() {
|
---|
| 160 | $this->position = 0;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * Moves the pointer to the last result.
|
---|
| 165 | * @return void
|
---|
| 166 | * @access public
|
---|
| 167 | */
|
---|
| 168 | function moveLast() {
|
---|
| 169 | $this->position = count($this->RdqlResult) - 1;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /**
|
---|
| 173 | * Moves the pointer to a specific result.
|
---|
| 174 | * If you set an off-bounds value, next(), previous() and current() will return NULL
|
---|
| 175 | * @return void
|
---|
| 176 | * @access public
|
---|
| 177 | */
|
---|
| 178 | function moveTo($position) {
|
---|
| 179 | $this->position = $position;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /**
|
---|
| 183 | * Returns the current position of the iterator.
|
---|
| 184 | * @return integer
|
---|
| 185 | * @access public
|
---|
| 186 | */
|
---|
| 187 | function getCurrentPosition() {
|
---|
| 188 | return $this->position;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | ?> |
---|