Browse Source

added level option to tree

ever.zet 14 years ago
parent
commit
7221a64b3c

+ 2 - 1
lib/Gedmo/Tree/Mapping/Annotations.php

@@ -17,4 +17,5 @@ use Doctrine\Common\Annotations\Annotation;
  */
 final class TreeLeft extends Annotation {}
 final class TreeRight extends Annotation {}
-final class TreeParent extends Annotation {}
+final class TreeParent extends Annotation {}
+final class TreeLevel extends Annotation {}

+ 5 - 0
lib/Gedmo/Tree/Node.php

@@ -34,4 +34,9 @@ interface Node
      * in every tree there should be link to parent. To identify a relation
      * as parent relation to child use @Tree:Ancestor annotation on the related property
      */
+
+    /**
+     * @gedmo:TreeLevel
+     * level of node.
+     */
 }

+ 27 - 1
lib/Gedmo/Tree/TreeListener.php

@@ -52,6 +52,11 @@ class TreeListener implements EventSubscriber
      */
     const ANNOTATION_PARENT = 'Gedmo\Tree\Mapping\TreeParent';
     
+    /**
+     * Annotation to mark node level
+     */
+    const ANNOTATION_LEVEL = 'Gedmo\Tree\Mapping\TreeLevel';
+    
     /**
      * List of cached entity configurations
      *  
@@ -301,9 +306,18 @@ class TreeListener implements EventSubscriber
             $parent = $meta->getReflectionProperty($config['parent'])->getValue($entity);
             if ($parent === null) {
                 $this->_prepareRoot($em, $entity);
+                if (isset($config['level'])) {
+                    $meta->getReflectionProperty($config['level'])->setValue($entity, 0);
+                }
             } else {
                 $meta->getReflectionProperty($config['left'])->setValue($entity, 0);
                 $meta->getReflectionProperty($config['right'])->setValue($entity, 0);
+                if (isset($config['level'])) {
+                    $meta->getReflectionProperty($config['level'])->setValue(
+                        $entity,
+                        $meta->getReflectionProperty($config['level'])->getValue($parent) + 1
+                    );
+                }
                 $this->_pendingChildNodeInserts[] = $entity;
             }
         }
@@ -360,6 +374,7 @@ class TreeListener implements EventSubscriber
                 }
                 $config['left'] = $field;
             }
+            // right
             if ($right = $reader->getPropertyAnnotation($property, self::ANNOTATION_RIGHT)) {
                 $field = $property->getName();
                 if (!$meta->hasField($field)) {
@@ -378,6 +393,17 @@ class TreeListener implements EventSubscriber
                 }
                 $config['parent'] = $field;
             }
+            // level
+            if ($parent = $reader->getPropertyAnnotation($property, self::ANNOTATION_LEVEL)) {
+                $field = $property->getName();
+                if (!$meta->hasField($field)) {
+                    throw Exception::fieldMustBeMapped($field, $meta->name);
+                }
+                if (!$this->_isValidField($meta, $field)) {
+                    throw Exception::notValidFieldType($field, $meta->name);
+                }
+                $config['level'] = $field;
+            }
         }
     }
     
@@ -414,7 +440,7 @@ class TreeListener implements EventSubscriber
         
         $meta->getReflectionProperty($config['left'])->setValue($entity, $edge + 1);
         $meta->getReflectionProperty($config['right'])->setValue($entity, $edge + 2);
-            
+        
         $this->_treeEdge = $edge + 2;
     }
     

+ 6 - 0
tests/Gedmo/Tree/Fixture/Category.php

@@ -39,6 +39,12 @@ class Category implements Node
      */
     private $parent;
     
+    /**
+     * @gedmo:TreeLevel
+     * @Column(name="lvl", type="integer")
+     */
+     private $level;
+    
     /**
      * @OneToMany(targetEntity="Category", mappedBy="parent")
      */

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

@@ -79,16 +79,20 @@ class TreeTest extends \PHPUnit_Framework_TestCase
         $root = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(1);
         $left = $meta->getReflectionProperty('lft')->getValue($root);
         $right = $meta->getReflectionProperty('rgt')->getValue($root);
+        $level = $meta->getReflectionProperty('level')->getValue($root);
         
         $this->assertEquals($left, 1);
         $this->assertEquals($right, 4);
+        $this->assertEquals($level, 0);
         
         $child = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(2);
         $left = $meta->getReflectionProperty('lft')->getValue($child);
         $right = $meta->getReflectionProperty('rgt')->getValue($child);
+        $level = $meta->getReflectionProperty('level')->getValue($child);
         
         $this->assertEquals($left, 2);
         $this->assertEquals($right, 3);
+        $this->assertEquals($level, 1);
         
         $child2 = new Category();
         $child2->setTitle("child2");
@@ -101,16 +105,20 @@ class TreeTest extends \PHPUnit_Framework_TestCase
         $root = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(1);
         $left = $meta->getReflectionProperty('lft')->getValue($root);
         $right = $meta->getReflectionProperty('rgt')->getValue($root);
+        $level = $meta->getReflectionProperty('level')->getValue($root);
         
         $this->assertEquals($left, 1);
         $this->assertEquals($right, 6);
+        $this->assertEquals($level, 0);
         
         $child2 = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(3);
         $left = $meta->getReflectionProperty('lft')->getValue($child2);
         $right = $meta->getReflectionProperty('rgt')->getValue($child2);
+        $level = $meta->getReflectionProperty('level')->getValue($child2);
         
         $this->assertEquals($left, 4);
         $this->assertEquals($right, 5);
+        $this->assertEquals($level, 1);
         
         $childsChild = new Category();
         $childsChild->setTitle("childs2_child");
@@ -123,9 +131,15 @@ class TreeTest extends \PHPUnit_Framework_TestCase
         $child2 = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(3);
         $left = $meta->getReflectionProperty('lft')->getValue($child2);
         $right = $meta->getReflectionProperty('rgt')->getValue($child2);
+        $level = $meta->getReflectionProperty('level')->getValue($child2);
         
         $this->assertEquals($left, 4);
         $this->assertEquals($right, 7);
+        $this->assertEquals($level, 1);
+
+        $level = $meta->getReflectionProperty('level')->getValue($childsChild);
+
+        $this->assertEquals($level, 2);
         
         // test updates to nodes, parent changes
         
@@ -141,9 +155,11 @@ class TreeTest extends \PHPUnit_Framework_TestCase
         $child = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(2);
         $left = $meta->getReflectionProperty('lft')->getValue($child);
         $right = $meta->getReflectionProperty('rgt')->getValue($child);
+        $level = $meta->getReflectionProperty('level')->getValue($child);
         
         $this->assertEquals($left, 2);
         $this->assertEquals($right, 5);
+        $this->assertEquals($level, 1);
         
         // test deletion