PHPackages                             thinktomorrow/vine - 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. thinktomorrow/vine

ActiveLibrary

thinktomorrow/vine
==================

Generate a tree from an adjacent database structure

0.5.3(3mo ago)240.8k↓33.6%[1 issues](https://github.com/thinktomorrow/vine/issues)3MITPHPPHP &gt;=8.2CI passing

Since Jun 8Pushed 3mo ago4 watchersCompare

[ Source](https://github.com/thinktomorrow/vine)[ Packagist](https://packagist.org/packages/thinktomorrow/vine)[ RSS](/packages/thinktomorrow-vine/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (33)Used By (3)

Vine
====

[](#vine)

`Thinktomorrow\Vine` is a PHP package designed to manage and manipulate **adjacent tree-structured models**. It provides a powerful interface for building and querying collections of hierarchical data, with methods to traverse, sort, and manipulate node collections in a flexible way.

It's key features are:

- **Tree Traversal**: Map and traverse nodes with recursion.
- **Manipulation**: Add, remove, or merge nodes.
- **Flattening &amp; Inflating**: Convert hierarchical data to flat lists and vice versa.
- **Customizable Queries**: Find nodes based on specific attributes.

**IMPORTANT**An adjacent tree structure is assumed, where each node has an `id` and `parent_id` attribute.

Installation
------------

[](#installation)

Install the package via composer:

```
composer require thinktomorrow/vine
```

Basic Usage
-----------

[](#basic-usage)

### 1. Creating a Node Collection

[](#1-creating-a-node-collection)

You can create a node collection from an array of nodes.

```
use Thinktomorrow\Vine\NodeCollection;
use Thinktomorrow\Vine\Node;

// Assuming Node is a model implementing the Node interface
$nodes = [
    new Node(['id' => 1, 'name' => 'Parent']),
    new Node(['id' => 2, 'name' => 'Child', 'parent_id' => 1])
];

$collection = NodeCollection::fromArray($nodes);
```

### 2. Traversing Nodes

[](#2-traversing-nodes)

The package allows you to iterate over nodes recursively.

```
$collection->eachRecursive(function($node) {
    echo $node->getName();  // Access node attributes
});
```

### 3. Flattening a Tree

[](#3-flattening-a-tree)

To get a flat array of all nodes:

```
$flatNodes = $collection->flatten()->toArray();
```

### 4. Inflating a Flattened Collection

[](#4-inflating-a-flattened-collection)

You can restore a flattened collection back to its tree structure:

```
$inflatedCollection = $collection->inflate();
```

### 5. Finding Nodes

[](#5-finding-nodes)

To find nodes by specific attributes:

```
$node = $collection->find('id', 1); // Find a node with id = 1
```

Or find multiple nodes by a set of values:

```
$nodes = $collection->findMany('id', [1, 2, 3]); // Find nodes with ids 1, 2, and 3
```

### 6. Adding and Merging Nodes

[](#6-adding-and-merging-nodes)

You can add or merge nodes into the collection:

```
$newNode = new Node(['id' => 3, 'name' => 'New Child']);
$collection->add($newNode); // Add a new node

$otherCollection = NodeCollection::fromArray([...]); // Another node collection
$collection->merge($otherCollection); // Merge collections
```

### 7. Sorting Nodes

[](#7-sorting-nodes)

Nodes can be sorted by any attribute:

```
$sortedCollection = $collection->sort('name'); // Sort by 'name' attribute
```

### 8. Removing Nodes

[](#8-removing-nodes)

To remove nodes by attribute or condition:

```
$collection = $collection->remove(function($node) {
    return $node->getId() == 2; // Remove node with id 2
});
```

Example: Full Usage
-------------------

[](#example-full-usage)

```
use Thinktomorrow\Vine\NodeCollection;
use Thinktomorrow\Vine\Node;

$nodes = [
    new Node(['id' => 1, 'name' => 'Root']),
    new Node(['id' => 2, 'name' => 'Child 1', 'parent_id' => 1]),
    new Node(['id' => 3, 'name' => 'Child 2', 'parent_id' => 1])
];

$collection = NodeCollection::fromArray($nodes);

// Add a node
$collection->add(new Node(['id' => 4, 'name' => 'New Child', 'parent_id' => 2]));

// Flatten, sort, and remove
$flatNodes = $collection->flatten()->sort('name')->remove(function($node) {
    return $node->getName() === 'Child 2';
});

// Output as array
print_r($flatNodes->toArray());
```

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance69

Regular maintenance activity

Popularity31

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 99% 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

Every ~105 days

Recently: every ~269 days

Total

31

Last Release

111d ago

PHP version history (5 changes)0.2.1PHP &gt;=7.0

0.2.7PHP &gt;=7.2

0.3.0PHP &gt;=7.4

0.4.0PHP &gt;=8.1

0.5.1PHP &gt;=8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/497668?v=4)[Ben Cavens](/maintainers/BenCavens)[@BenCavens](https://github.com/BenCavens)

---

Top Contributors

[![BenCavens](https://avatars.githubusercontent.com/u/497668?v=4)](https://github.com/BenCavens "BenCavens (191 commits)")[![yinx](https://avatars.githubusercontent.com/u/1022847?v=4)](https://github.com/yinx "yinx (2 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/thinktomorrow-vine/health.svg)

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

PHPackages © 2026

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