Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
0845f4b1
Commit
0845f4b1
authored
Jun 28, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enhanced ArrayHelper::toArray()
parent
95b926a9
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
93 additions
and
5 deletions
+93
-5
ArrayHelper.php
framework/yii/helpers/base/ArrayHelper.php
+45
-2
ArrayHelperTest.php
tests/unit/framework/helpers/ArrayHelperTest.php
+48
-3
No files found.
framework/yii/helpers/base/ArrayHelper.php
View file @
0845f4b1
...
@@ -21,13 +21,56 @@ use yii\base\InvalidParamException;
...
@@ -21,13 +21,56 @@ use yii\base\InvalidParamException;
class
ArrayHelper
class
ArrayHelper
{
{
/**
/**
* Converts
the object
into an array.
* Converts
an object or an array of objects
into an array.
* @param object|array $object the object to be converted into an array
* @param object|array $object the object to be converted into an array
* @param array $properties a mapping from object class names to the properties that need to put into the resulting arrays.
* The properties specified for each class is an array of the following format:
*
* ~~~
* array(
* 'app\models\Post' => array(
* 'id',
* 'title',
* // the key name in array result => property name
* 'createTime' => 'create_time',
* // the key name in array result => anonymous function
* 'length' => function ($post) {
* return strlen($post->content);
* },
* ),
* )
* ~~~
*
* The result of `ArrayHelper::toArray($post, $properties)` could be like the following:
*
* ~~~
* array(
* 'id' => 123,
* 'title' => 'test',
* 'createTime' => '2013-01-01 12:00AM',
* 'length' => 301,
* )
* ~~~
*
* @param boolean $recursive whether to recursively converts properties which are objects into arrays.
* @param boolean $recursive whether to recursively converts properties which are objects into arrays.
* @return array the array representation of the object
* @return array the array representation of the object
*/
*/
public
static
function
toArray
(
$object
,
$recursive
=
true
)
public
static
function
toArray
(
$object
,
$
properties
=
array
(),
$
recursive
=
true
)
{
{
if
(
!
empty
(
$properties
)
&&
is_object
(
$object
))
{
$className
=
get_class
(
$object
);
if
(
!
empty
(
$properties
[
$className
]))
{
$result
=
array
();
foreach
(
$properties
[
$className
]
as
$key
=>
$name
)
{
if
(
is_int
(
$key
))
{
$result
[
$name
]
=
$object
->
$name
;
}
else
{
$result
[
$key
]
=
static
::
getValue
(
$object
,
$name
);
}
}
return
$result
;
}
}
if
(
$object
instanceof
Arrayable
)
{
if
(
$object
instanceof
Arrayable
)
{
$object
=
$object
->
toArray
();
$object
=
$object
->
toArray
();
if
(
!
$recursive
)
{
if
(
!
$recursive
)
{
...
...
tests/unit/framework/helpers/ArrayHelperTest.php
View file @
0845f4b1
...
@@ -2,16 +2,61 @@
...
@@ -2,16 +2,61 @@
namespace
yiiunit\framework\helpers
;
namespace
yiiunit\framework\helpers
;
use
yii\base\Object
;
use
yii\helpers\ArrayHelper
;
use
yii\helpers\ArrayHelper
;
use
yii\test\TestCase
;
use
yii\test\TestCase
;
use
yii\data\Sort
;
use
yii\data\Sort
;
class
ArrayHelperTest
extends
TestCase
class
Post1
{
{
public
function
testMerge
()
public
$id
=
23
;
{
public
$title
=
'tt'
;
}
class
Post2
extends
Object
{
public
$id
=
123
;
public
$content
=
'test'
;
private
$secret
=
's'
;
public
function
getSecret
()
{
return
$this
->
secret
;
}
}
class
ArrayHelperTest
extends
TestCase
{
public
function
testToArray
()
{
$object
=
new
Post1
;
$this
->
assertEquals
(
get_object_vars
(
$object
),
ArrayHelper
::
toArray
(
$object
));
$object
=
new
Post2
;
$this
->
assertEquals
(
get_object_vars
(
$object
),
ArrayHelper
::
toArray
(
$object
));
$object1
=
new
Post1
;
$object2
=
new
Post2
;
$this
->
assertEquals
(
array
(
get_object_vars
(
$object1
),
get_object_vars
(
$object2
),
),
ArrayHelper
::
toArray
(
array
(
$object1
,
$object2
,
)));
$object
=
new
Post2
;
$this
->
assertEquals
(
array
(
'id'
=>
123
,
'secret'
=>
's'
,
'_content'
=>
'test'
,
'length'
=>
4
,
),
ArrayHelper
::
toArray
(
$object
,
array
(
$object
->
className
()
=>
array
(
'id'
,
'secret'
,
'_content'
=>
'content'
,
'length'
=>
function
(
$post
)
{
return
strlen
(
$post
->
content
);
}
))));
}
}
public
function
testRemove
()
public
function
testRemove
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment