src/Utils.php
public static function applyCallback(callable $callback, NodeInterface $root, $targetClass = ChildNodeInterface::class)
    {
        $processed = [];
        $f = function (ChildNodeInterface $targetNode) use ($callback, $targetClass, &$f, &$processed) {
            if (in_array($targetNode, $processed, true)) {
                return;
            }
            $nodes = $targetNode instanceof ParentNodeInterface
                ? $targetNode->getChildrenRecursive()->toArray()
                : [];
            $nodes[] = $targetNode;

            /** @var NodeInterface $node */
            foreach ($nodes as $node) {
                $node instanceof $targetClass && call_user_func($callback, $node);
                $node instanceof ParentNodeInterface && $node->children()->onItemAdd($f);
                $processed[] = $node;
            }
        };
        $f($root);
    }
public static function applyCallback(callable $callback, NodeInterface $root, $targetClass = ChildNodeInterface::class)
    {
        $processed = [];
        $f = function (ChildNodeInterface $targetNode) use ($callback, $targetClass, &$f, &$processed) {
            if (in_array($targetNode, $processed, true)) {
                return;
            }
            $nodes = $targetNode instanceof ParentNodeInterface
                ? $targetNode->getChildrenRecursive()->toArray()
                : [];
            $nodes[] = $targetNode;

            /** @var NodeInterface $node */
            foreach ($nodes as $node) {
                $node instanceof $targetClass && call_user_func($callback, $node);
                $node instanceof ParentNodeInterface && $node->children()->onItemAdd($f);
                $processed[] = $node;
            }
        };
        $f($root);
    }
public static function applyCallback ( callable $ callback , NodeInterface $ root , $ targetClass = ChildNodeInterface :: class ) { $ processed = [ ] ; $ f = function ( ChildNodeInterface $ targetNode ) use ( $ callback , $ targetClass , & $ f , & $ processed ) { if ( in_array ( $ targetNode , $ processed , true ) ) { return ; } $ nodes = $ targetNode instanceof ParentNodeInterface ? $ targetNode -> getChildrenRecursive ( ) -> toArray ( ) : [ ] ; $ nodes [ ] = $ targetNode ; /** @var NodeInterface $node */ foreach ( $ nodes as $ node ) { $ node instanceof $ targetClass && call_user_func ( $ callback , $ node ) ; $ node instanceof ParentNodeInterface && $ node -> children ( ) -> onItemAdd ( $ f ) ; $ processed [ ] = $ node ; } } ; $ f ( $ root ) ; }
Applies callback to root node, if it's existing and further descendant nodes directly after adding to tree.

@param callable      $callback    function to apply
@param NodeInterface $root        root node
@param string        $targetClass callback will be applied only to nodes that are instances of $targetClass or inherited classes
Applies callback to root node if it s existing and further descendant nodes directly after adding to tree .