diff --git a/src/Baum/Node.php b/src/Baum/Node.php index 8dcab130..b11ac05f 100644 --- a/src/Baum/Node.php +++ b/src/Baum/Node.php @@ -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)); } diff --git a/tests/suite/Category/CategoryHierarchyTest.php b/tests/suite/Category/CategoryHierarchyTest.php index 74a3a551..68ca807f 100644 --- a/tests/suite/Category/CategoryHierarchyTest.php +++ b/tests/suite/Category/CategoryHierarchyTest.php @@ -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); + } }