etrepat / baum

Baum is an implementation of the Nested Set pattern for Laravel's Eloquent ORM.
http://etrepat.com/baum
MIT License
2.24k stars 459 forks source link

Descendants nested list #268

Open vitorbari opened 7 years ago

vitorbari commented 7 years ago

This PR adds 2 methods to generate nested list from a node instance.

The new methods are: getDescendantsNestedList and getDescendantsAndSelfNestedList. They receive the same parameters of the static method getNestedList.

An example use case:

/* Given the tree:
root
  |_ Child 1
    |_ Child 1.1
    |_ Child 1.2
  |_ Child 2
    |_ Child 2.1
    |_ Child 2.2
*/

// $node represents root
$nestedList = $node->getDescendantsNestedList('name');
// $nestedList will contain an array like the following:
// array(
//   2 => 'Child 1',
//   3 => ' Child 1.1',
//   4 => ' Child 1.2',
//   5 => 'Child 2',
//   6 => ' Child 2.1',
//   7 => ' Child 2.2',
// );

// $node represents Child 2
$nestedList = $node->getDescendantsNestedList('name');
// $nestedList will contain an array like the following:
// array(
//   6 => 'Child 2.1',
//   7 => 'Child 2.1',
// );

// $node represents root
$nestedList = $node->getDescendantsAndSelfNestedList('name');
// $nestedList will contain an array like the following:
// array(
//   1 => 'root',
//   2 => ' Child 1',
//   3 => '  Child 1.1',
//   4 => '  Child 1.2',
//   5 => ' Child 2',
//   6 => '  Child 2.1',
//   7 => '  Child 2.2',
// );

// $node represents Child 2
$nestedList = $node->getDescendantsAndSelfNestedList('name');
// $nestedList will contain an array like the following:
// array(
//   5 => 'Child 2',
//   6 => ' Child 2.1',
//   7 => ' Child 2.1',
// );