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

@@ -59,7 +59,7 @@ trait AetherEnum
/**
* 根据值获取枚举实例(严格模式).
* @param int|string $value 枚举值
* @return AetherEnum|NoticeStatsModel|NoticeStatusEnum 枚举实例
* @return AetherEnum 枚举实例
*/
public static function fromValue(int|string $value): self
{
@@ -95,7 +95,7 @@ trait AetherEnum
/**
* 根据描述获取枚举实例(精确匹配).
* @param string $description 描述文本
* @return null|AetherEnum|NoticeStatsModel|NoticeStatusEnum 匹配的枚举实例无匹配时返回null
* @return null|AetherEnum 匹配的枚举实例无匹配时返回null
*/
public static function fromDescription(string $description): ?self
{

View File

@@ -1,24 +1,26 @@
<?php
declare(strict_types=1);
namespace Aether\Traits;
use Hyperf\Database\Model\SoftDeletes;
/**
* 通用软删除Trait供需要软删除的模型使用
* 通用软删除Trait供需要软删除的模型使用.
*/
trait AetherSoftDelete
{
use SoftDeletes;
/**
* 初始化软删除相关配置自动隐藏deleted_at字段
* 初始化软删除相关配置自动隐藏deleted_at字段.
*/
protected function initializeAetherSoftDeletes(): void
{
// 自动将deleted_at添加到隐藏字段避免序列化时暴露
if (!in_array('deleted_at', $this->hidden, true)) {
if (! in_array('deleted_at', $this->hidden, true)) {
$this->hidden[] = 'deleted_at';
}
}
}
}

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;
}
}