This commit is contained in:
Aether
2025-09-25 09:12:22 +08:00
parent f286e18e71
commit 25aa56439a
13 changed files with 171 additions and 167 deletions

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Aether\Traits;
use Aether\AetherModel;
@@ -13,25 +15,16 @@ trait AetherTree
{
parent::__construct($attributes);
if (!$this instanceof AetherModel) {
if (! $this instanceof AetherModel) {
throw new LogicException(
"使用AetherTree trait的类必须继承AetherModel当前类: " . get_class($this)
'使用AetherTree trait的类必须继承AetherModel当前类: ' . get_class($this)
);
}
}
/**
* 抽象方法获取父ID字段名由子类实现
*/
abstract protected function getParentIdField(): string;
/**
* 抽象方法:获取排序字段名(由子类实现)
*/
abstract protected function getSortField(): string;
/**
* 构建树形结构
* 构建树形结构.
* @param mixed $items
*/
public static function buildTree($items, int $parentId = 0): array
{
@@ -45,7 +38,7 @@ trait AetherTree
foreach ($items as $item) {
if ($item[$parentField] == $parentId) {
$children = static::buildTree($items, $item['id']);
if (!empty($children)) {
if (! empty($children)) {
$item['children'] = $children;
}
$tree[] = $item;
@@ -57,20 +50,7 @@ trait AetherTree
}
/**
* 树形节点排序
*/
protected function sortTreeItems(array &$items, string $sortField): void
{
usort($items, function ($a, $b) use ($sortField) {
$direction = $this->treeSortDirection ?? 'asc';
return $direction === 'desc'
? $b[$sortField] <=> $a[$sortField]
: $a[$sortField] <=> $b[$sortField];
});
}
/**
* 获取指定节点的所有子节点ID
* 获取指定节点的所有子节点ID.
*/
public function getChildIds(int $id): array
{
@@ -84,7 +64,57 @@ trait AetherTree
}
/**
* 递归收集子节点ID
* 获取节点的完整路径.
*/
public function getPath(int $id): array
{
$parentField = $this->getParentIdField();
// 安全调用newQuery()
$node = $this->newQuery()->find($id);
if (! $node) {
return [];
}
$path = [$node->toArray()];
$parentId = $node[$parentField];
while ($parentId > 0) {
$parent = $this->newQuery()->find($parentId);
if (! $parent) {
break;
}
array_unshift($path, $parent->toArray());
$parentId = $parent[$parentField];
}
return $path;
}
/**
* 抽象方法获取父ID字段名由子类实现.
*/
abstract protected function getParentIdField(): string;
/**
* 抽象方法:获取排序字段名(由子类实现).
*/
abstract protected function getSortField(): string;
/**
* 树形节点排序.
*/
protected function sortTreeItems(array &$items, string $sortField): void
{
usort($items, function ($a, $b) use ($sortField) {
$direction = $this->treeSortDirection ?? 'asc';
return $direction === 'desc'
? $b[$sortField] <=> $a[$sortField]
: $a[$sortField] <=> $b[$sortField];
});
}
/**
* 递归收集子节点ID.
*/
private function collectChildIds(array $items, int $parentId, string $parentField, array &$ids): void
{
@@ -95,31 +125,4 @@ trait AetherTree
}
}
}
/**
* 获取节点的完整路径
*/
public function getPath(int $id): array
{
$parentField = $this->getParentIdField();
// 安全调用newQuery()
$node = $this->newQuery()->find($id);
if (!$node) {
return [];
}
$path = [$node->toArray()];
$parentId = $node[$parentField];
while ($parentId > 0) {
$parent = $this->newQuery()->find($parentId);
if (!$parent) {
break;
}
array_unshift($path, $parent->toArray());
$parentId = $parent[$parentField];
}
return $path;
}
}