PHP code example of sam152 / treenum

1. Go to this page and download the library: Download sam152/treenum library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sam152 / treenum example snippets


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 => [],
        };
    }
}

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

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

php > var_export(Pet::rootCases());
array (
  Pet::Dog,
  Pet::Bird,
  Pet::Cat,
)

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