Parcourir la source

[Tree] Allow to change the default index "__children" used to hold the children of nodes.

comfortablynumb il y a 12 ans
Parent
commit
8690cabf9c

+ 1 - 0
doc/tree.md

@@ -1197,6 +1197,7 @@ There are repository methods that are available for you in all the strategies:
       * childClose: string ('\</li\>') - close of node
       * childSort: array || keys allowed: field: field to sort on, dir: direction. 'asc' or 'desc'
   - *includeNode*: Using "true", this argument allows you to include in the result the node you passed as the first argument. Defaults to "false".
+* **setChildrenIndex** / **getChildrenIndex**: These methods allow you to change the default index used to hold the children when you use the **childrenHierarchy** method. Index defaults to "__children".
 
 This list is not complete yet. We're working on including more methods in the common API offered by repositories of all the strategies.
 Soon we'll be adding more helpful methods here.

+ 16 - 0
lib/Gedmo/Tree/Document/MongoDB/Repository/AbstractTreeRepository.php

@@ -95,6 +95,22 @@ abstract class AbstractTreeRepository extends DocumentRepository implements Repo
         return $this->repoUtils->buildTree($nodes, $options);
     }
 
+    /**
+     * @see \Gedmo\Tree\RepositoryUtilsInterface::setChildrenIndex
+     */
+    public function setChildrenIndex($childrenIndex)
+    {
+        $this->repoUtils->setChildrenIndex($childrenIndex);
+    }
+
+    /**
+     * @see \Gedmo\Tree\RepositoryUtilsInterface::getChildrenIndex
+     */
+    public function getChildrenIndex()
+    {
+        return $this->repoUtils->getChildrenIndex();
+    }
+
     /**
      * {@inheritDoc}
      */

+ 16 - 0
lib/Gedmo/Tree/Entity/Repository/AbstractTreeRepository.php

@@ -143,6 +143,22 @@ abstract class AbstractTreeRepository extends EntityRepository implements Reposi
         return $this->repoUtils->buildTreeArray($nodes);
     }
 
+    /**
+     * @see \Gedmo\Tree\RepositoryUtilsInterface::setChildrenIndex
+     */
+    public function setChildrenIndex($childrenIndex)
+    {
+        $this->repoUtils->setChildrenIndex($childrenIndex);
+    }
+
+    /**
+     * @see \Gedmo\Tree\RepositoryUtilsInterface::getChildrenIndex
+     */
+    public function getChildrenIndex()
+    {
+        return $this->repoUtils->getChildrenIndex();
+    }
+
     /**
      * Checks if current repository is right
      * for currently used tree strategy

+ 3 - 2
lib/Gedmo/Tree/Entity/Repository/ClosureTreeRepository.php

@@ -290,6 +290,7 @@ class ClosureTreeRepository extends AbstractTreeRepository
         $idField = $meta->getSingleIdentifierFieldName();
         $hasLevelProp = !empty($config['level']);
         $levelProp = $hasLevelProp ? $config['level'] : self::SUBQUERY_LEVEL;
+        $childrenIndex = $this->repoUtils->getChildrenIndex();
 
         if (count($nodes) > 0) {
             $firstLevel = $hasLevelProp ? $nodes[0][0]['descendant'][$levelProp] : $nodes[0][$levelProp];
@@ -298,7 +299,7 @@ class ClosureTreeRepository extends AbstractTreeRepository
 
             foreach ($nodes as $n) {
                 $node = $n[0]['descendant'];
-                $node['__children'] = array();
+                $node[$childrenIndex] = array();
                 $level = $hasLevelProp ? $node[$levelProp] : $n[$levelProp];
 
                 if ($l < $level) {
@@ -308,7 +309,7 @@ class ClosureTreeRepository extends AbstractTreeRepository
                 if ($l == $firstLevel) {
                     $tmp = &$nestedTree;
                 } else {
-                    $tmp = &$refs[$n['parent_id']]['__children'];
+                    $tmp = &$refs[$n['parent_id']][$childrenIndex];
                 }
 
                 $key = count($tmp);

+ 35 - 8
lib/Gedmo/Tree/RepositoryUtils.php

@@ -20,6 +20,15 @@ class RepositoryUtils implements RepositoryUtilsInterface
     /** @var \Gedmo\Tree\RepositoryInterface */
     protected $repo;
 
