sam152/treenum 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sam152/treenum

最新稳定版本:v1.0.0

Composer 安装命令:

composer require sam152/treenum

包简介

PHP trees based on enums.

README 文档

README

Treenum is a lightweight library for defining and traversing items in an enum, that have a tree structure.

composer require sam152/treenum

Defining a tree

Trees may be defined either by identifying the parents or the children of any given item, whichever is easier for the consumer.

Example of a tree identified by declaring children of any given item:

enum Pet implements TreeEnum {
    use GetChildrenImplementation;

    case Dog;
    case Retriever;
    case Labrador;
    case Golden;
    case Terrier;
    case Bird;
    case Chicken;
    case Cat;

    public function getChildren(): array {
        return match ($this) {
            static::Dog => [
                static::Retriever,
                static::Terrier,
            ],
            static::Retriever => [
                static::Labrador,
                static::Golden,
            ],
            static::Bird => [
                static::Chicken,
            ],
            default => [],
        };
    }
}

And the same tree identified by declaring the parent of any given item:

enum Pet implements TreeEnum {
    use GetParentImplementation;

    case Dog;
    case Retriever;
    case Labrador;
    case Golden;
    case Terrier;
    case Bird;
    case Chicken;
    case Cat;

    public function getParent(): static|null {
        return match($this) {
            static::Labrador, static::Golden => static::Retriever,
            static::Retriever, static::Terrier => static::Dog,
            static::Chicken => static::Bird,
            default => null,
        };
    }
}

Public API

The following methods are defined on TreeEnum and can be used to traverse the tree:

public function getAncestors(): array;
public function getDescendants(): array;
public function getChildren(): array;
public function getParent(): static | null;
public function getDepth(): int;
public static function rootCases(): array;
public static function leafCases(): array;

Example usage:

php > var_export(Pet::Dog->getChildren());
array (
  Pet::Retriever,
  Pet::Terrier,
)
php > var_export(Pet::rootCases());
array (
  Pet::Dog,
  Pet::Bird,
  Pet::Cat,
)

Additional helpers

php > print \Treenum\Internal\Utility::dumpTree(Pet::class);
.
├── Dog
│   ├── Retriever
│   │   ├── Labrador
│   │   └── Golden
│   └── Terrier
├── Bird
│   └── Chicken
└── Cat

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-06