Skip to content

Commit

Permalink
Central handling of callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Nov 7, 2024
1 parent df25c32 commit ba680e6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 69 deletions.
75 changes: 25 additions & 50 deletions src/XmlDocumentReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use DOMXPath;
use DOMDocument;
use DOMNodeList;
use horstoeko\zugferdublbridge\traits\HandlesCallbacks;
use horstoeko\zugferdublbridge\xml\XmlNodeList;
use horstoeko\zugferdublbridge\XmlDocumentBase;
use RuntimeException;
Expand All @@ -29,6 +30,8 @@
*/
class XmlDocumentReader extends XmlDocumentBase
{
use HandlesCallbacks;

/**
* Internal XPath
*
Expand Down Expand Up @@ -223,17 +226,13 @@ public function queryAll(string $expression, ?DOMNode $contextNode = null): XmlN
public function whenExists(string $expression, ?DOMNode $contextNode, $callback, $callbackElse = null): XmlDocumentReader
{
if ($this->exists($expression, $contextNode)) {
if (is_callable($callback)) {
call_user_func(
$callback,
$this->query($expression, $contextNode)->item(0),
$this->query($expression, $contextNode)->item(0)->parentNode
);
}
$this->fireCallback(
$callback,
$this->query($expression, $contextNode)->item(0),
$this->query($expression, $contextNode)->item(0)->parentNode
);
} else {
if (is_callable($callbackElse)) {
call_user_func($callbackElse);
}
$this->fireCallback($callbackElse);
}

return $this;
Expand All @@ -251,17 +250,13 @@ public function whenExists(string $expression, ?DOMNode $contextNode, $callback,
public function whenNotExists(string $expression, ?DOMNode $contextNode, $callback, $callbackElse = null): XmlDocumentReader
{
if (!$this->exists($expression, $contextNode)) {
if (is_callable($callback)) {
call_user_func(
$callback,
$this->query($expression, $contextNode)->item(0),
$this->query($expression, $contextNode)->item(0)->parentNode
);
}
$this->fireCallback(
$callback,
$this->query($expression, $contextNode)->item(0),
$this->query($expression, $contextNode)->item(0)->parentNode
);
} else {
if (is_callable($callbackElse)) {
call_user_func($callbackElse);
}
$this->fireCallback($callbackElse);
}

return $this;
Expand Down Expand Up @@ -293,17 +288,13 @@ public function whenEquals(string $expression, ?DOMNode $contextNode, $values, $
}

if ($equals === true) {
if (is_callable($callback)) {
call_user_func(
$callback,
$this->query($expression, $contextNode)->item(0),
$this->query($expression, $contextNode)->item(0)->parentNode
);
}
$this->fireCallback(
$callback,
$this->query($expression, $contextNode)->item(0),
$this->query($expression, $contextNode)->item(0)->parentNode
);
} else {
if (is_callable($callbackElse)) {
call_user_func($callbackElse);
}
$this->fireCallback($callbackElse);
}

return $this;
Expand Down Expand Up @@ -335,21 +326,9 @@ public function whenNotEquals(string $expression, ?DOMNode $contextNode, $values
}

if ($equals === false) {
if (is_callable($callback)) {
if ($this->query($expression, $contextNode)->item(0)) {
call_user_func(
$callback,
$this->query($expression, $contextNode)->item(0),
$this->query($expression, $contextNode)->item(0)->parentNode
);
} else {
call_user_func($callback);
}
}
$this->fireCallback($callback);
} else {
if (is_callable($callbackElse)) {
call_user_func($callbackElse);
}
$this->fireCallback($callbackElse);
}

return $this;
Expand All @@ -368,16 +347,12 @@ public function whenOneExists(array $expressions, array $contextNodes, $callback
{
foreach ($expressions as $expressionIndex => $expression) {
if ($this->exists($expression, $contextNodes[$expressionIndex])) {
if (is_callable($callback)) {
call_user_func($callback, $this->query($expression, $contextNodes[$expressionIndex])->item(0), $expressionIndex, $expression);
}
$this->fireCallback($callback, $this->query($expression, $contextNodes[$expressionIndex])->item(0), $expressionIndex, $expression);
return $this;
}
}

if (is_callable($callbackElse)) {
call_user_func($callbackElse);
}
$this->fireCallback($callbackElse);

return $this;
}
Expand Down
7 changes: 4 additions & 3 deletions src/XmlDocumentWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use DOMElement;
use DOMDocument;
use horstoeko\zugferdublbridge\traits\HandlesCallbacks;
use horstoeko\zugferdublbridge\XmlDocumentBase;

/**
Expand All @@ -24,6 +25,8 @@
*/
class XmlDocumentWriter extends XmlDocumentBase
{
use HandlesCallbacks;

/**
* Internal stack
*
Expand Down Expand Up @@ -264,9 +267,7 @@ public function group(string $tagName, $callback): void
}

$this->startElement($tagName);

call_user_func($callback);

$this->fireCallback($callback);
$this->endElement();
}

Expand Down
38 changes: 38 additions & 0 deletions src/traits/HandlesCallbacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* This file is a part of horstoeko/zugferdublbridge.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace horstoeko\zugferdublbridge\traits;

/**
* Trait for handling and firing callbacks
*
* @category Zugferd-UBL-Bridge
* @package Zugferd-UBL-Bridge
* @author D. Erling <[email protected]>
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/horstoeko/zugferdublbridge
*/
trait HandlesCallbacks
{
/**
* Internal helper function to fire a callback function
*
* @param callable $callback
* @param array ...$args
* @return mixed
*/
private function fireCallback($callback, ...$args)
{
if (!is_callable($callback)) {
return;
}

return call_user_func($callback, ...$args);
}
}
19 changes: 3 additions & 16 deletions src/xml/XmlNodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace horstoeko\zugferdublbridge\xml;

use DOMNodeList;
use horstoeko\zugferdublbridge\traits\HandlesCallbacks;

/**
* Class representing a XML node list
Expand All @@ -22,6 +23,8 @@
*/
class XmlNodeList
{
use HandlesCallbacks;

/**
* Nodelist
*
Expand Down Expand Up @@ -100,20 +103,4 @@ public function forEachMax(int $max, $callback, $callBackBefore = null, $callbac

$this->fireCallback($callbackAfter);
}

/**
* Internal helper function to fire a callback function
*
* @param callable $callback
* @param array ...$args
* @return void
*/
private function fireCallback($callback, ...$args)
{
if (!is_callable($callback)) {
return;
}

call_user_func($callback, ...$args);
}
}

0 comments on commit ba680e6

Please sign in to comment.