[ Index ]

PHP Cross Reference of Web Application Component Toolkit

title

Body

[close]

/framework/util/ -> arraydataset.inc.php (source)

   1  <?php
   2  //--------------------------------------------------------------------------------
   3  // Copyright 2003 Procata, Inc.
   4  // Released under the LGPL license (http://www.gnu.org/copyleft/lesser.html)
   5  //--------------------------------------------------------------------------------
   6  /**
   7  * @package WACT_UTIL
   8  * @version $Id: arraydataset.inc.php,v 1.20 2004/06/15 03:03:50 jeffmoore Exp $
   9  */
  10  //--------------------------------------------------------------------------------
  11  /**
  12  * Provides an Iterator over a Recordset of DataSpaces.
  13  *
  14  * The recordset/dataset is made up of rows accessed using the Iterator and
  15  * DataSpace interfaces. It should be exactly analagous to a WACT database
  16  * recordset.
  17  *
  18  * e.g.
  19  * <code>
  20  * $dataset = array (
  21  *     array ('username'=>'jdoe','email'=>'jdoe@hotmail.com'),
  22  *     array ('username'=>'rsmith','email'=>'rsmith@cure.org'),
  23  *     array ('username'=>'nclark','email'=>'nclark@yahoo.com'),
  24  * );
  25  * $ds = new ArrayDataSet($dataset);
  26  * while ($ds->next()) {
  27  *     do_something($ds->get(username));
  28  * }
  29  * </code>
  30  * @see http://wact.sourceforge.net/index.php/ArrayDataSet
  31  * @todo - any need for paging setup?
  32  * @access public
  33  * @package WACT_UTIL
  34  */
  35  class ArrayDataSet {            /* implements DataSpace, Iterator */
  36      /**
  37       * @var array The Recordset (an array of arrays)
  38       * @access private
  39       */
  40      var $dataset;
  41  
  42      /**
  43       * @var array The current Record (row) from the Recordset
  44       * @access private
  45       */
  46      var $row;
  47  
  48      /**
  49       * @var array A duplicate of the current row, used with filters
  50       * @access private
  51       */
  52      var $row_copy;
  53  
  54      /**
  55       * @var object A filter object
  56       * @access private
  57       */
  58      var $filter;
  59  
  60      /**
  61       * @var boolean Set when a filter is applied to the current row
  62       * @access private
  63       */
  64      var $filtered;
  65  
  66      /**
  67       * @var boolean Indicates that the cursor is at the first record (row)
  68       * @access private
  69       */
  70      var $first;
  71  
  72      /**
  73       * @param array An array of arrays representing a Recordset
  74       */
  75      function ArrayDataSet($array) {
  76          $this->importDataSetAsArray($array);
  77          $this->row = array();
  78          $this->filtered = FALSE;
  79          $this->first = TRUE;
  80      }
  81  
  82      /**
  83       * Check whether the current row is empty
  84       */
  85      function isEmpty() {
  86          return empty($this->row);
  87      }
  88  
  89      /**
  90       * Sets up a Recordset from an array of row arrays
  91       *
  92       * Replaces the current Recordset and resets the internal cursor
  93       * @param array $dataset
  94       */
  95      function importDataSetAsArray($dataset) {
  96          $this->dataset = $dataset;
  97          $this->reset();
  98      }
  99  
 100      /**
 101       * Export the Recordset as an array of row arrays
 102       */
 103      function exportDataSetAsArray() {
 104          return $this->dataset;
 105      }
 106  
 107      /**
 108       * Alias for exportDataSetAsArray
 109       * @deprecated
 110       */
 111      function getDataSet() {
 112          return $this->exportDataSetAsArray();
 113      }
 114  
 115      //--------------------------------------------------------------------------------
 116      // DataSource implementation
 117  
 118      /**
 119       * Return an item from the current row by key/name
 120       * @param string $name The item key/name
 121       * @return mixed
 122       */
 123      function get($name) {
 124          if (isset($this->row[$name])) {
 125              return $this->row[$name];
 126          }
 127      }
 128  
 129      /**
 130       * Set the value of an item in the current row
 131       * @param string $name The item key/name
 132       * @param mixed $value The item value
 133       */
 134      function set($name, $value) {
 135          $this->row[$name] = $value;
 136      }
 137  
 138      function remove($name) {
 139          unset($this->row[$name]);
 140      }
 141  
 142      function removeAll() {
 143          $this->row = array();
 144      }
 145  
 146      /**
 147       * Import a dictionary/hash/map that will replace the current row
 148       * @param array $array 
 149       */
 150      function import($array) {
 151          if (is_array($array)) {
 152              $this->row =& $array;
 153          }
 154      }
 155  
 156      /**
 157       * Append a dictionary/hash/map of key value pairs to the current row
 158       *
 159       * Existing keys are overwritten
 160       * @param array $array
 161       */
 162      function merge($array) {
 163          $this->row = array_merge($this->row, $array);
 164      }
 165  
 166      /**
 167       * Export the current row/dataspace as a dictionary/hash/map
 168       * @return array
 169       */
 170      function &export() {
 171          return $this->row;
 172      }
 173  
 174      function isDataSource() {
 175          return TRUE;
 176      }
 177  
 178      function hasProperty($name) {
 179          return isset($this->row[$name]);
 180      }
 181  
 182      function getPropertyList() {
 183          return array_keys($this->row);
 184      }
 185      
 186      /**
 187       * Register a filter object on the current row.
 188       *
 189       * Filters can be used to transform rows. The filter object must provide a
 190       * doFilter(&$array) method that filters values from the current row. The
 191       * filter is passed a copy of the current row.
 192       * @param object $filter
 193       */
 194      function registerFilter(&$filter) {
 195          $this->filter =& $filter;
 196      }
 197  
 198      /**
 199       * Filter the current row as apt. Works on a copy of the row.
 200       */
 201      function prepare() {
 202          if (isset($this->filter) &&
 203              method_exists($this->filter, 'doFilter')) {
 204              if ($this->filtered) {
 205                  $this->row = $this->row_copy;
 206              } else {
 207                  $this->row_copy = $this->row;
 208              }
 209              $this->filter->doFilter($this->row);
 210              $this->filtered = TRUE;
 211          }
 212      }
 213  
 214      //--------------------------------------------------------------------------------
 215      // Iterator implementation
 216  
 217      /**
 218       * Reset the internal cursor to the beginning of the Recordset
 219       *
 220       * next() must be called after reset() to access a valid row via the
 221       * DataSpace methods.
 222       */
 223      function reset() {
 224          reset($this->dataset);
 225          $this->first = TRUE;
 226      }
 227  
 228      /**
 229       * Moves the internal cursor to the next row of the Recordset
 230       *
 231       * Sets the row accessed by the DataSpace interface to the next row of the
 232       * Recordset. Returns TRUE if there is another row in the recordset, FALSE
 233       * otherwise. Calls prepare().
 234       */
 235      function next() {
 236          $this->filtered = FALSE;
 237          if ($this->first) {
 238              $dataspace = current($this->dataset);
 239              $this->first = FALSE;
 240          } else {
 241              $dataspace = next($this->dataset);
 242          }
 243          /* casts are for clarification only */
 244          $this->row = (bool) $dataspace ? $dataspace : array();
 245          if ((bool) $dataspace) {
 246              $this->prepare();
 247          }
 248          return (bool) $dataspace;
 249      }
 250  
 251  }
 252  ?>


Generated: Sun Nov 28 19:36:09 2004 Cross-referenced by PHPXref 0.5