hguenot/data-tree

v1.1.2 2018-10-22 04:47 UTC

This package is auto-updated.

Last update: 2024-08-29 04:50:31 UTC


README

这个库通过离散特质提供树形结构。

它旨在供其他库使用。

(由https://github.com/timostamm/data-tree 分支而来 - PHP 7 支持,以及发布)

持续集成

Build Status Code coverage GitHub version Packagist version

ChildrenTrait

使用此特质添加、访问和移除子节点。

LookupTrait

可选方法来检查树。

AttributesTrait

每个节点可选的属性。

ToStringTrait

可选的 __toString() 实现,输出节点的类名及其属性。

NodeTrait

结合了上述所有特质

受保护访问

子命名空间 ProtectedAccess 包含了 ChildrenTraitLookupTraitAttributesTrait 的受保护版本,其中所有方法都是受保护的。这在使用特质时提供了对公共 API 的精细控制。

示例

// Create a simple tree.
$root = new Node();
$root->setAttribute('name', 'root');

$a = new Node();
$a->setAttribute('name', 'a');
$root->addChild($a);

$b = new Node();
$b->setAttribute('name', 'b');
$root->addChild($b);

$c = new Node();
$c->setAttribute('name', 'c');
$root->addChild($c);


// Find a descendant with the name=b
$bAgain = $root->descendant(function ($node) {
	return $node->getAttribute('name') === 'b';
});

// Get the root node of any node
$rootAgain = $bAgain->findRootNode();

// Remove a node
$bAgain->remove();
$root->removeChild($a);
$root->removeChildAt(0);