merged branch mvrhov/currency (PR #6566)
This PR was merged into the master branch.
Discussion
----------
[2.3] Added currency locale data, form type and validator
Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
License of the code: MIT
Documentation PR: symfony/symfony-docs#2105
Commits
-------
5609aae
Added currency form type and validator
This commit is contained in:
commit
221893629e
@ -149,6 +149,9 @@
|
||||
<service id="form.type.reset" class="Symfony\Component\Form\Extension\Core\Type\ResetType">
|
||||
<tag name="form.type" alias="reset" />
|
||||
</service>
|
||||
<service id="form.type.currency" class="Symfony\Component\Form\Extension\Core\Type\CurrencyType">
|
||||
<tag name="form.type" alias="currency" />
|
||||
</service>
|
||||
|
||||
<!-- FormTypeHttpFoundationExtension -->
|
||||
<service id="form.type_extension.form.http_foundation" class="Symfony\Component\Form\Extension\HttpFoundation\Type\FormTypeHttpFoundationExtension">
|
||||
|
@ -53,6 +53,7 @@ class CoreExtension extends AbstractExtension
|
||||
new Type\ButtonType(),
|
||||
new Type\SubmitType(),
|
||||
new Type\ResetType(),
|
||||
new Type\CurrencyType(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Form\Extension\Core\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Intl\Intl;
|
||||
use Symfony\Component\Locale\Locale;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||
|
||||
class CurrencyType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
{
|
||||
$resolver->setDefaults(array(
|
||||
'choices' => Intl::getCurrencyBundle()->getCurrencyNames(),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getParent()
|
||||
{
|
||||
return 'choice';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'currency';
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
|
||||
|
||||
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
|
||||
use Symfony\Component\Intl\Util\IntlTestHelper;
|
||||
|
||||
class CurrencyTypeTest extends TypeTestCase
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
IntlTestHelper::requireIntl($this);
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testCurrenciesAreSelectable()
|
||||
{
|
||||
$form = $this->factory->create('currency');
|
||||
$view = $form->createView();
|
||||
$choices = $view->vars['choices'];
|
||||
|
||||
$this->assertContains(new ChoiceView('EUR', 'EUR', 'Euro'), $choices, '', false, false);
|
||||
$this->assertContains(new ChoiceView('USD', 'USD', 'US Dollar'), $choices, '', false, false);
|
||||
$this->assertContains(new ChoiceView('SIT', 'SIT', 'Slovenian Tolar'), $choices, '', false, false);
|
||||
}
|
||||
|
||||
}
|
26
src/Symfony/Component/Validator/Constraints/Currency.php
Normal file
26
src/Symfony/Component/Validator/Constraints/Currency.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
*
|
||||
* @author Miha Vrhovnik <miha.vrhovnik@pagein.si>
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class Currency extends Constraint
|
||||
{
|
||||
public $message = 'This value is not a valid currency.';
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Intl\Intl;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
/**
|
||||
* Validates whether a value is a valid currency
|
||||
*
|
||||
* @author Miha Vrhovnik <miha.vrhovnik@pagein.si>
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class CurrencyValidator extends ConstraintValidator
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
if (null === $value || '' === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
|
||||
throw new UnexpectedTypeException($value, 'string');
|
||||
}
|
||||
|
||||
$value = (string) $value;
|
||||
$currencies = Intl::getCurrencyBundle()->getCurrencyNames();
|
||||
|
||||
if (!isset($currencies[$value])) {
|
||||
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
|
||||
}
|
||||
}
|
||||
}
|
@ -242,6 +242,10 @@
|
||||
<source>This value is not a valid ISSN.</source>
|
||||
<target>This value is not a valid ISSN.</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="64">
|
||||
<source>This value is not a valid currency.</source>
|
||||
<target>This value is not a valid currency.</target>
|
||||
</trans-unit>
|
||||
</body>
|
||||
</file>
|
||||
</xliff>
|
||||
|
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Tests\Constraints;
|
||||
|
||||
use Symfony\Component\Intl\Util\IntlTestHelper;
|
||||
use Symfony\Component\Validator\Constraints\Currency;
|
||||
use Symfony\Component\Validator\Constraints\CurrencyValidator;
|
||||
|
||||
class CurrencyValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $context;
|
||||
protected $validator;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
IntlTestHelper::requireIntl($this);
|
||||
|
||||
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
|
||||
$this->validator = new CurrencyValidator();
|
||||
$this->validator->initialize($this->context);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->context = null;
|
||||
$this->validator = null;
|
||||
}
|
||||
|
||||
public function testNullIsValid()
|
||||
{
|
||||
$this->context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$this->validator->validate(null, new Currency());
|
||||
}
|
||||
|
||||
public function testEmptyStringIsValid()
|
||||
{
|
||||
$this->context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$this->validator->validate('', new Currency());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
|
||||
*/
|
||||
public function testExpectsStringCompatibleType()
|
||||
{
|
||||
$this->validator->validate(new \stdClass(), new Currency());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getValidCurrencies
|
||||
*/
|
||||
public function testValidCurrencies($currency)
|
||||
{
|
||||
$this->context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$this->validator->validate($currency, new Currency());
|
||||
}
|
||||
|
||||
public function getValidCurrencies()
|
||||
{
|
||||
return array(
|
||||
array('EUR'),
|
||||
array('USD'),
|
||||
array('SIT'),
|
||||
array('AUD'),
|
||||
array('CAD'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalidCurrencies
|
||||
*/
|
||||
public function testInvalidCurrencies($currency)
|
||||
{
|
||||
$constraint = new Currency(array(
|
||||
'message' => 'myMessage'
|
||||
));
|
||||
|
||||
$this->context->expects($this->once())
|
||||
->method('addViolation')
|
||||
->with('myMessage', array(
|
||||
'{{ value }}' => $currency,
|
||||
));
|
||||
|
||||
$this->validator->validate($currency, $constraint);
|
||||
}
|
||||
|
||||
public function getInvalidCurrencies()
|
||||
{
|
||||
return array(
|
||||
array('EN'),
|
||||
array('foobar'),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user