cloudformation-plus-plus: cfn template preprocessor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

114 lines
1.8 KiB

<?php
declare(strict_types=1);
namespace App\Util;
class GraphNode
{
protected $parent;
protected $value;
protected $children;
public function __construct($value = null)
{
$this->value = $value;
$this->children = [];
}
public function __invoke()
{
return $this->value;
}
public function setValue($value): void
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
public function setParent(GraphNode $node): void
{
$this->parent = $node;
}
public function hasParent(): bool
{
return isset($this->parent);
}
public function getParent(): GraphNode
{
return $this->parent;
}
public function countChildren(): int
{
return sizeof($this->children);
}
public function hasChildren(): bool
{
return sizeof($this->children) > 0;
}
public function getChildren(): array
{
return $this->children;
}
public function appendChild(GraphNode $child): void
{
$this->children[] = $child;
$child->setParent($this);
}
public function replaceChild(GraphNode $original, GraphNode $new): void
{
for ($i = 0; $i < sizeof($this->children); $i++)
{
if ($this->children[$i] === $original)
{
$this->children[$i] = $new;
$new->setParent($this);
return;
}
}
}
public function clearChildren(): void
{
$this->children = [];
}
public function add($value): GraphNode
{
$this->children[] = new GraphNode($value);
end($this->children)->setParent($this);
return end($this->children);
}
public function walk(callable $callback)
{
static::walkNodes([$this], $callback);
}
protected static function walkNodes(array $nodes, callable $callback)
{
foreach ($nodes as $node)
{
if ($node->hasChildren())
{
static::walkNodes($node->getChildren(), $callback);
}
$callback($node);
}
}
}