Skip to content

Commit

Permalink
Fix XmlNodeList
Browse files Browse the repository at this point in the history
  • Loading branch information
ruff committed Jul 26, 2024
1 parent ed3423e commit 4156452
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,4 @@ _backup*
/examples/cii/*.xml
/examples/cii/*.html
myfile_dbg.xml
EN16931-UBL-validation.xsl
53 changes: 53 additions & 0 deletions src/xml/XmlNodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,49 @@ public function forEach($callback, $callBackBefore = null, $callbackAfter = null
if (is_null($this->domNodeList)) {
return;
}

if (!is_callable($callback)) {
return;
}

if (is_callable($callBackBefore)) {
call_user_func($callBackBefore);
}

foreach ($this->domNodeList as $node) {
if (is_callable($callbackBeforeEach)) {
call_user_func($callbackBeforeEach);
}

call_user_func($callback, $node);

if (is_callable($callbackAfterEach)) {
call_user_func($callbackAfterEach);
}
}

if (is_callable($callbackAfter)) {
call_user_func($callbackAfter);
}
}

/**
* Foreach for only $max nodes in internal nodelist.
*
* @param integer $max
* @param callable $callback
* @param callable|null $callBackBefore
* @param callable|null $callbackAfter
* @param callable|null $callbackBeforeEach
* @param callable|null $callbackAfterEach
* @return void
*/
public function forEachMax(int $max, $callback, $callBackBefore = null, $callbackAfter = null, $callbackBeforeEach = null, $callbackAfterEach = null)
{
if (is_null($this->domNodeList)) {
return;
}

if (!is_callable($callback)) {
return;
}
Expand All @@ -73,11 +116,21 @@ public function forEach($callback, $callBackBefore = null, $callbackAfter = null
call_user_func($callBackBefore);
}

$count = 0;

foreach ($this->domNodeList as $node) {
$count++;

if ($count > $max) {
break;
}

if (is_callable($callbackBeforeEach)) {
call_user_func($callbackBeforeEach);
}

call_user_func($callback, $node);

if (is_callable($callbackAfterEach)) {
call_user_func($callbackAfterEach);
}
Expand Down

0 comments on commit 4156452

Please sign in to comment.