本文整理汇总了PHP中Set::__map方法的典型用法代码示例。如果您正苦于以下问题:PHP Set::__map方法的具体用法?PHP Set::__map怎么用?PHP Set::__map使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Set
的用法示例。
在下文中一共展示了Set::__map方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __map
/**
* 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: __map
/**
* 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: __map
/**
* 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;
}