-
-
Notifications
You must be signed in to change notification settings - Fork 2
Add toArrayFull() #10
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); | ||
| } | ||
|
|
||
| $newArr = array(); | ||
|
Comment on lines
+316
to
+326
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. Complex Type Handling in Array Conversion
Tell me moreWhat is the issue?Method accepts multiple types (SimpleXMLElement|DOMNode|array) and has recursive type casting, making the flow hard to follow and maintain. Why this mattersViolates SOLID's Single Responsibility Principle by handling multiple type conversions in one method. This complexity makes the code harder to test and more prone to bugs. Suggested change ∙ Feature PreviewSplit into specialized methods for each type: protected function convertSimpleXmlToArray(SimpleXMLElement $element): array;
protected function convertDomNodeToArray(DOMNode $node): array;
protected function processArrayValues(array $input): array;Provide feedback to improve future suggestions💬 Looking for more details? Reply to this comment to chat with Korbit. |
||
| 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.
Infinite Recursion Risk
Tell me more
What is the issue?
The _toArray method contains potential infinite recursion when handling SimpleXMLElement instances.
Why this matters
If a SimpleXMLElement is passed, it will continuously cast to array and call itself, leading to a stack overflow.
Suggested change ∙ Feature Preview
Modify the method to handle SimpleXMLElement directly:
Provide feedback to improve future suggestions
💬 Looking for more details? Reply to this comment to chat with Korbit.