From d066a4e5ee8f7c342268b301b19e6a6ed86a74a6 Mon Sep 17 00:00:00 2001
From: Vitor Bari Buccianti <vitorbari@gmail.com>
Date: Thu, 13 Apr 2017 09:57:45 -0300
Subject: [PATCH 1/2] Add getDescendantsNestedList method Add test for
 getDescendantsNestedList

---
 src/Baum/Node.php                             | 19 +++++++++++++++++++
 .../suite/Category/CategoryHierarchyTest.php  | 17 +++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/src/Baum/Node.php b/src/Baum/Node.php
index 8dcab130..f4a981b4 100644
--- a/src/Baum/Node.php
+++ b/src/Baum/Node.php
@@ -1214,6 +1214,25 @@ public static function getNestedList($column, $key = null, $seperator = ' ') {
     }, $nodes));
   }
 
+  /**
+   * Return an key-value array indicating the node's depth with $seperator
+   *
+   * @return Array
+   */
+  public function getDescendantsNestedList($column, $key = null, $seperator = ' ') {
+    $nodes = $this->getDescendants()->toArray();
+
+    $depthColumn = $this->getDepthColumnName();
+
+    $rootDepth = $this->{$depthColumn} + 1;
+
+    return array_combine(array_map(function($node) use($key) {
+      return $node[$key];
+    }, $nodes), array_map(function($node) use($seperator, $depthColumn, $column, $rootDepth) {
+      return str_repeat($seperator, $node[$depthColumn] - $rootDepth) . $node[$column];
+    }, $nodes));
+  }
+
   /**
    * Maps the provided tree structure into the database using the current node
    * as the parent. The provided tree structure will be inserted/updated as the
diff --git a/tests/suite/Category/CategoryHierarchyTest.php b/tests/suite/Category/CategoryHierarchyTest.php
index 74a3a551..7ef9bc9e 100644
--- a/tests/suite/Category/CategoryHierarchyTest.php
+++ b/tests/suite/Category/CategoryHierarchyTest.php
@@ -702,4 +702,21 @@ public function testGetNestedList() {
     $this->assertArraysAreEqual($expected, $nestedList);
   }
 
+  public function testGetDescendantsNestedList() {
+    $parent = $this->categories('Root 1');
+
+    $seperator = ' ';
+
+    $nestedList = $parent->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);
+  }
+
 }

From 18a880d8a99735d97ae19ee4e8a16033a9e811c1 Mon Sep 17 00:00:00 2001
From: Vitor Bari Buccianti <vitorbari@gmail.com>
Date: Thu, 13 Apr 2017 16:13:23 -0300
Subject: [PATCH 2/2] Fix getDescendantsNestedList docblock Add method
 getDescendantsAndSelfNestedList Add tests for getDescendantsNestedList  and
 getDescendantsAndSelfNestedList

---
 src/Baum/Node.php                             | 48 ++++++++++++++---
 .../suite/Category/CategoryHierarchyTest.php  | 53 ++++++++++++++++---
 2 files changed, 86 insertions(+), 15 deletions(-)

diff --git a/src/Baum/Node.php b/src/Baum/Node.php
index f4a981b4..b11ac05f 100644
--- a/src/Baum/Node.php
+++ b/src/Baum/Node.php
@@ -1207,16 +1207,15 @@ public static function getNestedList($column, $key = null, $seperator = ' ') {
 
     $nodes = $instance->newNestedSetQuery()->get()->toArray();
 
-    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));
+    return $instance->buildNestedList($nodes, $depthColumn, $column, $key, $seperator);
   }
 
   /**
-   * Return an key-value array indicating the node's depth with $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 = ' ') {
@@ -1226,9 +1225,42 @@ public function getDescendantsNestedList($column, $key = null, $seperator = ' ')
 
     $rootDepth = $this->{$depthColumn} + 1;
 
-    return array_combine(array_map(function($node) use($key) {
+    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, $rootDepth) {
+    }, $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 7ef9bc9e..68ca807f 100644
--- a/tests/suite/Category/CategoryHierarchyTest.php
+++ b/tests/suite/Category/CategoryHierarchyTest.php
@@ -703,20 +703,59 @@ public function testGetNestedList() {
   }
 
   public function testGetDescendantsNestedList() {
-    $parent = $this->categories('Root 1');
-
     $seperator = ' ';
 
-    $nestedList = $parent->getDescendantsNestedList('name', 'id', $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',
+      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);
+  }
 }
