From efcfb8b22d533dcc3a4d18dacbd9776d3c6d2348 Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Thu, 26 Jul 2018 14:57:13 -0400 Subject: [PATCH] [Validator] New `DivisibleBy` constraint for testing divisibility --- .../Validator/Constraints/DivisibleBy.php | 29 +++++++ .../Constraints/DivisibleByValidator.php | 36 +++++++++ .../Resources/translations/validators.de.xlf | 4 + .../Resources/translations/validators.en.xlf | 4 + .../Resources/translations/validators.es.xlf | 4 + .../Resources/translations/validators.fr.xlf | 4 + .../Resources/translations/validators.nl.xlf | 4 + .../Constraints/DivisibleByValidatorTest.php | 75 +++++++++++++++++++ 8 files changed, 160 insertions(+) create mode 100644 src/Symfony/Component/Validator/Constraints/DivisibleBy.php create mode 100644 src/Symfony/Component/Validator/Constraints/DivisibleByValidator.php create mode 100644 src/Symfony/Component/Validator/Tests/Constraints/DivisibleByValidatorTest.php diff --git a/src/Symfony/Component/Validator/Constraints/DivisibleBy.php b/src/Symfony/Component/Validator/Constraints/DivisibleBy.php new file mode 100644 index 0000000000..c438f273c0 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/DivisibleBy.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * @Annotation + * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) + * + * @author Colin O'Dell + */ +class DivisibleBy extends AbstractComparison +{ + const NOT_DIVISIBLE_BY = '6d99d6c3-1464-4ccf-bdc7-14d083cf455c'; + + protected static $errorNames = array( + self::NOT_DIVISIBLE_BY => 'NOT_DIVISIBLE_BY', + ); + + public $message = 'This value should be a multiple of {{ compared_value }}.'; +} diff --git a/src/Symfony/Component/Validator/Constraints/DivisibleByValidator.php b/src/Symfony/Component/Validator/Constraints/DivisibleByValidator.php new file mode 100644 index 0000000000..c9455d71a1 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/DivisibleByValidator.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +/** + * Validates that values are a multiple of the given number. + * + * @author Colin O'Dell + */ +class DivisibleByValidator extends AbstractComparisonValidator +{ + /** + * {@inheritdoc} + */ + protected function compareValues($value1, $value2) + { + return (float) 0 === fmod($value1, $value2); + } + + /** + * {@inheritdoc} + */ + protected function getErrorCode() + { + return DivisibleBy::NOT_DIVISIBLE_BY; + } +} diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.de.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.de.xlf index 3e44e1e284..454d733fc6 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.de.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.de.xlf @@ -322,6 +322,10 @@ This is not a valid UUID. Dies ist keine gültige UUID. + + This value should be a multiple of {{ compared_value }}. + Dieser Wert sollte ein Vielfaches von {{ compared_value }} sein. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf index 3d173846a5..4bb2760b41 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf @@ -322,6 +322,10 @@ This is not a valid UUID. This is not a valid UUID. + + This value should be a multiple of {{ compared_value }}. + This value should be a multiple of {{ compared_value }}. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.es.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.es.xlf index 25d5b8a5d3..18eb8f4ca2 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.es.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.es.xlf @@ -322,6 +322,10 @@ This is not a valid UUID. Este valor no es un UUID válido. + + This value should be a multiple of {{ compared_value }}. + Este valor debería ser un múltiplo de {{ compared_value }}. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf index 382acb975c..c7ee2795b5 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf @@ -322,6 +322,10 @@ This is not a valid UUID. Ceci n'est pas un UUID valide. + + This value should be a multiple of {{ compared_value }}. + Cette valeur doit être un multiple de {{ compared_value }}. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf index 413a97eb17..687bcee042 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf @@ -318,6 +318,10 @@ This is not a valid UUID. Deze waarde is geen geldige UUID waarde. + + This value should be a multiple of {{ compared_value }}. + Deze waarde moet een veelvoud zijn van {{ compared_value }}. + diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DivisibleByValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DivisibleByValidatorTest.php new file mode 100644 index 0000000000..b4812ca8d5 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/DivisibleByValidatorTest.php @@ -0,0 +1,75 @@ + + * + * 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\Validator\Constraints\DivisibleBy; +use Symfony\Component\Validator\Constraints\DivisibleByValidator; + +/** + * @author Colin O'Dell + */ +class DivisibleByValidatorTest extends AbstractComparisonValidatorTestCase +{ + protected function createValidator() + { + return new DivisibleByValidator(); + } + + protected function createConstraint(array $options = null) + { + return new DivisibleBy($options); + } + + protected function getErrorCode() + { + return DivisibleBy::NOT_DIVISIBLE_BY; + } + + /** + * {@inheritdoc} + */ + public function provideValidComparisons() + { + return array( + array(-7, 1), + array(0, 3.1415), + array(42, 42), + array(42, 21), + array(3.25, 0.25), + array('100', '10'), + ); + } + + /** + * {@inheritdoc} + */ + public function provideValidComparisonsToPropertyPath() + { + return array( + array(25), + ); + } + + /** + * {@inheritdoc} + */ + public function provideInvalidComparisons() + { + return array( + array(1, '1', 2, '2', 'integer'), + array(10, '10', 3, '3', 'integer'), + array(10, '10', 0, '0', 'integer'), + array(42, '42', INF, 'INF', 'double'), + array('22', '"22"', '10', '"10"', 'string'), + ); + } +}