[Form] Removed automatic distribution of the locale in the Form component. This leads to more problems than it solves.

This commit is contained in:
Bernhard Schussek 2011-01-31 15:15:12 +01:00
parent c468db5c5b
commit fdbc064f06
34 changed files with 81 additions and 234 deletions

View File

@ -67,10 +67,6 @@ class FrameworkExtension extends Extension
} }
} }
if (isset($config['i18n']) && $config['i18n']) {
FormContext::setLocale(\Locale::getDefault());
}
if (isset($config['ide'])) { if (isset($config['ide'])) {
switch ($config['ide']) { switch ($config['ide']) {
case 'textmate': case 'textmate':

View File

@ -23,7 +23,7 @@ class CountryField extends ChoiceField
{ {
protected function configure() protected function configure()
{ {
$this->addOption('choices', Locale::getDisplayCountries($this->locale)); $this->addOption('choices', Locale::getDisplayCountries(\Locale::getDefault()));
parent::configure(); parent::configure();
} }

View File

@ -104,7 +104,7 @@ class DateField extends HybridField
$this->addOption('user_timezone', 'UTC'); $this->addOption('user_timezone', 'UTC');
$this->formatter = new \IntlDateFormatter( $this->formatter = new \IntlDateFormatter(
$this->locale, \Locale::getDefault(),
self::$intlFormats[$this->getOption('format')], self::$intlFormats[$this->getOption('format')],
\IntlDateFormatter::NONE \IntlDateFormatter::NONE
); );

View File

@ -49,7 +49,6 @@ use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
class Field extends Configurable implements FieldInterface class Field extends Configurable implements FieldInterface
{ {
protected $taintedData = null; protected $taintedData = null;
protected $locale = null;
private $errors = array(); private $errors = array();
private $key = ''; private $key = '';
@ -74,7 +73,6 @@ class Field extends Configurable implements FieldInterface
$this->addOption('normalization_transformer'); $this->addOption('normalization_transformer');
$this->key = (string)$key; $this->key = (string)$key;
$this->locale = FormContext::getLocale();
parent::__construct($options); parent::__construct($options);
@ -412,20 +410,6 @@ class Field extends Configurable implements FieldInterface
return $this->errors; return $this->errors;
} }
/**
* Injects the locale into the given object, if set.
*
* The locale is injected only if the object implements Localizable.
*
* @param object $object
*/
protected function injectLocale($object)
{
if ($object instanceof Localizable) {
$object->setLocale($this->locale);
}
}
/** /**
* Sets the ValueTransformer. * Sets the ValueTransformer.
* *
@ -433,8 +417,6 @@ class Field extends Configurable implements FieldInterface
*/ */
protected function setNormalizationTransformer(ValueTransformerInterface $normalizationTransformer) protected function setNormalizationTransformer(ValueTransformerInterface $normalizationTransformer)
{ {
$this->injectLocale($normalizationTransformer);
$this->normalizationTransformer = $normalizationTransformer; $this->normalizationTransformer = $normalizationTransformer;
} }
@ -455,8 +437,6 @@ class Field extends Configurable implements FieldInterface
*/ */
protected function setValueTransformer(ValueTransformerInterface $valueTransformer) protected function setValueTransformer(ValueTransformerInterface $valueTransformer)
{ {
$this->injectLocale($valueTransformer);
$this->valueTransformer = $valueTransformer; $this->valueTransformer = $valueTransformer;
} }

View File

@ -65,26 +65,6 @@ class FormContext implements FormContextInterface
*/ */
protected $csrfFieldName = '_token'; protected $csrfFieldName = '_token';
/**
* Globally sets the locale for new forms and fields
*
* @param string $locale A valid locale, such as "en", "de_DE" etc.
*/
public static function setLocale($locale)
{
self::$locale = $locale;
}
/**
* Returns the locale used for new forms and fields
*
* @return string A valid locale, such as "en", "de_DE" etc.
*/
public static function getLocale()
{
return self::$locale;
}
/** /**
* @inheritDoc * @inheritDoc
*/ */

View File

@ -24,7 +24,6 @@ use Symfony\Component\Validator\ValidatorInterface;
* *
* <code> * <code>
* $form = $context * $form = $context
* ->locale('en_US')
* ->validationGroups('Address') * ->validationGroups('Address')
* ->getForm('author'); * ->getForm('author');
* </code> * </code>

View File

@ -25,7 +25,6 @@ use Symfony\Component\Validator\ValidatorInterface;
* *
* <code> * <code>
* $defaultContext = new FormContext(); * $defaultContext = new FormContext();
* $defaultContext->locale('en_US');
* $defaultContext->csrfProtection(true); * $defaultContext->csrfProtection(true);
* $factory = new FormFactory($defaultContext); * $factory = new FormFactory($defaultContext);
* *
@ -39,7 +38,6 @@ use Symfony\Component\Validator\ValidatorInterface;
* *
* <code> * <code>
* $form = $factory * $form = $factory
* ->locale('de_DE')
* ->csrfProtection(false) * ->csrfProtection(false)
* ->getForm('author'); * ->getForm('author');
* </code> * </code>

View File

@ -26,7 +26,7 @@ class LanguageField extends ChoiceField
*/ */
protected function configure() protected function configure()
{ {
$this->addOption('choices', Locale::getDisplayLanguages($this->locale)); $this->addOption('choices', Locale::getDisplayLanguages(\Locale::getDefault()));
parent::configure(); parent::configure();
} }

View File

@ -26,7 +26,7 @@ class LocaleField extends ChoiceField
*/ */
protected function configure() protected function configure()
{ {
$this->addOption('choices', Locale::getDisplayLocales($this->locale)); $this->addOption('choices', Locale::getDisplayLocales(\Locale::getDefault()));
parent::configure(); parent::configure();
} }

View File

@ -1,27 +0,0 @@
<?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;
/**
* Marks classes that you can inject a locale into.
*
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/
interface Localizable
{
/**
* Sets the locale of the class.
*
* @param string $locale
*/
function setLocale($locale);
}

View File

@ -70,12 +70,12 @@ class MoneyField extends NumberField
return '{{ widget }}'; return '{{ widget }}';
} }
if (!isset(self::$patterns[$this->locale])) { if (!isset(self::$patterns[\Locale::getDefault()])) {
self::$patterns[$this->locale] = array(); self::$patterns[\Locale::getDefault()] = array();
} }
if (!isset(self::$patterns[$this->locale][$this->getOption('currency')])) { if (!isset(self::$patterns[\Locale::getDefault()][$this->getOption('currency')])) {
$format = new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY); $format = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::CURRENCY);
$pattern = $format->formatCurrency('123', $this->getOption('currency')); $pattern = $format->formatCurrency('123', $this->getOption('currency'));
// the spacings between currency symbol and number are ignored, because // the spacings between currency symbol and number are ignored, because
@ -87,14 +87,14 @@ class MoneyField extends NumberField
preg_match('/^([^\s\xc2\xa0]*)[\s\xc2\xa0]*123[,.]00[\s\xc2\xa0]*([^\s\xc2\xa0]*)$/', $pattern, $matches); preg_match('/^([^\s\xc2\xa0]*)[\s\xc2\xa0]*123[,.]00[\s\xc2\xa0]*([^\s\xc2\xa0]*)$/', $pattern, $matches);
if (!empty($matches[1])) { if (!empty($matches[1])) {
self::$patterns[$this->locale] = $matches[1].' {{ widget }}'; self::$patterns[\Locale::getDefault()] = $matches[1].' {{ widget }}';
} else if (!empty($matches[2])) { } else if (!empty($matches[2])) {
self::$patterns[$this->locale] = '{{ widget }} '.$matches[2]; self::$patterns[\Locale::getDefault()] = '{{ widget }} '.$matches[2];
} else { } else {
self::$patterns[$this->locale] = '{{ widget }}'; self::$patterns[\Locale::getDefault()] = '{{ widget }}';
} }
} }
return self::$patterns[$this->locale]; return self::$patterns[\Locale::getDefault()];
} }
} }

