Skip to content

Conversation

@vitorbari
Copy link

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',
// );

Add test for getDescendantsNestedList
Add method getDescendantsAndSelfNestedList
Add tests for getDescendantsNestedList  and getDescendantsAndSelfNestedList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant