[ Index ]

PHP Cross Reference of Web Application Component Toolkit

title

Body

[close]

/framework/util/ -> dataspace.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: dataspace.inc.php,v 1.13 2004/11/15 01:46:04 jeffmoore Exp $
   9  */
  10  
  11  /**
  12  * The DataSpace is a container for a set of named data values (properties).
  13  * @see http://wact.sourceforge.net/index.php/DataSpace
  14  * @access public
  15  * @abstract
  16  * @package WACT
  17  */
  18  class DataSpace {
  19      /**
  20      * Properties stored in the DataSpace are contained here
  21      * @var array
  22      * @access private
  23      */
  24      var $properties = array(); /* change to properties */
  25  
  26      /**
  27      * Filter object for transforming stored data
  28      * @var object
  29      * @access private
  30      */
  31      var $filter;
  32  
  33      /**
  34      * Gets a copy of a stored property by name
  35      * @param string name of property
  36      * @return mixed value of property or NULL if not found
  37      * @access public
  38      */
  39      function get($name) {
  40          if (isset($this->properties[$name])) {
  41              return $this->properties[$name];
  42          }
  43      }
  44  
  45      /**
  46      * Gets a property value by navagating a dot separated path
  47      * that dereferences elements within the dataspace.
  48      * If an element cannot be dereferenced or is not set, the
  49      * value NULL is returned.
  50      * @param string name of property
  51      * @return mixed value of property or NULL if not found
  52      * @access public
  53      */
  54      function getPath($path) {
  55          if (($pos = strpos($path, '.')) === FALSE) {
  56              return $this->get($path);
  57          } else {
  58              $value = $this->get(substr($path, 0, $pos));
  59              $dataspace =& DataSpace::makeDataSpace($value);
  60              if (is_null($dataspace)) {
  61                  return NULL;
  62              }
  63              return $dataspace->getPath(substr($path, $pos + 1));
  64          }
  65      }
  66  
  67      /**
  68      * Stores a copy of value into a Property
  69      * @param string name of property
  70      * @param mixed value of property
  71      * @return void
  72      * @access public
  73      */
  74      function set($name, $value) {
  75          $this->properties[$name] = $value;
  76      }
  77  
  78      /**
  79      * Stores a copy of value into a Property based on a dot separated
  80      * path.
  81      * If an element cannot be dereferenced, or is not set, it is 
  82      * converted to an array.
  83      * @param string name of property
  84      * @param mixed value of property
  85      * @return void
  86      * @access public
  87      */
  88      function setPath($path, $value) {
  89          if (strpos($path, '.') == FALSE) {
  90              return $this->set($path, $value);
  91          }
  92          $var =& $this->properties;
  93          do {
  94              $pos = strpos($path, '.');
  95              if ($pos === FALSE) {
  96                  break;
  97              } else {
  98                  $key = substr($path, 0, $pos);
  99              }
 100              if (is_object($var)) {
 101                  if (method_exists($var, 'isDataSource')) {
 102                      return $var->setPath($path, $value);
 103                  } else {
 104                      $var =& $var->$key;
 105                  }
 106              } else if (is_array($var)) {
 107                  $var =& $var[$key];
 108              } else {
 109                  $var = array();
 110                  $var =& $var[$key];
 111              }
 112              $path = substr($path, $pos + 1);
 113          } while (TRUE);
 114          if (is_object($var)) {
 115             if (method_exists($var, 'isDataSource')) {
 116                  $var->set($path, $value);
 117             } else {
 118                  $var->$path = $value;
 119             }
 120          } else if (is_array($var)) {
 121              $var[$path] = $value;
 122          } else {
 123              $var = array();
 124              $var[$path] = $value;
 125          }
 126      }
 127  
 128      /**
 129      * removes stored property value
 130      * @param string name of property
 131      * @return void
 132      * @access public
 133      */
 134      function remove($name) {
 135          unset($this->properties[$name]);
 136      }
 137      
 138      /**
 139      * removes all property values
 140      * @return void
 141      * @access public
 142      */
 143      function removeAll() {
 144          $this->properties = array();
 145      }
 146  
 147      /**
 148      * replaces the current properties of this dataspace with the proprties and values
 149      * passed as a parameter
 150      * @param array
 151      * @return void
 152      * @access public
 153      */
 154      function import($property_list) {
 155          $this->properties = $property_list;
 156      }
 157  
 158      /**
 159      * Append a new list of values to the DataSpace. Existing key values will be
 160      * overwritten if duplicated in the new value list.
 161      *
 162      * @param array a list of property names and the values to set them to
 163      * @return void
 164      * @access public
 165      */
 166      function merge($property_list) {
 167          foreach ($property_list as $name => $value) {
 168              $this->set($name, $value);
 169          }
 170      }
 171  
 172      /**
 173      * Returns a reference to the complete array of properties stored
 174      * @return array
 175      * @access public
 176      */
 177      function &export() {
 178          return $this->properties;
 179      }
 180  
 181      /**
 182      * Any class that implements the DataSource interface should implement this method
 183      * This is a PHP4 way of detecting which objects implement the interface.
 184      * @return Boolean always TRUE
 185      * @access public
 186      */
 187      function isDataSource() {
 188          return TRUE;
 189      }
 190  
 191      /**
 192      * Has a value been assigned under this name for this dataspace?
 193      * @param string name of property
 194      * @return boolean TRUE if property exists
 195      * @access public
 196      */
 197      function hasProperty($name) {
 198          return isset($this->properties[$name]);
 199      }
 200      
 201      /**
 202      * Return a unique list of available properties
 203      * This method is probably going to have capitalization problems.
 204      * @return array
 205      * @access public
 206      */
 207      function getPropertyList() {
 208          return array_keys($this->properties);
 209      }
 210  
 211      /**
 212      * Registers a filter with the dataspace. Filters are used to transform
 213      * stored properties.
 214      * @param object instance of filter class containing a doFilter() method
 215      * @return void
 216      * @access public
 217      * @deprecated
 218      */
 219      function registerFilter(&$filter) {
 220          $this->filter =& $filter;
 221      }
 222  
 223      /**
 224      * Prepares the dataspace, executing the doFilter() method of the
 225      * registered filter, if one exists
 226      * @return void
 227      * @access protected
 228      * @deprecated
 229      */
 230      function prepare() {
 231          if (isset($this->filter)) {
 232              $this->filter->doFilter($this->properties);
 233          }
 234      }
 235  
 236      /**
 237      * Static method to convert a variable into a dataspace.
 238      * @return void
 239      */
 240      function &makeDataSpace(&$var) {
 241          if (is_object($var)) {
 242              if (method_exists($var, 'isDataSource')) {
 243                  return $var;
 244              } else {
 245                  require_once WACT_ROOT . 'util/objectdataspace.inc.php';
 246                  return new ObjectDataSpace($var);
 247              }
 248          } else if (is_array($var)) {
 249              $dataspace =& new dataSpace();
 250              $dataspace->properties = $var;
 251              return $dataspace;
 252          } else {
 253              return NULL;
 254          }
 255      }
 256      
 257  }
 258  
 259  ?>


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