Source for file datareflector.php

Documentation is available at datareflector.php

  1. <?php
  2. /**
  3. * reflect all submitted data back to the user
  4. *
  5. * @author    Stuart Prescott
  6. * @copyright  Copyright Stuart Prescott
  7. * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  8. * @version    $Id$
  9. * @package    Bumblebee
  10. * @subpackage FormsLibrary
  11. */
  12.  
  13. /** Load ancillary functions */
  14. require_once 'inc/typeinfo.php';
  15.  
  16. /**
  17. * Reflects all inputted data through hidden fields
  18. *
  19. * Can optionally exclude some fields
  20. *
  21. * Typical usage:<code>
  22. *     $ref = new DataReflector();
  23. *     $ref->exclude(array('id', 'name'));
  24. *     $ref->excludeRegEx(array('/^setting-.+/'));
  25. *     echo $ref->display($_POST);
  26. * </code>
  27. * @package    Bumblebee
  28. * @subpackage FormsLibrary
  29. */
  30. class DataReflector {
  31.   /** @var string  basename that will be appended to all keys */
  32.   var $basename = '';
  33.   /** @var array   list of fields to exclude from the datareflector */
  34.   var $excludes = array();
  35.   /** @var array   list of regexp fields to exclude from the datareflector  */
  36.   var $excludesRegEx = array();
  37.   /** @var array   list of list of keys that have value restrictions */
  38.   var $limitKeys = array();
  39.   /** @var array   list of list of values that are acceptable for the limited keys */
  40.   var $limitValues = array();
  41.   /** @var integer   debug level    */
  42.   var $DEBUG = 0;
  43.  
  44.   /**
  45.   *  Create a datareflector object
  46.   */
  47.   function DataReflector() {
  48.   }
  49.  
  50.   /**
  51.   *  Creates hidden fields html representation
  52.   *
  53.   * @param array $PD  array of $field => $value
  54.   * @return string  html hidden fields
  55.   */
  56.   function display($PD) {
  57.     $t = '';
  58.     foreach ($PD as $key => $val) {
  59.       if ($this->_includeKey($key, $val)) {
  60.       // then we should be included in the reflection
  61.         $t .= sprintf('<input type="hidden" name="%s" value="%s" />',
  62.                         $this->basename.xssqw($key),
  63.                         xssqw($val)
  64.                     );
  65.       }
  66.     }
  67.     return $t;
  68.   }
  69.  
  70.   function _includeKey($key, $val) {
  71.     if (in_array($key, $this->excludes)) {
  72.       return false;
  73.     }
  74.     foreach ($this->excludesRegEx as $re) {
  75.       if (preg_match($re, $key)) {
  76.         return false;
  77.       }
  78.     }
  79.     foreach ($this->limitKeys as $seq => $lk) {
  80.       if (in_array($key, $lk)) {
  81.         // a restricted key; check that the key's value is OK
  82.         if (! in_array($val, $this->limitValues[$seq])) {
  83.           return false;
  84.         }
  85.       }
  86.     }
  87.     return true;
  88.   }
  89.  
  90.   /**
  91.   *  Exclude these fields from the reflection
  92.   *
  93.   * @param mixed  $arr single field or list of fields to exclude
  94.   */
  95.   function exclude($arr) {
  96.     if (! is_array($arr)) {
  97.       $this->excludes[] = $arr;
  98.     } else {
  99.       $this->excludes = array_merge($this->excludes, $arr);
  100.     }
  101.   }
  102.  
  103.   /**
  104.   *  Exclude the fields that match these regexps from the reflection
  105.   *
  106.   * @param mixed   $arr single regexp or list of regexps to use for exclusion
  107.   */
  108.   function excludeRegEx($arr) {
  109.     if (! is_array($arr)) {
  110.       $this->excludesRegEx[] = $arr;
  111.     } else {
  112.       $this->excludesRegEx = array_merge($this->excludesRegEx, $arr);
  113.     }
  114.   }
  115.  
  116.   function excludeLogin() {
  117.     $this->excludes[] = 'username';
  118.     $this->excludes[] = 'pass';
  119.     $this->excludes[] = 'magicTag';
  120.   }
  121.  
  122.   function addLimit($keys, $values) {
  123.     $this->limitKeys[] = $keys;
  124.     $this->limitValues[] = $values;
  125.   }
  126.  
  127. } // class DataReflector
  128.  
  129. ?>

Documentation generated on Tue, 06 Mar 2007 10:01:10 +0000 by phpDocumentor 1.3.0