PHPackages                             sam152/treenum - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sam152/treenum

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sam152/treenum
==============

PHP trees based on enums.

v1.0.0(2y ago)04MITPHPPHP &gt;=8.1

Since Nov 6Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Sam152/treenum)[ Packagist](https://packagist.org/packages/sam152/treenum)[ RSS](/packages/sam152-treenum/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

Treenum: tree + enum in PHP
===========================

[](#treenum-tree--enum-in-php)

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

[](#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
----------

[](#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:

[](#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

[](#additional-helpers)

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

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

922d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1080ab116ed4117f1361595d2b55163f9144b204308c41c36ebdba7f5d01001e?d=identicon)[Sam152](/maintainers/Sam152)

---

Top Contributors

[![Sam152](https://avatars.githubusercontent.com/u/820219?v=4)](https://github.com/Sam152 "Sam152 (4 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sam152-treenum/health.svg)

```
[![Health](https://phpackages.com/badges/sam152-treenum/health.svg)](https://phpackages.com/packages/sam152-treenum)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
