RDQL
[ class tree: RDQL ] [ index: RDQL ] [ all elements ]

Source for file ResultIterator.php

Documentation is available at ResultIterator.php

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

Documentation generated on Mon, 11 Mar 2019 15:39:50 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.