Explorar o código

[Form] Refactored casting logic from IntegerField to IntegerToLocalizedStringTransformer

Bernhard Schussek %!s(int64=14) %!d(string=hai) anos
pai
achega
6cc0a58edc

+ 7 - 9
src/Symfony/Component/Form/IntegerField.php

@@ -11,7 +11,7 @@
 
 namespace Symfony\Component\Form;
 
-use Symfony\Component\Form\ValueTransformer\NumberToLocalizedStringTransformer;
+use Symfony\Component\Form\ValueTransformer\IntegerToLocalizedStringTransformer;
 
 /*
  * This file is part of the Symfony package.
@@ -43,16 +43,14 @@ class IntegerField extends NumberField
         $this->addOption('precision', 0);
 
         // Integer cast rounds towards 0, so do the same when displaying fractions
-        $this->addOption('rounding-mode', NumberToLocalizedStringTransformer::ROUND_DOWN);
+        $this->addOption('rounding-mode', IntegerToLocalizedStringTransformer::ROUND_DOWN);
 
         parent::configure();
-    }
 
-    /**
-     * {@inheritDoc}
-     */
-    public function getData()
-    {
-        return (int)parent::getData();
+        $this->setValueTransformer(new IntegerToLocalizedStringTransformer(array(
+            'precision' => $this->getOption('precision'),
+            'grouping' => $this->getOption('grouping'),
+            'rounding-mode' => $this->getOption('rounding-mode'),
+        )));
     }
 }

+ 32 - 0
src/Symfony/Component/Form/ValueTransformer/IntegerToLocalizedStringTransformer.php

@@ -0,0 +1,32 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Form\ValueTransformer;
+
+use Symfony\Component\Form\Configurable;
+use Symfony\Component\Form\Exception\UnexpectedTypeException;
+
+/**
+ * Transforms between an integer and a localized number with grouping
+ * (each thousand) and comma separators.
+ *
+ * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
+ */
+class IntegerToLocalizedStringTransformer extends NumberToLocalizedStringTransformer
+{
+    /**
+     * {@inheritDoc}
+     */
+    public function reverseTransform($value)
+    {
+        return (int)parent::reverseTransform($value);
+    }
+}