Browse Source

Do not call "toArray" if private or not an object

Guillaume "Elektordi" Genty 9 years ago
parent
commit
71878d22fa
1 changed files with 6 additions and 6 deletions
  1. 6 6
      src/Utility/Xml.php

+ 6 - 6
src/Utility/Xml.php

@@ -203,8 +203,8 @@ class Xml
      */
     public static function fromArray($input, $options = [])
     {
-        if (is_object($input) && method_exists($input, 'toArray')) {
-            $input = $input->toArray();
+        if (is_object($input) && method_exists($input, 'toArray') && is_callable([$input, 'toArray'])) {
+            $input = call_user_func([$input, 'toArray']);
         }
         if (!is_array($input) || count($input) !== 1) {
             throw new XmlException('Invalid input.');
@@ -257,8 +257,8 @@ class Xml
         }
         foreach ($data as $key => $value) {
             if (is_string($key)) {
-                if (is_object($value) && method_exists($value, 'toArray')) {
-                    $value = $value->toArray();
+                if (is_object($value) && method_exists($value, 'toArray') && is_callable([$value, 'toArray'])) {
+                    $value = call_user_func([$value, 'toArray']);
                 }
 
                 if (!is_array($value)) {
@@ -323,8 +323,8 @@ class Xml
     {
         extract($data);
         $childNS = $childValue = null;
-        if (is_object($value) && method_exists($value, 'toArray')) {
-            $value = $value->toArray();
+        if (is_object($value) && method_exists($value, 'toArray') && is_callable([$value, 'toArray'])) {
+            $value = call_user_func([$value, 'toArray']);
         }
         if (is_array($value)) {
             if (isset($value['@'])) {