Pārlūkot izejas kodu

[tree][closure] added yaml mapping support for closure tree

Gediminas Morkevicius 14 gadi atpakaļ
vecāks
revīzija
b770c8fb97
1 mainītis faili ar 40 papildinājumiem un 1 dzēšanām
  1. 40 1
      lib/Gedmo/Tree/Mapping/Driver/Yaml.php

+ 40 - 1
lib/Gedmo/Tree/Mapping/Driver/Yaml.php

@@ -44,7 +44,8 @@ class Yaml extends File implements Driver
      * @var array
      * @var array
      */
      */
     private $strategies = array(
     private $strategies = array(
-        'nested'
+        'nested',
+        'closure'
     );
     );
     
     
     /**
     /**
@@ -76,6 +77,13 @@ class Yaml extends File implements Driver
                 }
                 }
                 $config['strategy'] = $strategy;
                 $config['strategy'] = $strategy;
             }
             }
+            if (isset($classMapping['tree']['closure'])) {
+                $class = $classMapping['tree']['closure'];
+                if (!class_exists($class)) {
+                    throw new InvalidMappingException("Tree closure class: {$class} does not exist.");
+                }
+                $config['closure'] = $class;
+            }
         }
         }
         if (isset($mapping['fields'])) {
         if (isset($mapping['fields'])) {
             foreach ($mapping['fields'] as $field => $fieldMapping) {
             foreach ($mapping['fields'] as $field => $fieldMapping) {
@@ -100,6 +108,11 @@ class Yaml extends File implements Driver
                             throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
                             throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
                         }
                         }
                         $config['root'] = $field;
                         $config['root'] = $field;
+                    } elseif (in_array('treeChildCount', $fieldMapping['gedmo'])) {
+                        if (!$this->isValidField($meta, $field)) {
+                            throw new InvalidMappingException("Tree child count field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
+                        }
+                        $config['childCount'] = $field;
                     }
                     }
                 }
                 }
             }
             }
@@ -163,4 +176,30 @@ class Yaml extends File implements Driver
             throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
             throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
         }
         }
     }
     }
+    
+	/**
+     * Validates metadata for closure type tree
+     * 
+     * @param ClassMetadata $meta
+     * @param array $config
+     * @throws InvalidMappingException
+     * @return void
+     */
+    private function validateClosureTreeMetadata(ClassMetadata $meta, array $config)
+    {
+        if (is_array($meta->identifier) && count($meta->identifier) > 1) {
+            throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
+        }
+        
+        $missingFields = array();
+        if (!isset($config['parent'])) {
+            $missingFields[] = 'ancestor';
+        }
+        if (!isset($config['closure'])) {
+            $missingFields[] = 'closure class';
+        }
+        if ($missingFields) {
+            throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
+        }
+    }
 }
 }