-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[2.3] Added currency locale data, form type and validator #6566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/Symfony/Component/Form/Extension/Core/Type/CurrencyType.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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'; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Symfony/Component/Form/Tests/Extension/Core/Type/CurrencyTypeTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* | ||
* @api | ||
*/ | ||
class Currency extends Constraint | ||
{ | ||
public $message = 'This value is not a valid currency.'; | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Symfony/Component/Validator/Constraints/CurrencyValidator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* | ||
* @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)); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/Symfony/Component/Validator/Tests/Constraints/CurrencyValidatorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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'), | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing doc block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bschussek All form types are without it. do you still want me to add it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bad examples are still bad examples :)