Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Descendants nested list #268

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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