Przeglądaj źródła

cleaned Translator interface little bit and added missing docBlocks

everzet 13 lat temu
rodzic
commit
1b572f1033

+ 8 - 2
lib/Gedmo/Translator/Document/Translation.php

@@ -9,8 +9,14 @@ use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
 use Doctrine\ODM\MongoDB\Mapping\Annotations\String as MongoString;
 
 /**
-* @MappedSuperclass
-*/
+ * Document translation class.
+ *
+ * @author  Konstantin Kudryashov <ever.zet@gmail.com>
+ * @link    http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ *
+ * @MappedSuperclass
+ */
 abstract class Translation extends BaseTranslation
 {
     /**

+ 8 - 2
lib/Gedmo/Translator/Entity/Translation.php

@@ -10,8 +10,14 @@ use Doctrine\ORM\Mapping\Id;
 use Doctrine\ORM\Mapping\GeneratedValue;
 
 /**
-* @MappedSuperclass
-*/
+ * Entity translation class.
+ *
+ * @author  Konstantin Kudryashov <ever.zet@gmail.com>
+ * @link    http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ *
+ * @MappedSuperclass
+ */
 abstract class Translation extends BaseTranslation
 {
     /**

+ 7 - 0
lib/Gedmo/Translator/Translation.php

@@ -2,6 +2,13 @@
 
 namespace Gedmo\Translator;
 
+/**
+ * Base translation class.
+ *
+ * @author  Konstantin Kudryashov <ever.zet@gmail.com>
+ * @link    http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
 abstract class Translation implements TranslationInterface
 {
     protected $translatable;

+ 7 - 0
lib/Gedmo/Translator/TranslationInterface.php

@@ -2,6 +2,13 @@
 
 namespace Gedmo\Translator;
 
+/**
+ * Entity/Document translation interface.
+ *
+ * @author  Konstantin Kudryashov <ever.zet@gmail.com>
+ * @link    http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
 interface TranslationInterface
 {
     /**

+ 38 - 25
lib/Gedmo/Translator/TranslationProxy.php

@@ -4,6 +4,13 @@ namespace Gedmo\Translator;
 
 use Doctrine\Common\Collections\Collection;
 
+/**
+ * Proxy class for Entity/Document translations.
+ *
+ * @author  Konstantin Kudryashov <ever.zet@gmail.com>
+ * @link    http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
 class TranslationProxy
 {
     protected $locale;
@@ -69,6 +76,10 @@ class TranslationProxy
     public function __get($property)
     {
         if (in_array($property, $this->properties)) {
+            if (method_exists($this, $getter = 'get'.ucfirst($property))) {
+                return $this->$getter;
+            }
+
             return $this->getTranslatedValue($property);
         }
 
@@ -78,59 +89,61 @@ class TranslationProxy
     public function __set($property, $value)
     {
         if (in_array($property, $this->properties)) {
-            $this->setTranslatedValue($property, $value);
+            if (method_exists($this, $setter = 'set'.ucfirst($property))) {
+                return $this->$setter($value);
+            }
 
-            return $value;
+            return $this->setTranslatedValue($property, $value);
         }
 
         $this->translatable->property = $value;
     }
 
+    /**
+     * Returns translated value for specific property.
+     *
+     * @param   string  $property   property name
+     *
+     * @return  mixed
+     */
     public function getTranslatedValue($property)
     {
-        return $this->getOrCreateTranslationForProperty($property, $this->locale)->getValue();
-    }
-
-    public function setTranslatedValue($property, $value)
-    {
-        $this->getOrCreateTranslationForProperty($property, $this->locale)->setValue($value);
+        return $this->findOrCreateTranslationForProperty($property, $this->locale)->getValue();
     }
 
     /**
-     * Finds or creates new translation for specified property
+     * Sets translated value for specific property.
      *
-     * @param   string  $property   object property name
-     * @param   string  $locale     locale name
-     *
-     * @return  Translation
+     * @param   string  $property   property name
+     * @param   string  $value      value
      */
-    private function getOrCreateTranslationForProperty($property, $locale)
+    public function setTranslatedValue($property, $value)
     {
-        if (!($translation = $this->getTranslationForProperty($property, $locale))) {
-            $translation = new $this->class;
-            $translation->setTranslatable($this->translatable);
-            $translation->setProperty($property);
-            $translation->setLocale($locale);
-            $this->coll->add($translation);
-        }
-
-        return $translation;
+        $this->findOrCreateTranslationForProperty($property, $this->locale)->setValue($value);
     }
 
     /**
-     * Finds translation for specified property
+     * Finds existing or creates new translation for specified property
      *
      * @param   string  $property   object property name
      * @param   string  $locale     locale name
      *
      * @return  Translation
      */
-    private function getTranslationForProperty($property, $locale)
+    private function findOrCreateTranslationForProperty($property, $locale)
     {
         foreach ($this->coll as $translation) {
             if ($locale === $translation->getLocale() && $property === $translation->getProperty()) {
                 return $translation;
             }
         }
+
+        $translation = new $this->class;
+        $translation->setTranslatable($this->translatable);
+        $translation->setProperty($property);
+        $translation->setLocale($locale);
+        $this->coll->add($translation);
+
+        return $translation;
     }
 }