mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-23 05:44:36 +02:00
lib/tree: Save child nodes into an array instead of using SplDoublyLinkedList
refs #6630
This commit is contained in:
parent
c8da05d0a7
commit
f331c03f63
@ -3,10 +3,18 @@
|
|||||||
|
|
||||||
namespace Icinga\Data\Tree;
|
namespace Icinga\Data\Tree;
|
||||||
|
|
||||||
use SplDoublyLinkedList;
|
use Identifiable;
|
||||||
|
use IteratorAggregate;
|
||||||
|
|
||||||
class Node extends SplDoublyLinkedList implements NodeInterface
|
class Node implements Identifiable, IteratorAggregate
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* The node's ID
|
||||||
|
*
|
||||||
|
* @type mixed
|
||||||
|
*/
|
||||||
|
protected $id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The node's value
|
* The node's value
|
||||||
*
|
*
|
||||||
@ -15,13 +23,45 @@ class Node extends SplDoublyLinkedList implements NodeInterface
|
|||||||
protected $value;
|
protected $value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new node
|
* The node's children
|
||||||
*
|
*
|
||||||
* @param mixed $value The node's value
|
* @type array
|
||||||
*/
|
*/
|
||||||
public function __construct($value = null)
|
protected $children = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the node's ID
|
||||||
|
*
|
||||||
|
* @param mixed $id ID of the node
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setId($id)
|
||||||
|
{
|
||||||
|
$this->id = $id;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (non-PHPDoc)
|
||||||
|
* @see Identifiable::getId() For the method documentation.
|
||||||
|
*/
|
||||||
|
public function getId()
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the node's value
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setValue($value)
|
||||||
{
|
{
|
||||||
$this->value = $value;
|
$this->value = $value;
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -35,44 +75,45 @@ class Node extends SplDoublyLinkedList implements NodeInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new node from the given value and insert the node as the last child of this node
|
* Append a child node as the last child of this node
|
||||||
*
|
*
|
||||||
* @param mixed $value The node's value
|
* @param Node $child The child to append
|
||||||
*
|
*
|
||||||
* @return NodeInterface The appended node
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function appendChild($value)
|
public function appendChild(Node $child)
|
||||||
{
|
{
|
||||||
$child = new static($value);
|
$this->children[] = $child;
|
||||||
$this->push($child);
|
return $this;
|
||||||
return $child;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether this node has child nodes
|
* Get whether the node has children
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasChildren()
|
public function hasChildren()
|
||||||
{
|
{
|
||||||
$current = $this->current();
|
return ! empty($this->children);
|
||||||
if ($current === null) {
|
|
||||||
$current = $this;
|
|
||||||
}
|
|
||||||
return ! $current->isEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the node's child nodes
|
* Get the node's children
|
||||||
*
|
*
|
||||||
* @return NodeInterface
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getChildren()
|
public function getChildren()
|
||||||
{
|
{
|
||||||
$current = $this->current();
|
return $this->children;
|
||||||
if ($current === null) {
|
}
|
||||||
$current = $this;
|
|
||||||
}
|
/**
|
||||||
return $current;
|
* (non-PHPDoc)
|
||||||
|
* @see IteratorAggregate::getIterator() For the method documentation.
|
||||||
|
*/
|
||||||
|
public function getIterator()
|
||||||
|
{
|
||||||
|
return new TreeNodeIterator($this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user