浏览代码

[Tree] MaterializedPath: Now separator is configurable

comfortablynumb 13 年之前
父节点
当前提交
011f6b4bc4

+ 1 - 1
lib/Gedmo/Mapping/Annotation/TreePath.php

@@ -19,6 +19,6 @@ use Doctrine\Common\Annotations\Annotation;
  */
 final class TreePath extends Annotation
 {
-
+    public $separator = ',';
 }
 

+ 1 - 1
lib/Gedmo/Tree/Document/MongoDB/Repository/MaterializedPathRepository.php

@@ -29,7 +29,7 @@ class MaterializedPathRepository extends AbstractTreeRepository
     {
         $meta = $this->getClassMetadata();
         $config = $this->listener->getConfiguration($this->dm, $meta->name);
-        $separator = preg_quote(MaterializedPath::PATH_SEPARATOR);
+        $separator = preg_quote($config['path_separator']);
         
         return $this->dm->createQueryBuilder()
             ->find($meta->name)

+ 5 - 1
lib/Gedmo/Tree/Mapping/Driver/Annotation.php

@@ -219,7 +219,7 @@ class Annotation implements AnnotationDriverInterface
                 $config['level'] = $field;
             }
             // path
-            if ($this->reader->getPropertyAnnotation($property, self::PATH)) {
+            if ($pathAnnotation = $this->reader->getPropertyAnnotation($property, self::PATH)) {
                 $field = $property->getName();
                 if (!$meta->hasField($field)) {
                     throw new InvalidMappingException("Unable to find 'path' - [{$field}] as mapped property in entity - {$meta->name}");
@@ -227,7 +227,11 @@ class Annotation implements AnnotationDriverInterface
                 if (!$this->isValidFieldForPath($meta, $field)) {
                     throw new InvalidMappingException("Tree Path field - [{$field}] type is not valid. It must be string or text in class - {$meta->name}");
                 }
+                if (strlen($pathAnnotation->separator) > 1) {
+                    throw new InvalidMappingException("Tree Path field - [{$field}] Separator {$pathAnnotation->separator} is invalid. It must be only one character long.");
+                }
                 $config['path'] = $field;
+                $config['path_separator'] = $pathAnnotation->separator;
             }
             // path source
             if ($this->reader->getPropertyAnnotation($property, self::PATH_SOURCE)) {

+ 8 - 0
lib/Gedmo/Tree/Mapping/Driver/Xml.php

@@ -126,7 +126,15 @@ class Xml extends BaseXml
                     if (!$this->isValidFieldForPath($meta, $field)) {
                         throw new InvalidMappingException("Tree Path field - [{$field}] type is not valid. It must be string or text in class - {$meta->name}");
                     }
+
+                    $separator = $this->_getAttribute($mapping->{'tree-path'}, 'separator');
+
+                    if (strlen($separator) > 1) {
+                        throw new InvalidMappingException("Tree Path field - [{$field}] Separator {$separator} is invalid. It must be only one character long.");
+                    }
+
                     $config['path'] = $field;
+                    $config['path_separator'] = $separator;
                 } elseif (isset($mapping->{'tree-path-source'})) {
                     if (!$this->isValidFieldForPathSource($meta, $field)) {
                         throw new InvalidMappingException("Tree PathSource field - [{$field}] type is not valid. It can be any of the integer variants, double, float or string in class - {$meta->name}");

+ 5 - 0
lib/Gedmo/Tree/Mapping/Driver/Yaml.php

@@ -125,7 +125,12 @@ class Yaml extends File implements Driver
                         if (!$this->isValidFieldForPath($meta, $field)) {
                             throw new InvalidMappingException("Tree Path field - [{$field}] type is not valid. It must be string or text in class - {$meta->name}");
                         }
+                        $separator = $fieldMapping['gedmo']['treePath']['separator'];
+                        if (strlen($separator) > 1) {
+                            throw new InvalidMappingException("Tree Path field - [{$field}] Separator {$separator} is invalid. It must be only one character long.");
+                        }
                         $config['path'] = $field;
+                        $config['path_separator'] = $separator;
                     } elseif (in_array('treePathSource', $fieldMapping['gedmo'])) {
                         if (!$this->isValidFieldForPathSource($meta, $field)) {
                             throw new InvalidMappingException("Tree PathSource field - [{$field}] type is not valid. It can be any of the integer variants, double, float or string in class - {$meta->name}");

+ 1 - 6
lib/Gedmo/Tree/Strategy/AbstractMaterializedPath.php

@@ -22,11 +22,6 @@ use Doctrine\Common\Persistence\ObjectManager;
 
 abstract class AbstractMaterializedPath implements Strategy
 {
-    /**
-     * Path separator
-     */
-    const PATH_SEPARATOR = ',';
-
     /**
      * TreeListener
      *
@@ -148,7 +143,7 @@ abstract class AbstractMaterializedPath implements Strategy
         $pathProp->setAccessible(true);
         $pathSourceProp = $meta->getReflectionProperty($config['path_source']);
         $pathSourceProp->setAccessible(true);
-        $path = $pathSourceProp->getValue($node).self::PATH_SEPARATOR;
+        $path = $pathSourceProp->getValue($node).$config['path_separator'];
 
         if ($parent) {
             $changeSet = $uow->isScheduledForUpdate($parent) ? $ea->getObjectChangeSet($uow, $parent) : false;

+ 1 - 1
tests/Gedmo/Tree/Fixture/Document/Category.php

@@ -24,7 +24,7 @@ class Category
 
     /**
      * @MONGO\Field(type="string")
-     * @Gedmo\TreePath
+     * @Gedmo\TreePath(separator="|")
      */
     private $path;