mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-17 14:57:23 +08:00
Moved toArray() from YiiBase to ArrayHelper.
This commit is contained in:
@@ -624,44 +624,6 @@ class YiiBase
|
|||||||
$object->$name = $value;
|
$object->$name = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the public member variables of an object.
|
|
||||||
* This method is provided such that we can get the public member variables of an object.
|
|
||||||
* It is different from "get_object_vars()" because the latter will return private
|
|
||||||
* and protected variables if it is called within the object itself.
|
|
||||||
* @param object $object the object to be handled
|
|
||||||
* @return array the public member variables of the object
|
|
||||||
*/
|
|
||||||
public static function getObjectVars($object)
|
|
||||||
{
|
|
||||||
return get_object_vars($object);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts the object into an array.
|
|
||||||
* @param object|array $object the object to be converted into an array
|
|
||||||
* @param boolean $recursive whether to recursively converts properties which are objects into arrays.
|
|
||||||
* @return array the array representation of the object
|
|
||||||
*/
|
|
||||||
public static function toArray($object, $recursive = true)
|
|
||||||
{
|
|
||||||
if ($object instanceof Arrayable) {
|
|
||||||
$object = $object->toArray();
|
|
||||||
if (!$recursive) {
|
|
||||||
return $object;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$result = array();
|
|
||||||
foreach ($object as $key => $value) {
|
|
||||||
if ($recursive && (is_array($value) || is_object($value))) {
|
|
||||||
$result[$key] = static::toArray($value, true);
|
|
||||||
} else {
|
|
||||||
$result[$key] = $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
YiiBase::$aliases = array(
|
YiiBase::$aliases = array(
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
namespace yii\base;
|
namespace yii\base;
|
||||||
|
|
||||||
use Yii;
|
use Yii;
|
||||||
|
use yii\helpers\ArrayHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @include @yii/base/Object.md
|
* @include @yii/base/Object.md
|
||||||
@@ -227,6 +228,6 @@ class Object implements Arrayable
|
|||||||
*/
|
*/
|
||||||
public function toArray()
|
public function toArray()
|
||||||
{
|
{
|
||||||
return Yii::toArray($this, false);
|
return ArrayHelper::toArray($this, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
namespace yii\helpers\base;
|
namespace yii\helpers\base;
|
||||||
|
|
||||||
use Yii;
|
use Yii;
|
||||||
|
use yii\base\Arrayable;
|
||||||
use yii\base\InvalidParamException;
|
use yii\base\InvalidParamException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -19,6 +20,31 @@ use yii\base\InvalidParamException;
|
|||||||
*/
|
*/
|
||||||
class ArrayHelper
|
class ArrayHelper
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Converts the object into an array.
|
||||||
|
* @param object|array $object the object to be converted into an array
|
||||||
|
* @param boolean $recursive whether to recursively converts properties which are objects into arrays.
|
||||||
|
* @return array the array representation of the object
|
||||||
|
*/
|
||||||
|
public static function toArray($object, $recursive = true)
|
||||||
|
{
|
||||||
|
if ($object instanceof Arrayable) {
|
||||||
|
$object = $object->toArray();
|
||||||
|
if (!$recursive) {
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$result = array();
|
||||||
|
foreach ($object as $key => $value) {
|
||||||
|
if ($recursive && (is_array($value) || is_object($value))) {
|
||||||
|
$result[$key] = static::toArray($value, true);
|
||||||
|
} else {
|
||||||
|
$result[$key] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merges two or more arrays into one recursively.
|
* Merges two or more arrays into one recursively.
|
||||||
* If each array has an element with the same string key value, the latter
|
* If each array has an element with the same string key value, the latter
|
||||||
|
|||||||
Reference in New Issue
Block a user