-
-
Notifications
You must be signed in to change notification settings - Fork 2
Includes the older toArray conversion #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -260,14 +260,13 @@ public function removeTagName(string $tagName): bool | |
| } | ||
| } | ||
|
|
||
|
|
||
| public function toArray(Closure $func = null): array | ||
| public function toFullArray(): array | ||
| { | ||
| $sxml = simplexml_import_dom($this->DOMNode()); | ||
| return [$sxml->getName() => $this->_toArray($sxml)]; | ||
| return [$sxml->getName() => $this->_toFullArray($sxml)]; | ||
| } | ||
|
|
||
| protected function _toArray(SimpleXMLElement $node): array|string | ||
| protected function _toFullArray(SimpleXMLElement $node): array|string | ||
| { | ||
| $hasAttributes = (count($node->attributes()) > 0); | ||
| $hasChildren = (count($node->children()) > 0); | ||
|
|
@@ -289,7 +288,7 @@ protected function _toArray(SimpleXMLElement $node): array|string | |
| if ($hasChildren) { | ||
| foreach ($node->children() as $child) { | ||
| $childName = $child->getName(); | ||
| $childData = $this->_toArray($child); | ||
| $childData = $this->_toFullArray($child); | ||
|
|
||
| if (isset($output[$childName])) { | ||
| if (!is_array($output[$childName]) || !isset($output[$childName][0])) { | ||
|
|
@@ -309,6 +308,39 @@ protected function _toArray(SimpleXMLElement $node): array|string | |
| return $output; | ||
| } | ||
|
|
||
| public function toArray(Closure $func = null): array | ||
| { | ||
| return $this->_toArray($this->DOMNode(), $func); | ||
| } | ||
|
|
||
| protected function _toArray(SimpleXMLElement|DOMNode|array $arr, Closure|null $func = null): array | ||
| { | ||
| if ($arr instanceof SimpleXMLElement) { | ||
| return $this->_toArray((array) $arr, $func); | ||
| } | ||
|
|
||
| if ($arr instanceof DOMNode) { | ||
| return $this->_toArray((array) simplexml_import_dom($arr), $func); | ||
| } | ||
|
Comment on lines
+316
to
+324
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inefficient Recursive Type Conversions
Tell me moreWhat is the issue?Multiple type casting and conversions in recursive calls create unnecessary object instantiations and type conversions. Why this mattersEach recursive iteration potentially creates new object instances and performs type conversions, leading to memory churn and slower execution, especially for deeply nested XML structures. Suggested change ∙ Feature PreviewNormalize the input type once at the entry point and work with a consistent type throughout the recursion: protected function _toArray($input, ?Closure $func = null): array
{
$arr = $this->normalizeToArray($input);
$result = [];
foreach ($arr as $key => $value) {
$result[$key] = is_array($value) ? $this->_toArray($value, $func) :
(!empty($func) ? $func($value) : $value);
}
return $result;
}
private function normalizeToArray($input)
{
if ($input instanceof SimpleXMLElement) return (array)$input;
if ($input instanceof DOMNode) return (array)simplexml_import_dom($input);
return $input;
}Provide feedback to improve future suggestions💬 Looking for more details? Reply to this comment to chat with Korbit. |
||
|
|
||
| $newArr = array(); | ||
| if (!empty($arr)) { | ||
| foreach ($arr as $key => $value) { | ||
| $newArr[$key] = | ||
| ( | ||
| is_array($value) | ||
| || ($value instanceof DOMNode) | ||
| || ($value instanceof SimpleXMLElement) | ||
| ? $this->_toArray($value, $func) | ||
| : (!empty($func) ? $func($value) : $value) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return $newArr; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * @param string|null $prefix | ||
| * @param string $uri | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling for invalid XML in toFullArray
Tell me more
What is the issue?
The toFullArray() method doesn't handle the case where simplexml_import_dom() returns false, which can happen with invalid XML nodes.
Why this matters
If the XML node is invalid, the method will trigger a PHP error when trying to call getName() on a false value, causing unexpected crashes.
Suggested change ∙ Feature Preview
Add null check and throw an exception for invalid XML:
Provide feedback to improve future suggestions
💬 Looking for more details? Reply to this comment to chat with Korbit.