phabel/src/Context.php
2020-08-14 20:40:01 +02:00

52 lines
1.2 KiB
PHP

<?php
namespace Phabel;
use PhpParser\Node;
use SplStack;
/**
* @author Daniil Gentili <daniil@daniil.it>
* @license MIT
*/
class Context
{
/**
* Parent nodes stack.
*
* @var SplStack<Node>
*/
public SplStack $parents;
public function __construct()
{
$this->parents = new SplStack;
}
/**
* Insert nodes before node.
*
* @param Node $node
* @param Node ...$nodes
* @return void
*/
public function insertBefore(Node $node, Node ...$nodes): void
{
$subNode = $node->getAttribute('currentNode');
$subNodeIndex = $node->getAttribute('currentNodeIndex');
\array_splice($node->{$subNode}, $subNodeIndex, 0, $nodes);
$node->setAttribute('currentNodeIndex', $subNodeIndex+\count($nodes));
}
/**
* Insert nodes after node.
*
* @param Node $node
* @param Node ...$nodes
* @return void
*/
public function insertAfter(Node $node, Node ...$nodes): void
{
$subNode = $node->getAttribute('currentNode');
$subNodeIndex = $node->getAttribute('currentNodeIndex');
\array_splice($node->{$subNode}, $subNodeIndex+1, 0, $nodes);
}
}