Forráskód Böngészése

[tree] builtree options closure support for decorations, closes #216

gedi 13 éve
szülő
commit
0b738f77d7

+ 18 - 7
lib/Gedmo/Tree/Entity/Repository/NestedTreeRepository.php

@@ -792,9 +792,9 @@ class NestedTreeRepository extends AbstractTreeRepository
      * @param array $options :
      *     decorate: boolean (false) - retrieves tree as UL->LI tree
      *     nodeDecorator: Closure (null) - uses $node as argument and returns decorated item as string
-     *     rootOpen: string ('<ul>') - branch start
+     *     rootOpen: string || Closure ('<ul>') - branch start, closure will be given $children as a parameter
      *     rootClose: string ('</ul>') - branch close
-     *     childStart: string ('<li>') - start of node
+     *     childStart: string || Closure ('<li>') - start of node, closure will be given $node as a parameter
      *     childClose: string ('</li>') - close of node
      *
      * @return array|string
@@ -977,10 +977,20 @@ class NestedTreeRepository extends AbstractTreeRepository
     }
 
     /**
-     * Builds the tree
+     * Retrieves the nested array or the decorated output.
+     * Uses @options to handle decorations
+     * NOTE: @nodes should be fetched and hydrated as array
+     *
+     * @throws \Gedmo\Exception\InvalidArgumentException
+     * @param array $nodes - list o nodes to build tree
+     * @param array $options :
+     *     decorate: boolean (false) - retrieves tree as UL->LI tree
+     *     nodeDecorator: Closure (null) - uses $node as argument and returns decorated item as string
+     *     rootOpen: string || Closure ('<ul>') - branch start, closure will be given $children as a parameter
+     *     rootClose: string ('</ul>') - branch close
+     *     childStart: string || Closure ('<li>') - start of node, closure will be given $node as a parameter
+     *     childClose: string ('</li>') - close of node
      *
-     * @param array $nodes - array result of ORM query
-     * @param array $options
      * @return array|string
      */
     public function buildTree(array $nodes, array &$options = array())
@@ -1044,9 +1054,10 @@ class NestedTreeRepository extends AbstractTreeRepository
         }
 
         $build = function($tree) use (&$build, &$options) {
-            $output = $options['rootOpen'];
+            $output = is_string($options['rootOpen']) ? $options['rootOpen'] : $options['rootOpen']($tree);
             foreach ($tree as $node) {
-                $output .= $options['childOpen'] . $options['nodeDecorator']($node);
+                $output .= is_string($options['childOpen']) ? $options['childOpen'] : $options['childOpen']($node);
+                $output .= $options['nodeDecorator']($node);
                 if (count($node['__children']) > 0) {
                     $output .= $build($node['__children']);
                 }

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

@@ -94,6 +94,22 @@ class NestedTreeRootRepositoryTest extends BaseTestCaseORM
             "-Fruits\n-Vegitables\n--Carrots\n--Potatoes\n",
             $decoratedCliTree
         );
+
+        $rootOpen = function () {return '<ul class="group">';};
+        $childOpen = function (&$node) {
+            return '<li class="depth'.$node['level'].'">';
+        };
+
+        $decoratedHtmlTree = $repo->childrenHierarchy(
+            $food,
+            false,
+            compact('decorate', 'rootOpen', 'childOpen')
+        );
+
+        $this->assertEquals(
+            '<ul class="group"><li class="depth1">Fruits</li><li class="depth1">Vegitables<ul class="group"><li class="depth2">Carrots</li><li class="depth2">Potatoes</li></ul></li></ul>',
+            $decoratedHtmlTree
+        );
     }
 
     /**