View File

@ -11,7 +11,9 @@
namespace Symfony\Component\Form\ValueTransformer; namespace Symfony\Component\Form\ValueTransformer;
abstract class BaseDateTimeTransformer extends BaseValueTransformer use Symfony\Component\Form\Configurable;
abstract class BaseDateTimeTransformer extends Configurable implements ValueTransformerInterface
{ {
const NONE = 'none'; const NONE = 'none';
const FULL = 'full'; const FULL = 'full';

View File

@ -1,46 +0,0 @@
<?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;
/**
* Implements functionality shared by most value transformers
*
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/
abstract class BaseValueTransformer extends Configurable implements ValueTransformerInterface
{
/**
* The locale of this transformer as accepted by the class Locale
* @var string
*/
protected $locale;
/**
* {@inheritDoc}
*/
public function __construct(array $options = array())
{
$this->locale = class_exists('\Locale', false) ? \Locale::getDefault() : 'en';
parent::__construct($options);
}
/**
* {@inheritDoc}
*/
public function setLocale($locale)
{
$this->locale = $locale;
}
}

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Form\ValueTransformer; namespace Symfony\Component\Form\ValueTransformer;
use Symfony\Component\Form\Configurable;
use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\Form\Exception\UnexpectedTypeException;
/** /**
@ -19,7 +20,7 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com> * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
* @author Florian Eckerstorfer <florian@eckerstorfer.org> * @author Florian Eckerstorfer <florian@eckerstorfer.org>
*/ */
class BooleanToStringTransformer extends BaseValueTransformer class BooleanToStringTransformer extends Configurable implements ValueTransformerInterface
{ {
/** /**
* Transforms a Boolean into a string. * Transforms a Boolean into a string.

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Form\ValueTransformer; namespace Symfony\Component\Form\ValueTransformer;
use Symfony\Component\Form\Configurable;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
/** /**
@ -33,7 +34,7 @@ use Doctrine\Common\Collections\Collection;
* @author Benjamin Eberlei <kontakt@beberlei.de> * @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com> * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/ */
class CollectionToStringTransformer extends BaseValueTransformer class CollectionToStringTransformer extends Configurable implements ValueTransformerInterface
{ {
protected function configure() protected function configure()
{ {

View File

@ -21,7 +21,6 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
* * "input": The type of the normalized format ("time" or "timestamp"). Default: "datetime" * * "input": The type of the normalized format ("time" or "timestamp"). Default: "datetime"
* * "output": The type of the transformed format ("string" or "array"). Default: "string" * * "output": The type of the transformed format ("string" or "array"). Default: "string"
* * "format": The format of the time string ("short", "medium", "long" or "full"). Default: "short" * * "format": The format of the time string ("short", "medium", "long" or "full"). Default: "short"
* * "locale": The locale of the localized string. Default: Result of Locale::getDefault()
* *
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com> * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
* @author Florian Eckerstorfer <florian@eckerstorfer.org> * @author Florian Eckerstorfer <florian@eckerstorfer.org>

View File

@ -21,7 +21,6 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
* * "input": The type of the normalized format ("time" or "timestamp"). Default: "datetime" * * "input": The type of the normalized format ("time" or "timestamp"). Default: "datetime"
* * "output": The type of the transformed format ("string" or "array"). Default: "string" * * "output": The type of the transformed format ("string" or "array"). Default: "string"
* * "format": The format of the time string ("short", "medium", "long" or "full"). Default: "short" * * "format": The format of the time string ("short", "medium", "long" or "full"). Default: "short"
* * "locale": The locale of the localized string. Default: Result of Locale::getDefault()
* *
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com> * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
* @author Florian Eckerstorfer <florian@eckerstorfer.org> * @author Florian Eckerstorfer <florian@eckerstorfer.org>
@ -126,6 +125,6 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
$timeFormat = $this->getIntlFormatConstant($this->getOption('time_format')); $timeFormat = $this->getIntlFormatConstant($this->getOption('time_format'));
$timezone = $this->getOption('output_timezone'); $timezone = $this->getOption('output_timezone');
return new \IntlDateFormatter($this->locale, $dateFormat, $timeFormat, $timezone); return new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $timezone);
} }
} }

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Form\ValueTransformer; namespace Symfony\Component\Form\ValueTransformer;
use Symfony\Component\Form\Configurable;
use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\Form\Exception\UnexpectedTypeException;
/** /**
@ -19,7 +20,7 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com> * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
* @author Florian Eckerstorfer <florian@eckerstorfer.org> * @author Florian Eckerstorfer <florian@eckerstorfer.org>
*/ */
class DateTimeToStringTransformer extends BaseValueTransformer class DateTimeToStringTransformer extends Configurable implements ValueTransformerInterface
{ {
/** /**
* {@inheritDoc} * {@inheritDoc}

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Form\ValueTransformer; namespace Symfony\Component\Form\ValueTransformer;
use Symfony\Component\Form\Configurable;
use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\Form\Exception\UnexpectedTypeException;
/** /**
@ -19,7 +20,7 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com> * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
* @author Florian Eckerstorfer <florian@eckerstorfer.org> * @author Florian Eckerstorfer <florian@eckerstorfer.org>
*/ */
class DateTimeToTimestampTransformer extends BaseValueTransformer class DateTimeToTimestampTransformer extends Configurable implements ValueTransformerInterface
{ {
/** /**
* {@inheritDoc} * {@inheritDoc}

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Form\ValueTransformer; namespace Symfony\Component\Form\ValueTransformer;
use Symfony\Component\Form\Configurable;
use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\Form\Exception\UnexpectedTypeException;
/** /**
@ -20,7 +21,7 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com> * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
* @author Florian Eckerstorfer <florian@eckerstorfer.org> * @author Florian Eckerstorfer <florian@eckerstorfer.org>
*/ */
class NumberToLocalizedStringTransformer extends BaseValueTransformer class NumberToLocalizedStringTransformer extends Configurable implements ValueTransformerInterface
{ {
const ROUND_FLOOR = \NumberFormatter::ROUND_FLOOR; const ROUND_FLOOR = \NumberFormatter::ROUND_FLOOR;
const ROUND_DOWN = \NumberFormatter::ROUND_DOWN; const ROUND_DOWN = \NumberFormatter::ROUND_DOWN;
@ -100,7 +101,7 @@ class NumberToLocalizedStringTransformer extends BaseValueTransformer
*/ */
protected function getNumberFormatter() protected function getNumberFormatter()
{ {
$formatter = new \NumberFormatter($this->locale, \NumberFormatter::DECIMAL); $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
if ($this->getOption('precision') !== null) { if ($this->getOption('precision') !== null) {
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->getOption('precision')); $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->getOption('precision'));

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Form\ValueTransformer; namespace Symfony\Component\Form\ValueTransformer;
use Symfony\Component\Form\Configurable;
use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\Form\Exception\UnexpectedTypeException;
/** /**
@ -19,7 +20,7 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com> * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
* @author Florian Eckerstorfer <florian@eckerstorfer.org> * @author Florian Eckerstorfer <florian@eckerstorfer.org>
*/ */
class PercentToLocalizedStringTransformer extends BaseValueTransformer class PercentToLocalizedStringTransformer extends Configurable implements ValueTransformerInterface
{ {
const FRACTIONAL = 'fractional'; const FRACTIONAL = 'fractional';
const INTEGER = 'integer'; const INTEGER = 'integer';
@ -113,7 +114,7 @@ class PercentToLocalizedStringTransformer extends BaseValueTransformer
*/ */
protected function getNumberFormatter() protected function getNumberFormatter()
{ {
$formatter = new \NumberFormatter($this->locale, \NumberFormatter::DECIMAL); $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->getOption('precision')); $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->getOption('precision'));

View File

@ -52,12 +52,4 @@ class ReversedTransformer implements ValueTransformerInterface
{ {
return $this->reversedTransformer->transform($value); return $this->reversedTransformer->transform($value);
} }
/**
* {@inheritDoc}
*/
public function setLocale($locale)
{
$this->reversedTransformer->setLocale($locale);
}
} }

View File

@ -74,14 +74,4 @@ class ValueTransformerChain implements ValueTransformerInterface
return $value; return $value;
} }
/**
* {@inheritDoc}
*/
public function setLocale($locale)
{
foreach ($this->transformers as $transformer) {
$transformer->setLocale($locale);
}
}
} }

View File

@ -11,14 +11,12 @@
namespace Symfony\Component\Form\ValueTransformer; namespace Symfony\Component\Form\ValueTransformer;
use Symfony\Component\Form\Localizable;
/** /**
* Transforms a value between different representations. * Transforms a value between different representations.
* *
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com> * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/ */
interface ValueTransformerInterface extends Localizable interface ValueTransformerInterface
{ {
/** /**
* Transforms a value from the original representation to a transformed representation. * Transforms a value from the original representation to a transformed representation.

View File

@ -18,7 +18,7 @@ class CountryFieldTest extends \PHPUnit_Framework_TestCase
{ {
public function testCountriesAreSelectable() public function testCountriesAreSelectable()
{ {
FormContext::setLocale('de_AT'); \Locale::setDefault('de_AT');
$field = new CountryField('country'); $field = new CountryField('country');
$choices = $field->getOtherChoices(); $choices = $field->getOtherChoices();

View File

@ -20,7 +20,7 @@ class DateFieldTest extends DateTimeTestCase
{ {
protected function setUp() protected function setUp()
{ {
FormContext::setLocale('de_AT'); \Locale::setDefault('de_AT');
} }
public function testBind_fromInput_dateTime() public function testBind_fromInput_dateTime()

View File

@ -145,20 +145,6 @@ class FieldTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('news_article_title', $this->field->getId()); $this->assertEquals('news_article_title', $this->field->getId());
} }
public function testLocaleIsPassedToValueTransformer()
{
FormContext::setLocale('de_DE');
$transformer = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
$transformer->expects($this->exactly(1))
->method('setLocale')
->with($this->equalTo('de_DE'));
$field = new TestField('title', array(
'value_transformer' => $transformer,
));
}
public function testIsRequiredReturnsOwnValueIfNoParent() public function testIsRequiredReturnsOwnValueIfNoParent()
{ {
$this->field->setRequired(true); $this->field->setRequired(true);

View File

@ -18,7 +18,7 @@ class LanguageFieldTest extends \PHPUnit_Framework_TestCase
{ {
public function testCountriesAreSelectable() public function testCountriesAreSelectable()
{ {
FormContext::setLocale('de_AT'); \Locale::setDefault('de_AT');
$field = new LanguageField('language'); $field = new LanguageField('language');
$choices = $field->getOtherChoices(); $choices = $field->getOtherChoices();

View File

@ -18,7 +18,7 @@ class LocaleFieldTest extends \PHPUnit_Framework_TestCase
{ {
public function testLocalesAreSelectable() public function testLocalesAreSelectable()
{ {
FormContext::setLocale('de_AT'); \Locale::setDefault('de_AT');
$field = new LocaleField('language'); $field = new LocaleField('language');
$choices = $field->getOtherChoices(); $choices = $field->getOtherChoices();

View File

@ -25,6 +25,8 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
{ {
parent::setUp(); parent::setUp();
\Locale::setDefault('de_AT');
$this->dateTime = new \DateTime('2010-02-03 04:05:06 UTC'); $this->dateTime = new \DateTime('2010-02-03 04:05:06 UTC');
$this->dateTimeWithoutSeconds = new \DateTime('2010-02-03 04:05:00 UTC'); $this->dateTimeWithoutSeconds = new \DateTime('2010-02-03 04:05:00 UTC');
} }
@ -46,7 +48,6 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'date_format' => 'short', 'date_format' => 'short',
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals('03.02.10 04:05', $transformer->transform($this->dateTime)); $this->assertEquals('03.02.10 04:05', $transformer->transform($this->dateTime));
} }
@ -57,7 +58,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'date_format' => 'medium', 'date_format' => 'medium',
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals('03.02.2010 04:05', $transformer->transform($this->dateTime)); $this->assertEquals('03.02.2010 04:05', $transformer->transform($this->dateTime));
} }
@ -68,7 +69,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'date_format' => 'long', 'date_format' => 'long',
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals('03. Februar 2010 04:05', $transformer->transform($this->dateTime)); $this->assertEquals('03. Februar 2010 04:05', $transformer->transform($this->dateTime));
} }
@ -79,7 +80,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'date_format' => 'full', 'date_format' => 'full',
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals('Mittwoch, 03. Februar 2010 04:05', $transformer->transform($this->dateTime)); $this->assertEquals('Mittwoch, 03. Februar 2010 04:05', $transformer->transform($this->dateTime));
} }
@ -90,7 +91,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'time_format' => 'short', 'time_format' => 'short',
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals('03.02.2010 04:05', $transformer->transform($this->dateTime)); $this->assertEquals('03.02.2010 04:05', $transformer->transform($this->dateTime));
} }
@ -101,7 +102,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'time_format' => 'medium', 'time_format' => 'medium',
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals('03.02.2010 04:05:06', $transformer->transform($this->dateTime)); $this->assertEquals('03.02.2010 04:05:06', $transformer->transform($this->dateTime));
} }
@ -112,7 +113,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'time_format' => 'long', 'time_format' => 'long',
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals('03.02.2010 04:05:06 GMT+00:00', $transformer->transform($this->dateTime)); $this->assertEquals('03.02.2010 04:05:06 GMT+00:00', $transformer->transform($this->dateTime));
} }
@ -123,17 +124,19 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'time_format' => 'full', 'time_format' => 'full',
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals('03.02.2010 04:05:06 GMT+00:00', $transformer->transform($this->dateTime)); $this->assertEquals('03.02.2010 04:05:06 GMT+00:00', $transformer->transform($this->dateTime));
} }
public function testTransformToDifferentLocale() public function testTransformToDifferentLocale()
{ {
\Locale::setDefault('en_US');
$transformer = new DateTimeToLocalizedStringTransformer(array( $transformer = new DateTimeToLocalizedStringTransformer(array(
'input_timezone' => 'UTC', 'input_timezone' => 'UTC',
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
)); ));
$transformer->setLocale('en_US');
$this->assertEquals('Feb 3, 2010 4:05 AM', $transformer->transform($this->dateTime)); $this->assertEquals('Feb 3, 2010 4:05 AM', $transformer->transform($this->dateTime));
} }
@ -150,7 +153,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'input_timezone' => 'America/New_York', 'input_timezone' => 'America/New_York',
'output_timezone' => 'Asia/Hong_Kong', 'output_timezone' => 'Asia/Hong_Kong',
)); ));
$transformer->setLocale('de_AT');
$input = new \DateTime('2010-02-03 04:05:06 America/New_York'); $input = new \DateTime('2010-02-03 04:05:06 America/New_York');
@ -187,7 +190,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'date_format' => 'short', 'date_format' => 'short',
)); ));
$transformer->setLocale('de_AT');
$this->assertDateTimeEquals($this->dateTimeWithoutSeconds, $transformer->reverseTransform('03.02.10 04:05', null)); $this->assertDateTimeEquals($this->dateTimeWithoutSeconds, $transformer->reverseTransform('03.02.10 04:05', null));
} }
@ -198,7 +201,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'date_format' => 'medium', 'date_format' => 'medium',
)); ));
$transformer->setLocale('de_AT');
$this->assertDateTimeEquals($this->dateTimeWithoutSeconds, $transformer->reverseTransform('03.02.2010 04:05', null)); $this->assertDateTimeEquals($this->dateTimeWithoutSeconds, $transformer->reverseTransform('03.02.2010 04:05', null));
} }
@ -209,7 +212,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'date_format' => 'long', 'date_format' => 'long',
)); ));
$transformer->setLocale('de_AT');
$this->assertDateTimeEquals($this->dateTimeWithoutSeconds, $transformer->reverseTransform('03. Februar 2010 04:05', null)); $this->assertDateTimeEquals($this->dateTimeWithoutSeconds, $transformer->reverseTransform('03. Februar 2010 04:05', null));
} }
@ -220,7 +223,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'date_format' => 'full', 'date_format' => 'full',
)); ));
$transformer->setLocale('de_AT');
$this->assertDateTimeEquals($this->dateTimeWithoutSeconds, $transformer->reverseTransform('Mittwoch, 03. Februar 2010 04:05', null)); $this->assertDateTimeEquals($this->dateTimeWithoutSeconds, $transformer->reverseTransform('Mittwoch, 03. Februar 2010 04:05', null));
} }
@ -231,7 +234,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'time_format' => 'short', 'time_format' => 'short',
)); ));
$transformer->setLocale('de_AT');
$this->assertDateTimeEquals($this->dateTimeWithoutSeconds, $transformer->reverseTransform('03.02.2010 04:05', null)); $this->assertDateTimeEquals($this->dateTimeWithoutSeconds, $transformer->reverseTransform('03.02.2010 04:05', null));
} }
@ -242,7 +245,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'time_format' => 'medium', 'time_format' => 'medium',
)); ));
$transformer->setLocale('de_AT');
$this->assertDateTimeEquals($this->dateTime, $transformer->reverseTransform('03.02.2010 04:05:06', null)); $this->assertDateTimeEquals($this->dateTime, $transformer->reverseTransform('03.02.2010 04:05:06', null));
} }
@ -253,7 +256,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'time_format' => 'long', 'time_format' => 'long',
)); ));
$transformer->setLocale('de_AT');
$this->assertDateTimeEquals($this->dateTime, $transformer->reverseTransform('03.02.2010 04:05:06 GMT+00:00', null)); $this->assertDateTimeEquals($this->dateTime, $transformer->reverseTransform('03.02.2010 04:05:06 GMT+00:00', null));
} }
@ -264,17 +267,19 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
'time_format' => 'full', 'time_format' => 'full',
)); ));
$transformer->setLocale('de_AT');
$this->assertDateTimeEquals($this->dateTime, $transformer->reverseTransform('03.02.2010 04:05:06 GMT+00:00', null)); $this->assertDateTimeEquals($this->dateTime, $transformer->reverseTransform('03.02.2010 04:05:06 GMT+00:00', null));
} }
public function testReverseTransformFromDifferentLocale() public function testReverseTransformFromDifferentLocale()
{ {
\Locale::setDefault('en_US');
$transformer = new DateTimeToLocalizedStringTransformer(array( $transformer = new DateTimeToLocalizedStringTransformer(array(
'input_timezone' => 'UTC', 'input_timezone' => 'UTC',
'output_timezone' => 'UTC', 'output_timezone' => 'UTC',
)); ));
$transformer->setLocale('en_US');
$this->assertDateTimeEquals($this->dateTimeWithoutSeconds, $transformer->reverseTransform('Feb 3, 2010 04:05 AM', null)); $this->assertDateTimeEquals($this->dateTimeWithoutSeconds, $transformer->reverseTransform('Feb 3, 2010 04:05 AM', null));
} }
@ -284,7 +289,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
'input_timezone' => 'America/New_York', 'input_timezone' => 'America/New_York',
'output_timezone' => 'Asia/Hong_Kong', 'output_timezone' => 'Asia/Hong_Kong',
)); ));
$transformer->setLocale('de_AT');
$dateTime = new \DateTime('2010-02-03 04:05:00 Asia/Hong_Kong'); $dateTime = new \DateTime('2010-02-03 04:05:00 Asia/Hong_Kong');
$dateTime->setTimezone(new \DateTimeZone('America/New_York')); $dateTime->setTimezone(new \DateTimeZone('America/New_York'));

View File

@ -19,12 +19,18 @@ use Symfony\Tests\Component\Form\LocalizedTestCase;
class MoneyToLocalizedStringTransformerTest extends LocalizedTestCase class MoneyToLocalizedStringTransformerTest extends LocalizedTestCase
{ {
protected function setUp()
{
parent::setUp();
\Locale::setDefault('de_AT');
}
public function testTransform() public function testTransform()
{ {
$transformer = new MoneyToLocalizedStringTransformer(array( $transformer = new MoneyToLocalizedStringTransformer(array(
'divisor' => 100, 'divisor' => 100,
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals('1,23', $transformer->transform(123)); $this->assertEquals('1,23', $transformer->transform(123));
} }
@ -52,7 +58,6 @@ class MoneyToLocalizedStringTransformerTest extends LocalizedTestCase
$transformer = new MoneyToLocalizedStringTransformer(array( $transformer = new MoneyToLocalizedStringTransformer(array(
'divisor' => 100, 'divisor' => 100,
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals(123, $transformer->reverseTransform('1,23', null)); $this->assertEquals(123, $transformer->reverseTransform('1,23', null));
} }

View File

@ -18,10 +18,16 @@ use Symfony\Tests\Component\Form\LocalizedTestCase;
class NumberToLocalizedStringTransformerTest extends LocalizedTestCase class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
{ {
protected function setUp()
{
parent::setUp();
\Locale::setDefault('de_AT');
}
public function testTransform() public function testTransform()
{ {
$transformer = new NumberToLocalizedStringTransformer(); $transformer = new NumberToLocalizedStringTransformer();
$transformer->setLocale('de_AT');
$this->assertEquals('1', $transformer->transform(1)); $this->assertEquals('1', $transformer->transform(1));
$this->assertEquals('1,5', $transformer->transform(1.5)); $this->assertEquals('1,5', $transformer->transform(1.5));
@ -41,7 +47,6 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
$transformer = new NumberToLocalizedStringTransformer(array( $transformer = new NumberToLocalizedStringTransformer(array(
'grouping' => true, 'grouping' => true,
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals('1.234,5', $transformer->transform(1234.5)); $this->assertEquals('1.234,5', $transformer->transform(1234.5));
$this->assertEquals('12.345,912', $transformer->transform(12345.9123)); $this->assertEquals('12.345,912', $transformer->transform(12345.9123));
@ -52,7 +57,6 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
$transformer = new NumberToLocalizedStringTransformer(array( $transformer = new NumberToLocalizedStringTransformer(array(
'precision' => 2, 'precision' => 2,
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals('1234,50', $transformer->transform(1234.5)); $this->assertEquals('1234,50', $transformer->transform(1234.5));
$this->assertEquals('678,92', $transformer->transform(678.916)); $this->assertEquals('678,92', $transformer->transform(678.916));
@ -63,14 +67,12 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
$transformer = new NumberToLocalizedStringTransformer(array( $transformer = new NumberToLocalizedStringTransformer(array(
'rounding-mode' => NumberToLocalizedStringTransformer::ROUND_DOWN, 'rounding-mode' => NumberToLocalizedStringTransformer::ROUND_DOWN,
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals('1234,547', $transformer->transform(1234.547), '->transform() only applies rounding mode if precision set'); $this->assertEquals('1234,547', $transformer->transform(1234.547), '->transform() only applies rounding mode if precision set');
$transformer = new NumberToLocalizedStringTransformer(array( $transformer = new NumberToLocalizedStringTransformer(array(
'rounding-mode' => NumberToLocalizedStringTransformer::ROUND_DOWN, 'rounding-mode' => NumberToLocalizedStringTransformer::ROUND_DOWN,
'precision' => 2, 'precision' => 2,
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals('1234,54', $transformer->transform(1234.547), '->transform() rounding-mode works'); $this->assertEquals('1234,54', $transformer->transform(1234.547), '->transform() rounding-mode works');
} }
@ -78,7 +80,6 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
public function testReverseTransform() public function testReverseTransform()
{ {
$transformer = new NumberToLocalizedStringTransformer(); $transformer = new NumberToLocalizedStringTransformer();
$transformer->setLocale('de_AT');
$this->assertEquals(1, $transformer->reverseTransform('1', null)); $this->assertEquals(1, $transformer->reverseTransform('1', null));
$this->assertEquals(1.5, $transformer->reverseTransform('1,5', null)); $this->assertEquals(1.5, $transformer->reverseTransform('1,5', null));
@ -98,7 +99,6 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
$transformer = new NumberToLocalizedStringTransformer(array( $transformer = new NumberToLocalizedStringTransformer(array(
'grouping' => true, 'grouping' => true,
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals(1234.5, $transformer->reverseTransform('1.234,5', null)); $this->assertEquals(1234.5, $transformer->reverseTransform('1.234,5', null));
$this->assertEquals(12345.912, $transformer->reverseTransform('12.345,912', null)); $this->assertEquals(12345.912, $transformer->reverseTransform('12.345,912', null));

View File

@ -18,10 +18,16 @@ use Symfony\Tests\Component\Form\LocalizedTestCase;
class PercentToLocalizedStringTransformerTest extends LocalizedTestCase class PercentToLocalizedStringTransformerTest extends LocalizedTestCase
{ {
protected function setUp()
{
parent::setUp();
\Locale::setDefault('de_AT');
}
public function testTransform() public function testTransform()
{ {
$transformer = new PercentToLocalizedStringTransformer(); $transformer = new PercentToLocalizedStringTransformer();
$transformer->setLocale('de_AT');
$this->assertEquals('10', $transformer->transform(0.1)); $this->assertEquals('10', $transformer->transform(0.1));
$this->assertEquals('15', $transformer->transform(0.15)); $this->assertEquals('15', $transformer->transform(0.15));
@ -41,7 +47,6 @@ class PercentToLocalizedStringTransformerTest extends LocalizedTestCase
$transformer = new PercentToLocalizedStringTransformer(array( $transformer = new PercentToLocalizedStringTransformer(array(
'type' => 'integer', 'type' => 'integer',
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals('0', $transformer->transform(0.1)); $this->assertEquals('0', $transformer->transform(0.1));
$this->assertEquals('1', $transformer->transform(1)); $this->assertEquals('1', $transformer->transform(1));
@ -54,7 +59,6 @@ class PercentToLocalizedStringTransformerTest extends LocalizedTestCase
$transformer = new PercentToLocalizedStringTransformer(array( $transformer = new PercentToLocalizedStringTransformer(array(
'precision' => 2, 'precision' => 2,
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals('12,34', $transformer->transform(0.1234)); $this->assertEquals('12,34', $transformer->transform(0.1234));
} }
@ -62,7 +66,6 @@ class PercentToLocalizedStringTransformerTest extends LocalizedTestCase
public function testReverseTransform() public function testReverseTransform()
{ {
$transformer = new PercentToLocalizedStringTransformer(); $transformer = new PercentToLocalizedStringTransformer();
$transformer->setLocale('de_AT');
$this->assertEquals(0.1, $transformer->reverseTransform('10', null)); $this->assertEquals(0.1, $transformer->reverseTransform('10', null));
$this->assertEquals(0.15, $transformer->reverseTransform('15', null)); $this->assertEquals(0.15, $transformer->reverseTransform('15', null));
@ -82,7 +85,6 @@ class PercentToLocalizedStringTransformerTest extends LocalizedTestCase
$transformer = new PercentToLocalizedStringTransformer(array( $transformer = new PercentToLocalizedStringTransformer(array(
'type' => 'integer', 'type' => 'integer',
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals(10, $transformer->reverseTransform('10', null)); $this->assertEquals(10, $transformer->reverseTransform('10', null));
$this->assertEquals(15, $transformer->reverseTransform('15', null)); $this->assertEquals(15, $transformer->reverseTransform('15', null));
@ -95,7 +97,6 @@ class PercentToLocalizedStringTransformerTest extends LocalizedTestCase
$transformer = new PercentToLocalizedStringTransformer(array( $transformer = new PercentToLocalizedStringTransformer(array(
'precision' => 2, 'precision' => 2,
)); ));
$transformer->setLocale('de_AT');
$this->assertEquals(0.1234, $transformer->reverseTransform('12,34', null)); $this->assertEquals(0.1234, $transformer->reverseTransform('12,34', null));
} }

View File

@ -50,20 +50,4 @@ class ValueTransformerChainTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('baz', $chain->reverseTransform('foo', null)); $this->assertEquals('baz', $chain->reverseTransform('foo', null));
} }
public function testSetLocale()
{
$transformer1 = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
$transformer1->expects($this->once())
->method('setLocale')
->with($this->identicalTo('de_DE'));
$transformer2 = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
$transformer2->expects($this->once())
->method('setLocale')
->with($this->identicalTo('de_DE'));
$chain = new ValueTransformerChain(array($transformer1, $transformer2));
$chain->setLocale('de_DE');
}
} }