PHP code example of thinktomorrow / vine

1. Go to this page and download the library: Download thinktomorrow/vine 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/ */

    

thinktomorrow / vine example snippets


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

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

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

$inflatedCollection = $collection->inflate();

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

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

$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

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

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

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