Kaynağa Gözat

Implemented fallback as option of translatable annotation

Dmitry Pikhno 13 yıl önce
ebeveyn
işleme
df8b6965a2

+ 0 - 1
lib/Gedmo/Mapping/Annotation/All.php

@@ -19,7 +19,6 @@ include __DIR__.'/SortableGroup.php';
 include __DIR__.'/SortablePosition.php';
 include __DIR__.'/Timestampable.php';
 include __DIR__.'/Translatable.php';
-include __DIR__.'/NoFallback.php';
 include __DIR__.'/TranslationEntity.php';
 include __DIR__.'/Tree.php';
 include __DIR__.'/TreeClosure.php';

+ 0 - 18
lib/Gedmo/Mapping/Annotation/NoFallback.php

@@ -1,18 +0,0 @@
-<?php
-
-namespace Gedmo\Mapping\Annotation;
-
-use Doctrine\Common\Annotations\Annotation;
-
-/**
- * NoFallback annotation for Timestampable behavioral extension
- *
- * @Annotation
- * @Target("PROPERTY")
- *
- * @author Dmitry Pikhno
- */
-final class NoFallback extends Annotation
-{
-
-}

+ 2 - 1
lib/Gedmo/Mapping/Annotation/Translatable.php

@@ -18,6 +18,7 @@ use Doctrine\Common\Annotations\Annotation;
  */
 final class Translatable extends Annotation
 {
-
+    /** @var boolean */
+    public $fallback = true;
 }
 

+ 1 - 1
lib/Gedmo/Translatable/Mapping/Driver/Annotation.php

@@ -102,7 +102,7 @@ class Annotation implements AnnotationDriverInterface
                 }
                 // fields cannot be overrided and throws mapping exception
                 $config['fields'][] = $field;
-                if ($this->reader->getPropertyAnnotation($property, self::NOFALLBACK)) {
+                if (false === $translatable->fallback) {
                     $config['nofallback'][] = $field;
                 }
             }

+ 5 - 1
lib/Gedmo/Translatable/Mapping/Driver/Xml.php

@@ -63,7 +63,11 @@ class Xml extends BaseXml
                 $field = $this->_getAttribute($mappingDoctrine, 'name');
                 if (isset($mapping->translatable)) {
                     $config['fields'][] = $field;
-                    if (isset($mapping->nofallback)) {
+                    /**
+                     * @var \SimpleXmlElement $data
+                     */
+                    $data = $mapping->translatable;
+                    if($this->_isAttributeSet($data, 'fallback') && false === $this->_getAttribute($data, 'fallback')) {
                         $config['nofallback'][] = $field;
                     }
                 }

+ 2 - 1
lib/Gedmo/Translatable/Mapping/Driver/Yaml.php

@@ -54,7 +54,8 @@ class Yaml extends File implements Driver
                     if (in_array('translatable', $fieldMapping['gedmo'])) {
                         // fields cannot be overrided and throws mapping exception
                         $config['fields'][] = $field;
-                        if (in_array('nofallback', $fieldMapping['gedmo'])) {
+                        $mappingProperty = $fieldMapping['gedmo']['translatable'];
+                        if (isset($mappingProperty['fallback']) && false === $mappingProperty['fallback']) {
                             $config['nofallback'][] = $field;
                         }
                     }

+ 5 - 2
schemas/orm/doctrine-extensions-mapping-2-1.xsd

@@ -67,8 +67,7 @@ people to push their own additional attributes/elements into the same field elem
   <!-- field -->
   <xs:element name="sluggable" type="gedmo:sluggable"/>
   <xs:element name="slug" type="gedmo:slug"/>
-  <xs:element name="translatable" type="gedmo:emptyType"/>
-  <xs:element name="nofallback" type="gedmo:emptyType"/>
+  <xs:element name="translatable" type="gedmo:translatable"/>
   <xs:element name="timestampable" type="gedmo:timestampable"/>
   <xs:element name="versioned" type="gedmo:emptyType"/>
   <xs:element name="tree-left" type="gedmo:emptyType"/>
@@ -120,6 +119,10 @@ people to push their own additional attributes/elements into the same field elem
     <xs:attribute name="value" type="xs:string" use="optional" />
   </xs:complexType>
 
+  <xs:complexType name="translatable">
+    <xs:attribute name="fallback" type="xs:boolean" use="optional" />
+  </xs:complexType>
+
   <xs:complexType name="emptyType">
   </xs:complexType>
 

+ 5 - 2
schemas/orm/doctrine-extensions-mapping-2-2.xsd

@@ -29,8 +29,7 @@ people to push their own additional attributes/elements into the same field elem
   <xs:element name="soft-deleteable" type="gedmo:soft-deleteable"/>
   <!-- field -->
   <xs:element name="slug" type="gedmo:slug"/>
-  <xs:element name="translatable" type="gedmo:emptyType"/>
-  <xs:element name="nofallback" type="gedmo:emptyType"/>
+  <xs:element name="translatable" type="gedmo:translatable"/>
   <xs:element name="timestampable" type="gedmo:timestampable"/>
   <xs:element name="versioned" type="gedmo:emptyType"/>
   <xs:element name="tree-left" type="gedmo:emptyType"/>
@@ -100,6 +99,10 @@ people to push their own additional attributes/elements into the same field elem
     <xs:attribute name="value" type="xs:string" use="optional" />
   </xs:complexType>
 
+  <xs:complexType name="translatable">
+    <xs:attribute name="fallback" type="xs:boolean" use="optional" />
+  </xs:complexType>
+
   <xs:complexType name="emptyType">
   </xs:complexType>
 

+ 1 - 2
tests/Gedmo/Translatable/Fixture/Article.php

@@ -27,8 +27,7 @@ class Article implements Translatable
     private $content;
 
     /**
-     * @Gedmo\Translatable
-     * @Gedmo\NoFallback
+     * @Gedmo\Translatable(fallback=false)
      * @ORM\Column(name="views", type="integer", nullable=true)
      */
     private $views;

+ 1 - 1
tests/Gedmo/Translatable/TranslatableTest.php

@@ -250,7 +250,7 @@ class TranslatableTest extends BaseTestCaseORM
     /**
      * @test
      */
-    function shouldRespectNoFallbackAnnotation()
+    function shouldRespectFallbackOption()
     {
         $article = new Article;
         $article->setTitle('Euro2012');