+    /**
+     * This index is used to hold the children of a node
+     * when using any of the buildTree related methods.
+     *
+     * @var string
+     */
+    protected $childrenIndex = '__children';
+
+
     public function __construct(ObjectManager $om, ClassMetadata $meta, $listener, $repo)
     {
         $this->om = $om;
@@ -57,7 +66,7 @@ class RepositoryUtils implements RepositoryUtilsInterface
         // Gets the array of $node results. It must be ordered by depth
         $nodes = $this->repo->getNodesHierarchy($node, $direct, $options, $includeNode);
 
-        return $this->buildTree($nodes, $options);
+        return $this->repo->buildTree($nodes, $options);
     }
 
     /**
@@ -96,13 +105,15 @@ class RepositoryUtils implements RepositoryUtilsInterface
             return '';
         }
 
-        $build = function($tree) use (&$build, &$options) {
+        $childrenIndex = $this->childrenIndex;
+
+        $build = function($tree) use (&$build, &$options, $childrenIndex) {
             $output = is_string($options['rootOpen']) ? $options['rootOpen'] : $options['rootOpen']($tree);
             foreach ($tree as $node) {
                 $output .= is_string($options['childOpen']) ? $options['childOpen'] : $options['childOpen']($node);
                 $output .= $options['nodeDecorator']($node);
-                if (count($node['__children']) > 0) {
-                    $output .= $build($node['__children']);
+                if (count($node[$childrenIndex]) > 0) {
+                    $output .= $build($node[$childrenIndex]);
                 }
                 $output .= $options['childClose'];
             }
@@ -127,7 +138,7 @@ class RepositoryUtils implements RepositoryUtilsInterface
             $stack = array();
             foreach ($nodes as $child) {
                 $item = $child;
-                $item['__children'] = array();
+                $item[$this->childrenIndex] = array();
                 // Number of stack items
                 $l = count($stack);
                 // Check if we're dealing with different levels
@@ -143,13 +154,29 @@ class RepositoryUtils implements RepositoryUtilsInterface
                     $stack[] = &$nestedTree[$i];
                 } else {
                     // Add child to parent
-                    $i = count($stack[$l - 1]['__children']);
-                    $stack[$l - 1]['__children'][$i] = $item;
-                    $stack[] = &$stack[$l - 1]['__children'][$i];
+                    $i = count($stack[$l - 1][$this->childrenIndex]);
+                    $stack[$l - 1][$this->childrenIndex][$i] = $item;
+                    $stack[] = &$stack[$l - 1][$this->childrenIndex][$i];
                 }
             }
         }
 
         return $nestedTree;
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function setChildrenIndex($childrenIndex)
+    {
+        $this->childrenIndex = $childrenIndex;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getChildrenIndex()
+    {
+        return $this->childrenIndex;
+    }
 }

+ 14 - 0
lib/Gedmo/Tree/RepositoryUtilsInterface.php

@@ -55,4 +55,18 @@ interface RepositoryUtilsInterface
      * @return array - Array with tree structure
      */
     public function buildTreeArray(array $nodes);
+
+    /**
+     * Sets the current children index.
+     *
+     * @param string $childrenIndex
+     */
+    public function setChildrenIndex($childrenIndex);
+
+    /**
+     * Gets the current children index.
+     *
+     * @return string
+     */
+    public function getChildrenIndex();
 }

+ 14 - 1
tests/Gedmo/Tree/ClosureTreeRepositoryTest.php

@@ -193,7 +193,7 @@ class ClosureTreeRepositoryTest extends BaseTestCaseORM
         $this->assertFalse(strpos($qb->getQuery()->getDql(), '(SELECT MAX('));
     }
 
-    public function testoNotHavingLevelPropertyUsesASubqueryInSelectInGetNodesHierarchy()
+    public function testNotHavingLevelPropertyUsesASubqueryInSelectInGetNodesHierarchy()
     {
         $this->populate(self::CATEGORY_WITHOUT_LEVEL);
 
@@ -206,6 +206,19 @@ class ClosureTreeRepositoryTest extends BaseTestCaseORM
         $this->assertTrue(((bool) strpos($qb->getQuery()->getDql(), '(SELECT MAX(')));
     }
 
+    public function test_changeChildrenIndex()
+    {
+        $this->populate(self::CATEGORY);
+
+        $childrenIndex = 'myChildren';
+        $repo = $this->em->getRepository(self::CATEGORY);
+        $repo->setChildrenIndex($childrenIndex);
+
+        $tree = $repo->childrenHierarchy();
+
+        $this->assertInternalType('array', $tree[0][$childrenIndex]);
+    }
+
     // Utility Methods
 
     protected function buildTreeTests($class)

+ 10 - 0
tests/Gedmo/Tree/MaterializedPathODMMongoDBRepositoryTest.php

@@ -245,6 +245,16 @@ class MaterializedPathODMMongoDBRepositoryTest extends BaseTestCaseMongoODM
         $this->repo->childCount($this->createCategory());
     }
 
+    public function test_changeChildrenIndex()
+    {
+        $childrenIndex = 'myChildren';
+        $this->repo->setChildrenIndex($childrenIndex);
+
+        $tree = $this->repo->childrenHierarchy();
+
+        $this->assertInternalType('array', $tree[0][$childrenIndex]);
+    }
+
     protected function getUsedEntityFixtures()
     {
         return array(

+ 10 - 0
tests/Gedmo/Tree/MaterializedPathORMRepositoryTest.php

@@ -265,6 +265,16 @@ class MaterializedPathORMRepositoryTest extends BaseTestCaseORM
         $this->assertEquals(2, $newNode->getLevel());
     }
 
+    public function test_changeChildrenIndex()
+    {
+        $childrenIndex = 'myChildren';
+        $this->repo->setChildrenIndex($childrenIndex);
+
+        $tree = $this->repo->childrenHierarchy();
+
+        $this->assertInternalType('array', $tree[0][$childrenIndex]);
+    }
+
     protected function getUsedEntityFixtures()
     {
         return array(

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

@@ -427,6 +427,20 @@ class NestedTreeRootRepositoryTest extends BaseTestCaseORM
         $this->assertEquals('Food', $roots[1]->getTitle());
     }
 
+    /**
+     * @test
+     */
+    public function changeChildrenIndexTest()
+    {
+        $repo = $this->em->getRepository(self::CATEGORY);
+        $childrenIndex = 'myChildren';
+        $repo->setChildrenIndex($childrenIndex);
+
+        $tree = $repo->childrenHierarchy();
+
+        $this->assertInternalType('array', $tree[0][$childrenIndex]);
+    }
+
     protected function getUsedEntityFixtures()
     {
         return array(