Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions src/Baum/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -1207,10 +1207,61 @@ public static function getNestedList($column, $key = null, $seperator = ' ') {

$nodes = $instance->newNestedSetQuery()->get()->toArray();

return array_combine(array_map(function($node) use($key) {
return $instance->buildNestedList($nodes, $depthColumn, $column, $key, $seperator);
}

/**
* Return an key-value array with all nested children indicating the node's depth with $seperator
*
* @param $column
* @param null $key
* @param string $seperator
* @return Array
*/
public function getDescendantsNestedList($column, $key = null, $seperator = ' ') {
$nodes = $this->getDescendants()->toArray();

$depthColumn = $this->getDepthColumnName();

$rootDepth = $this->{$depthColumn} + 1;

return $this->buildNestedList($nodes, $depthColumn, $column, $key, $seperator, $rootDepth);
}

/**
* Return an key-value array with all nested children and self indicating the node's depth with $seperator
*
* @param $column
* @param null $key
* @param string $seperator
* @return Array
*/
public function getDescendantsAndSelfNestedList($column, $key = null, $seperator = ' ') {
$nodes = $this->getDescendantsAndSelf()->toArray();

$depthColumn = $this->getDepthColumnName();

$rootDepth = $this->{$depthColumn};

return $this->buildNestedList($nodes, $depthColumn, $column, $key, $seperator, $rootDepth);
}

/**
* Build key-value array
*
* @param array $nodes
* @param $depthColumn
* @param $column
* @param null $key
* @param string $seperator
* @param int $rootDepth
* @return Array
*/
private function buildNestedList(array $nodes, $depthColumn, $column, $key = null, $seperator = ' ', $rootDepth = 0) {
return array_combine(array_map(function ($node) use ($key) {
return $node[$key];
}, $nodes), array_map(function($node) use($seperator, $depthColumn, $column) {
return str_repeat($seperator, $node[$depthColumn]) . $node[$column];
}, $nodes), array_map(function ($node) use ($seperator, $depthColumn, $column, $rootDepth) {
return str_repeat($seperator, $node[$depthColumn] - $rootDepth) . $node[$column];
}, $nodes));
}

Expand Down
56 changes: 56 additions & 0 deletions tests/suite/Category/CategoryHierarchyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,4 +702,60 @@ public function testGetNestedList() {
$this->assertArraysAreEqual($expected, $nestedList);
}

public function testGetDescendantsNestedList() {
$seperator = ' ';

$root = $this->categories('Root 1');

$nestedList = $root->getDescendantsNestedList('name', 'id', $seperator);

$expected = array(
2 => str_repeat($seperator, 0) . 'Child 1',
3 => str_repeat($seperator, 0) . 'Child 2',
4 => str_repeat($seperator, 1) . 'Child 2.1',
5 => str_repeat($seperator, 0) . 'Child 3',
);

$this->assertArraysAreEqual($expected, $nestedList);


$child = $this->categories('Child 2');

$nestedList = $child->getDescendantsNestedList('name', 'id', $seperator);

$expected = array(
4 => str_repeat($seperator, 0) . 'Child 2.1',
);

$this->assertArraysAreEqual($expected, $nestedList);
}

public function testGetDescendantsAndSelfNestedList() {
$seperator = ' ';

$root = $this->categories('Root 1');

$nestedList = $root->getDescendantsAndSelfNestedList('name', 'id', $seperator);

$expected = array(
1 => str_repeat($seperator, 0) . 'Root 1',
2 => str_repeat($seperator, 1) . 'Child 1',
3 => str_repeat($seperator, 1) . 'Child 2',
4 => str_repeat($seperator, 2) . 'Child 2.1',
5 => str_repeat($seperator, 1) . 'Child 3',
);

$this->assertArraysAreEqual($expected, $nestedList);

$child = $this->categories('Child 2');

$nestedList = $child->getDescendantsAndSelfNestedList('name', 'id', $seperator);

$expected = array(
3 => str_repeat($seperator, 0) . 'Child 2',
4 => str_repeat($seperator, 1) . 'Child 2.1',
);

$this->assertArraysAreEqual($expected, $nestedList);
}
}