[ Index ]
 

Code source de PRADO 3.0.6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/framework/Util/ -> TDataFieldAccessor.php (source)

   1  <?php
   2  /**
   3   * TDataFieldAccessor class file
   4   *
   5   * @author Qiang Xue <qiang.xue@gmail.com>
   6   * @link http://www.pradosoft.com/
   7   * @copyright Copyright &copy; 2005 PradoSoft
   8   * @license http://www.pradosoft.com/license/
   9   * @version $Id: TDataFieldAccessor.php 1397 2006-09-07 07:55:53Z wei $
  10   * @package System.Util
  11   */
  12  
  13  /**
  14   * TDataFieldAccessor class
  15   *
  16   * TDataFieldAccessor is a utility class that provides access to a field of some data.
  17   * The accessor attempts to obtain the field value in the following order:
  18   * - If the data is an array, then the field is treated as an array index
  19   *   and the corresponding element value is returned;
  20   * - If the data is a TMap or TList object, then the field is treated as a key
  21   *   into the map or list, and the corresponding value is returned.
  22   * - If the data is an object, the field is treated as a property or subproperty
  23   *   defined with getter methods. For example, if the object has a method called
  24   *   getMyValue(), then field 'MyValue' will retrive the result of this method call.
  25   *   If getMyValue() returns an object which contains a method getMySubValue(),
  26   *   then field 'MyValue.MySubValue' will return that method call result.
  27   *
  28   * @author Qiang Xue <qiang.xue@gmail.com>
  29   * @version $Id: TDataFieldAccessor.php 1397 2006-09-07 07:55:53Z wei $
  30   * @package System.Util
  31   * @since 3.0
  32   */
  33  class TDataFieldAccessor
  34  {
  35      /**
  36       * Evaluates the data value at the specified field.
  37       * - If the data is an array, then the field is treated as an array index
  38       *   and the corresponding element value is returned;
  39       * - If the data is a TMap or TList object, then the field is treated as a key
  40       *   into the map or list, and the corresponding value is returned.
  41       * - If the data is an object, the field is treated as a property or subproperty
  42       *   defined with getter methods. For example, if the object has a method called
  43       *   getMyValue(), then field 'MyValue' will retrive the result of this method call.
  44       *   If getMyValue() returns an object which contains a method getMySubValue(),
  45       *   then field 'MyValue.MySubValue' will return that method call result.
  46       * @param mixed data containing the field value, can be an array, TMap, TList or object.
  47       * @param mixed field value
  48       * @return mixed value at the specified field
  49       * @throws TInvalidDataValueException if field or data is invalid
  50       */
  51  	public static function getDataFieldValue($data,$field)
  52      {
  53          if(Prado::getApplication()->getMode()===TApplicationMode::Performance)
  54          {
  55              if(is_array($data) || ($data instanceof ArrayAccess))
  56                  return $data[$field];
  57              else if(is_object($data))
  58              {
  59                  if(strpos($field,'.')===false)  // simple field
  60                  {
  61                      if(property_exists($data,$field))
  62                          return $data->{$field};
  63                      else
  64                          return call_user_func(array($data,'get'.$field));
  65                  }
  66                  else // field in the format of xxx.yyy.zzz
  67                  {
  68                      $object=$data;
  69                      foreach(explode('.',$field) as $f)
  70                          $object=call_user_func(array($object,'get'.$f));
  71                      return $object;
  72                  }
  73              }
  74              else
  75                  throw new TInvalidDataValueException('datafieldaccessor_data_invalid',$field);
  76          }
  77          else
  78          {
  79              if(is_array($data) || ($data instanceof ArrayAccess))
  80              {
  81                  if(isset($data[$field]) || $data[$field]===null)
  82                      return $data[$field];
  83                  else
  84                      throw new TInvalidDataValueException('datafieldaccessor_datafield_invalid',$field);
  85              }
  86              else if(is_object($data))
  87              {
  88                  if(strpos($field,'.')===false)  // simple field
  89                  {
  90                      if(property_exists($data,$field))
  91                          return $data->{$field};
  92                      else if(is_callable(array($data,'get'.$field)))
  93                          return call_user_func(array($data,'get'.$field));
  94                      else
  95                          throw new TInvalidDataValueException('datafieldaccessor_datafield_invalid',$field);
  96                  }
  97                  else // field in the format of xxx.yyy.zzz
  98                  {
  99                      $object=$data;
 100                      foreach(explode('.',$field) as $f)
 101                      {
 102                          $getter='get'.$f;
 103                          if(is_callable(array($object,$getter)))
 104                              $object=call_user_func(array($object,$getter));
 105                          else
 106                              throw new TInvalidDataValueException('datafieldaccessor_datafield_invalid',$field);
 107                      }
 108                      return $object;
 109                  }
 110              }
 111              else
 112                  throw new TInvalidDataValueException('datafieldaccessor_data_invalid',$field);
 113          }
 114      }
 115  }
 116  
 117  ?>


Généré le : Sun Feb 25 21:07:04 2007 par Balluche grâce à PHPXref 0.7