示例#1
0
文件: set.php 项目: venka10/RUS
 /**
  * Maps the given value as an object. If $value is an object,
  * it returns $value. Otherwise it maps $value as an object of
  * type $class, and if primary assign _name_ $key on first array.
  * If $value is not empty, it will be used to set properties of
  * returned object (recursively). If $key is numeric will maintain array
  * structure
  *
  * @param mixed $value Value to map
  * @param string $class Class name
  * @param boolean $primary whether to assign first array key as the _name_
  * @return mixed Mapped object
  * @access private
  */
 function __map(&$array, $class, $primary = false)
 {
     if ($class === true) {
         $out = new stdClass();
     } else {
         $out = new $class();
     }
     if (is_array($array)) {
         $keys = array_keys($array);
         foreach ($array as $key => $value) {
             if ($keys[0] === $key && $class !== true) {
                 $primary = true;
             }
             if (is_numeric($key)) {
                 if (is_object($out) && is_array($value)) {
                     $out = get_object_vars($out);
                 }
                 $out[$key] = Set::__map($value, $class, true);
             } elseif ($primary === true && is_array($value)) {
                 $out->_name_ = $key;
                 $primary = false;
                 foreach ($value as $key2 => $value2) {
                     $out->{$key2} = Set::__map($value2, $class);
                 }
             } else {
                 $out->{$key} = Set::__map($value, $class);
             }
         }
     } else {
         $out = $array;
     }
     return $out;
 }
示例#2
0
 /**
  * Maps the given value as an object. If $value is an object,
  * it returns $value. Otherwise it maps $value as an object of
  * type $class, and identity $identity. If $value is not empty,
  * it will be used to set properties of returned object
  * (recursively).
  *
  * @param mixed $value Value to map
  * @param string $class Class name
  * @param string $identity Identity to assign to class
  * @return mixed Mapped object
  * @access private
  */
 function __map($value, $class, $identity = null)
 {
     if (is_object($value)) {
         return $value;
     }
     if (!empty($value) && Set::numeric(array_keys($value))) {
         $ret = array();
         foreach ($value as $key => $val) {
             $ret[$key] = Set::__map($val, $class);
         }
     } else {
         $ret = new $class();
         if ($identity != null) {
             $ret->__identity__ = $identity;
         }
     }
     if (empty($value)) {
         return $ret;
     }
     $keys = array_keys($value);
     foreach ($value as $key => $val) {
         if (!is_numeric($key) && strlen($key) > 1) {
             if ($key[0] == strtoupper($key[0]) && $key[1] == strtolower($key[1]) && (is_array($val) || is_object($val))) {
                 if ($key == $keys[0]) {
                     $ret = Set::__map($val, $class, $key);
                 } else {
                     $ret->{$key} = Set::__map($val, $class, $key);
                 }
             } else {
                 $ret->{$key} = $val;
             }
         }
     }
     return $ret;
 }
示例#3
0
 /**
  * Maps the given value as an object. If $value is an object,
  * it returns $value. Otherwise it maps $value as an object of
  * type $class, and if primary assign _name_ $key on first array.
  * If $value is not empty, it will be used to set properties of
  * returned object (recursively). If $key is numeric will maintain array
  * structure
  *
  * @param mixed $value Value to map
  * @param string $class Class name
  * @param boolean $primary whether to assign first array key as the _name_
  * @return mixed Mapped object
  */
 public static function __map(&$array, $class, $primary = false)
 {
     if ($class === true) {
         $out = new stdClass();
     } else {
         $out = new $class();
     }
     if (is_array($array)) {
         $keys = array_keys($array);
         foreach ($array as $key => $value) {
             if ($keys[0] === $key && $class !== true) {
                 $primary = true;
             }
             if (is_numeric($key)) {
                 if (is_object($out)) {
                     $out = get_object_vars($out);
                 }
                 $out[$key] = Set::__map($value, $class);
                 if (is_object($out[$key])) {
                     if ($primary !== true && is_array($value) && Set::countDim($value, true) === 2) {
                         if (!isset($out[$key]->_name_)) {
                             $out[$key]->_name_ = $primary;
                         }
                     }
                 }
             } elseif (is_array($value)) {
                 if ($primary === true) {
                     if (!isset($out->_name_)) {
                         $out->_name_ = $key;
                     }
                     $primary = false;
                     foreach ($value as $key2 => $value2) {
                         $out->{$key2} = Set::__map($value2, true);
                     }
                 } else {
                     if (!is_numeric($key)) {
                         $out->{$key} = Set::__map($value, true, $key);
                         if (is_object($out->{$key}) && !is_numeric($key)) {
                             if (!isset($out->{$key}->_name_)) {
                                 $out->{$key}->_name_ = $key;
                             }
                         }
                     } else {
                         $out->{$key} = Set::__map($value, true);
                     }
                 }
             } else {
                 $out->{$key} = $value;
             }
         }
     } else {
         $out = $array;
     }
     return $out;
 }