Skip to content

[Validator] New DivisibleBy constraint for testing divisibility #28069

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Symfony/Component/Validator/Constraints/DivisibleBy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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;

/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Colin O'Dell <colinodell@gmail.com>
*/
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 }}.';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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;

/**
* Validates that values are a multiple of the given number.
*
* @author Colin O'Dell <colinodell@gmail.com>
*/
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@
<source>This is not a valid UUID.</source>
<target>Dies ist keine gültige UUID.</target>
</trans-unit>
<trans-unit id="84">
<source>This value should be a multiple of {{ compared_value }}.</source>
<target>Dieser Wert sollte ein Vielfaches von {{ compared_value }} sein.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@
<source>This is not a valid UUID.</source>
<target>This is not a valid UUID.</target>
</trans-unit>
<trans-unit id="84">
<source>This value should be a multiple of {{ compared_value }}.</source>
<target>This value should be a multiple of {{ compared_value }}.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@
<source>This is not a valid UUID.</source>
<target>Este valor no es un UUID válido.</target>
</trans-unit>
<trans-unit id="84">
<source>This value should be a multiple of {{ compared_value }}.</source>
<target>Este valor debería ser un múltiplo de {{ compared_value }}.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@
<source>This is not a valid UUID.</source>
<target>Ceci n'est pas un UUID valide.</target>
</trans-unit>
<trans-unit id="84">
<source>This value should be a multiple of {{ compared_value }}.</source>
<target>Cette valeur doit être un multiple de {{ compared_value }}.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@
<source>This is not a valid UUID.</source>
<target>Deze waarde is geen geldige UUID waarde.</target>
</trans-unit>
<trans-unit id="84">
<source>This value should be a multiple of {{ compared_value }}.</source>
<target>Deze waarde moet een veelvoud zijn van {{ compared_value }}.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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\Validator\Constraints\DivisibleBy;
use Symfony\Component\Validator\Constraints\DivisibleByValidator;

/**
* @author Colin O'Dell <colinodell@gmail.com>
*/
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'),
);
}
}