Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
solarium
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
solarium
Commits
2ee31129
Commit
2ee31129
authored
Apr 25, 2014
by
ehime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding default method when object does not posses the `toArray` method
parent
f602c202
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
1 deletion
+28
-1
library/Solarium/Core/Configurable.php
library/Solarium/Core/Configurable.php
+28
-1
No files found.
library/Solarium/Core/Configurable.php
View file @
2ee31129
...
...
@@ -83,6 +83,8 @@ class Configurable implements ConfigurableInterface
* If $options is an object, it will be converted into an array by calling
* its toArray method. This is compatible with the Zend_Config classes in
* Zend Framework, but can also easily be implemented in any other object.
* If $options does not have the toArray method, the internal method will
* be used instead.
*
* @throws InvalidArgumentException
* @param array|\Zend_Config $options
...
...
@@ -97,7 +99,7 @@ class Configurable implements ConfigurableInterface
// first convert to array if needed
if
(
!
is_array
(
$options
))
{
if
(
is_object
(
$options
))
{
$options
=
$options
->
toArray
(
);
$options
=
(
!
method_exists
(
$options
,
'toArray'
)
?
$this
->
toArray
(
$options
)
:
$options
->
toArray
()
);
}
else
{
throw
new
InvalidArgumentException
(
'Options value given to the setOptions() method must be an array or a Zend_Config object'
...
...
@@ -177,4 +179,29 @@ class Configurable implements ConfigurableInterface
{
return
$this
->
options
;
}
/**
* Turns an object array into an associative multidimensional array.
*
* @param $object
* @return array|object
*/
protected
function
toArray
(
$object
)
{
if
(
is_object
(
$object
))
{
// get_object_vars() does not handle recursive objects well,
// so use set-type without scope operator instead
settype
(
$object
,
'array'
);
}
/*
* Return array converted to object
* Using __METHOD__ (Magic constant)
* for recursive call
*/
if
(
is_array
(
$object
))
return
array_map
(
__METHOD__
,
$object
);
return
$object
;
}
}
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