Sfoglia il codice sorgente

[tree] when result is empty array, give empty string for tree result

gedi 13 anni fa
parent
commit
6849588693

+ 4 - 2
lib/Gedmo/Tree/Entity/Repository/NestedTreeRepository.php

@@ -993,7 +993,7 @@ class NestedTreeRepository extends AbstractTreeRepository
      *
      * @return array|string
      */
-    public function buildTree(array $nodes, array &$options = array())
+    public function buildTree(array $nodes, array $options = array())
     {
         //process the nested tree into a nested array
         $meta = $this->getClassMetadata();
@@ -1049,8 +1049,10 @@ class NestedTreeRepository extends AbstractTreeRepository
         );
         $options = array_merge($default, $options);
         // If you don't want any html output it will return the nested array
-        if (!$options['decorate'] || !count($nestedTree)) {
+        if (!$options['decorate']) {
             return $nestedTree;
+        } elseif (!count($nestedTree)) {
+            return '';
         }
 
         $build = function($tree) use (&$build, &$options) {

+ 3 - 0
tests/Gedmo/Tree/NestedTreeRootRepositoryTest.php

@@ -129,6 +129,9 @@ class NestedTreeRootRepositoryTest extends BaseTestCaseORM
         $tree = $repo->buildTree($q->getArrayResult());
         $this->assertEquals(1, count($tree));
         $this->assertEquals(2, count($tree[0]['__children']));
+        $nodes = array();
+        $options = array('decorate' => true);
+        $this->assertEquals('', $repo->buildTree($nodes, $options), 'should give empty string when there are no nodes given');
     }
 
     public function testRootRemoval()