diff options
author | Andres Rey <[email protected]> | 2017-11-30 19:18:26 +0000 |
---|---|---|
committer | Andres Rey <[email protected]> | 2017-11-30 19:18:26 +0000 |
commit | 13d3910781c599a5aea74ef4fa9d7ca4815cde69 (patch) | |
tree | c0f9621184a43ac2dd50805463bcc379792a2fe2 | |
parent | fa23ddff97283b7802612592df8f32292871f440 (diff) |
Optimize getChildren with array_filter instead of a foreach
-rw-r--r-- | src/NodeClass/NodeClassTrait.php | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/src/NodeClass/NodeClassTrait.php b/src/NodeClass/NodeClassTrait.php index bf34d5e..c117691 100644 --- a/src/NodeClass/NodeClassTrait.php +++ b/src/NodeClass/NodeClassTrait.php @@ -320,13 +320,12 @@ trait NodeClassTrait */ public function getChildren($filterEmptyDOMText = false) { - $ret = []; - foreach ($this->childNodes as $node) { - if ($filterEmptyDOMText && $node->nodeName === '#text' && !trim($node->nodeValue)) { - continue; - } - - $ret[] = $node; + $ret = iterator_to_array($this->childNodes); + if ($filterEmptyDOMText) { + // Array values is used to discard the key order. Needs to be 0 to whatever without skipping any number + $ret = array_values(array_filter($ret, function ($node) { + return $node->nodeName !== '#text' || mb_strlen(trim($node->nodeValue)); + })); } return $ret; |