PHPackages                             jlvn/tree-transform - 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. jlvn/tree-transform

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

jlvn/tree-transform
===================

A library to perform transformations on tree-like structures.

0.0.1(4y ago)07MITPHPPHP &gt;=7.4

Since Apr 18Pushed 4y ago1 watchersCompare

[ Source](https://github.com/jlvn/tree-transform)[ Packagist](https://packagist.org/packages/jlvn/tree-transform)[ RSS](/packages/jlvn-tree-transform/feed)WikiDiscussions develop Synced today

READMEChangelog (1)Dependencies (2)Versions (5)Used By (0)

Tree Transform
==============

[](#tree-transform)

A library to perform transformations on tree-like structures.

Usage
-----

[](#usage)

This library exposes a main class called: `TreeTransformer`. With this class a user can transform a tree-like structure with different data-types on a data-type by data-type basis. This is done using so-called `TreeTransformable`s.

### TreeTransformables

[](#treetransformables)

A TreeTransformable defines the transforming behaviour of a certain data-type. All TreeTransformables must implement the `TreeTransformableInterface`.

#### Using the GenericTreeTransformable class

[](#using-the-generictreetransformable-class)

This library exposes the `GenericTreeTransformable` class. With this class TreeTransformables can be created on the fly without having to define a class for each data type.

```
// A transformable that transforms a string into an array of strings
$stringTreeTransformable = new GenericTreeTransformable(
    gettype(''), // the tag of the transformable. 'string' in this case.
    fn() => [], // the branches of the current transformable. None in this case.
    fn(string $trunk) => split('', $trunk), // the transform function
)
```

#### implementing your own transformables

[](#implementing-your-own-transformables)

Another option is to simply implement the `TreeTransformableInterface` for your own specific data type.

```
class Custom {
    public string $name;
    public array $children;
}

class CustomTreeTransformable implements TreeTransformableInterface {
    public function getTag(): string {
        return Custom::class
    }

    public function getBranches($trunk): array {
        return $trunk->children;
    }

    public function transform($trunk, ReadOnlyMapInterface $branches) {
        return [
            'name' => $trunk->name,
            'customChildren' => $branches->getOrDefault(Custom::class, [])
        ]
    }
}
```

### Using the TreeTransformer

[](#using-the-treetransformer)

Now that we have defined our `TreeTransformable`s we can use them in the TreeTransformer. The TreeTransformer uses a simple depth first algorithm to traverse the tree. Tou can interact with the TreeTransformer in a multitude of ways.

```
$transformer = new TreeTransformer;

// Throws a NotFoundException when the transformable with a certain tag is not found.
$transformer->tryTransform($data);

// uses a default transformable (PlaceboTreeTransformer by default) when the transformable
// with a certain tag is not found.
$transformer->transformOrDefault($data);
```

#### Usage with method supplied transformables

[](#usage-with-method-supplied-transformables)

```
$transformer = new TreeTransformer;

$transformableMap = new TreeTransformableTagReadOnlyMap([
    $stringTreeTransformable,
    new CustomTreeTransformable
]);

try {
    $transformer->tryTransform($data, $transformableMap);
} catch(NotfoundException $e) {
    // thrown if transformable is not found
}

$transformer->transformOrDefault($data, $transformableMap);

//it is also possible to supply a new default transformable
$transformer->transformOrDefault($data, $transformableMap, $stringTreeTransformable);
```

#### Usage with constructor or default supplied transformables

[](#usage-with-constructor-or-default-supplied-transformables)

```
$transformableMap = new TreeTransformableTagReadOnlyMap([
    $stringTreeTransformable,
    new CustomTreeTransformable
]);

$transformer = new TreeTransformer($stringTreeTransformable, $transformableMap);

try {
    $transformer->tryTransform($data);
} catch(NotfoundException $e) {
    // thrown if transformable is not found
}

$transformer->transformOrDefault($data);
```

###  Health Score

20

—

LowBetter than 13% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity42

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

1536d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/22853045?v=4)[Jelmer](/maintainers/jlvn)[@jlvn](https://github.com/jlvn)

---

Top Contributors

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

---

Tags

php-librarytransformationtree-structure

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jlvn-tree-transform/health.svg)

```
[![Health](https://phpackages.com/badges/jlvn-tree-transform/health.svg)](https://phpackages.com/packages/jlvn-tree-transform)
```

###  Alternatives

[aimeos/laravel-analytics-bridge

Unified analytics bridge for Laravel

1652.1k9](/packages/aimeos-laravel-analytics-bridge)[fleetbase/fleetops-api

Fleet &amp; Transport Management Extension for Fleetbase

1731.5k7](/packages/fleetbase-fleetops-api)[kinglozzer/multiselectfield

A sortable multiple select field for managing many-to-many relations

1342.6k2](/packages/kinglozzer-multiselectfield)

PHPackages © 2026

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