[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/config/ -> properties.class.php (source)

   1  <?php
   2  
   3      
   4  
   5      /**
   6       * \ingroup Config
   7       * 
   8       * Class inspired by the java class Properties.
   9       */
  10      class Properties  
  11      {
  12  
  13          var $_props;
  14          
  15          var $_keyFilters;
  16  
  17          /**
  18           * Constructor.
  19           *
  20           * @param values If $values == null, then the object will be initialized empty.
  21           * If it contains a valid PHP array, all the properties will be initialized at once.
  22           */
  23  		function Properties( $values = null )
  24          {
  25              $this->_keyFilters = Array();
  26              
  27              if( $values == null )
  28                  $this->_props = Array();
  29              else
  30                  $this->_props = $values;
  31          }
  32  
  33          /**
  34           * Sets a value in our hash table.
  35           *
  36           * @param key Name of the value in the hash table
  37           * @param value Value that we want to assign to the key '$key'
  38           */
  39  		function setValue( $key, $value )
  40          {
  41              $this->_props[$key] = $value;
  42          }
  43  
  44          /**
  45           * Returns the value associated to a key
  46           *
  47           * @param key Key whose value we want to fetch
  48           * @param defaultValue value that we should return in case the one we're looking for
  49           * is empty or does not exist
  50           * @param filterClass An instance of an object implementing the FilterBase interface that
  51           * will be used to process the value before returning it.
  52           * @return Value associated to that key
  53           */
  54  		function getValue( $key, $defaultValue = null, $filterClass = null )
  55          {
  56              if( !isset($this->_props[$key]) ) {
  57                  $value = $defaultValue;
  58              } else {
  59                  $value = $this->_props[$key];
  60              }
  61  
  62              if( $filterClass  || isset( $this->_keyFilters[$key] )) {
  63                  // there's a filter class specified, so we should run the
  64                  // resulting value through it...
  65                  if( isset( $this->_keyFilters[$key] )) {
  66                      $filterClass = $this->_keyFilters[$key];
  67                      //print("using filter: ".get_class($filterClass)." - key = ".$key."<br/>" );                    
  68                  }
  69                      
  70                  $value = $filterClass->filter( $value );
  71              }
  72              
  73              return( $value );
  74          }
  75          
  76          /**
  77           * This method is an alias for Properties::getValue() but the filter
  78           * class is now a mandatory parameter
  79           *
  80           * @param key Key whose value we want to fetch
  81           * @param filterClass An instance of an object implementing the FilterBase interface that
  82           * will be used to process the value before returning it.
  83           * @param defaultValue value that we should return in case the one we're looking for
  84           * is empty or does not exist
  85           * @return Value associated to that key
  86            * @see Properties::getValue()
  87            */
  88  		function getFilteredValue( $key, $filterClass, $defaultValue = null )
  89          {
  90              return( $this->getValue( $key, $defaultValue, $filterClass ));
  91          }
  92          
  93          /**
  94           * Registers a filter class for the key "$key", so that 
  95           * every time Properties::getValue( "$key" ) is called to fetch the requested
  96           * value, the filter will be applied automatically without the need to specify
  97           * the filter class at every call
  98           *
  99           * @param key
 100           * @param filterClass
 101           */
 102  		function registerFilter( $key, &$filterClass ) 
 103          {
 104              $this->_keyFilters[$key] = $filterClass;
 105          }
 106  
 107          /**
 108           * Method overwritten from the Object class
 109           * @return Returns a nicer representation of our contents
 110           */
 111  		function toString()
 112          {
 113              print_r( $this->_props );
 114          }
 115  
 116          /**
 117           * Returns the internal arrary used to store the properties as a PHP array
 118           * @return Internal array as a PHP array
 119           */
 120  		function getAsArray()
 121          {
 122              return $this->_props;
 123          }
 124  
 125          /**
 126           * Returns an array containing all the keys used
 127           *
 128           * @return Array containing all the keys
 129           */
 130  		function getKeys()
 131          {
 132              return array_keys( $this->_props );
 133          }
 134  
 135          /**
 136           * Returns an array containing the values
 137           *
 138           * @return Array containing the values
 139           */
 140  		function getValues()
 141          {
 142              return array_values( $this->_props );
 143          }
 144          
 145          /**
 146           * returns whether a given key exists in the table
 147           *
 148           * @param key The key name
 149           * @return True if the key exists, false otherwise
 150           */
 151          function keyExists( $key )
 152          {
 153              return( array_key_exists( $key, $this->_props ));
 154          }
 155      }
 156  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics