marco-kretz/nested-set

嵌套集合的PHP实现

dev-master 2018-07-26 10:07 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:21:12 UTC


README

Build Status

这是我自己的"嵌套集合模型"实现。

目前还没有数据库连接。将很快实现mysql同步功能!

要求

  • composer
  • PHP >= 7.1

Node

节点表示嵌套集合中的单个节点(或容器)。它通过名称唯一标识。它可以安全地扩展(例如,通过有效载荷属性),并且仍然与嵌套集合一起工作。

还有一个扩展的Node,称为PayloadNode,可以存储任意可序列化的数据。

用法

$myNode = new Node('myNode');
$anotherNode = new Node('anotherNode);

NestedSet

这个库的核心。它管理嵌套集合模型,并使用Node作为节点(容器)。嵌套集合也是一个迭代器,因此您可以简单地迭代它,节点作为值。此外,还可以序列化单个节点或整个嵌套集合。

用法

$ns = new NestedSet();

// Define nodes
$rootNode = new Node('root');
$childNode1 = new Node('child1');
$childNode2 = new Node('child2');

// Set root node
$ns->addRoot($rootNode);

// Add nodes
$ns->addNode($rootNode, $childNode1);
$ns->addNode($rootNode, $childNode2);

print($ns);

// Remove node
$ns->removeNode($childNode2);

print($ns);

// Retrieve sub-nodes
$rootChildren = $ns->getSubNodes($rootNode);

for ($rootChildren as $rootChild) {
    print($rootChild);
}

// Iterate over NestedSet
for ($ns as $index => $node) {
    print($node);
}

// Serialization
$serialized = serialize($ns);
$nsCopy = unserialize($serialized);

print($ns === $nsCopy); // true

测试

composer test或简单地phpunit

待办事项

  • 添加数据库同步
  • 还有吗?