8000 [Validator] New `DivisibleBy` constraint for testing divisibility · symfony/symfony@efcfb8b · GitHub
[go: up one dir, main page]

Skip to content

Commit efcfb8b

Browse files
colinodellnicolas-grekas
authored andcommitted
[Validator] New DivisibleBy constraint for testing divisibility
1 parent a31f4aa commit efcfb8b

File tree

8 files changed

+160
-0
lines changed

8 files changed

+160
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
/**
15+
* @Annotation
16+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
17+
*
18+
* @author Colin O'Dell <colinodell@gmail.com>
19+
*/
20+
class DivisibleBy extends AbstractComparison
21+
{
22+
const NOT_DIVISIBLE_BY = '6d99d6c3-1464-4ccf-bdc7-14d083cf455c';
23+
24+
protected static $errorNames = array(
25+
self::NOT_DIVISIBLE_BY => 'NOT_DIVISIBLE_BY',
26+
);
27+
28+
public $message = 'This value should be a multiple of {{ compared_value }}.';
29+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
/**
15+
* Validates that values are a multiple of the given number.
16+
*
17+
* @author Colin O'Dell <colinodell@gmail.com>
18+
*/
19+
class DivisibleByValidator extends AbstractComparisonValidator
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
protected function compareValues($value1, $value2)
25+
{
26+
return (float) 0 === fmod($value1, $value2);
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
protected function getErrorCode()
33+
{
34+
return DivisibleBy::NOT_DIVISIBLE_BY;
35+
}
36+
}

src/Symfony/Component/Validator/Resources/translations/validators.de.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@
322322
<source>This is not a valid UUID.</source>
323323
<target>Dies ist keine gültige UUID.</target>
324324
</trans-unit>
325+
<trans-unit id="84">
326+
<source>This value should be a multiple of {{ compared_value }}.</source>
327+
<target>Dieser Wert sollte ein Vielfaches von {{ compared_value }} sein.</target>
328+
</trans-unit>
325329
</body>
326330
</file>
327331
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.en.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@
322322
<source>This is not a valid UUID.</source>
323323
<target>This is not a valid UUID.</target>
324324
</trans-unit>
325+
<trans-unit id="84">
326+
<source>This value should be a multiple of {{ compared_value }}.</source>
327+
<target>This value should be a multiple of {{ compared_value }}.</target>
328+
</trans-unit>
325329
</body>
326330
</file>
327331
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.es.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@
322322
<source>This is not a valid UUID.</source>
323323
<target>Este valor no es un UUID válido.</target>
324324
</trans-unit>
325+
<trans-unit id="84">
326+
<source>This value should be a multiple of {{ compared_value }}.</source>
327+
<target>Este valor debería ser un múltiplo de {{ compared_value }}.</target>
328+
</trans-unit>
325329
</body>
326330
</file>
327331
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@
322322
<source>This is not a valid UUID.</source>
323323
<target>Ceci n'est pas un UUID valide.</target>
324324
</trans-unit>
325+
<trans-unit id="84">
326+
<source>This value should be a multiple of {{ compared_value }}.</source>
327+
<target>Cette valeur doit être un multiple de {{ compared_value }}.</target>
328+
</trans-unit>
325329
</body>
326330
</file>
327331
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.nl.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@
318318
<source>This is not a valid UUID.</source>
319319
<target>Deze waarde is geen geldige UUID waarde.</target>
320320
</trans-unit>
321+
<trans-unit id="84">
322+
<source>This value should be a multiple of {{ compared_value }}.</source>
323+
<target>Deze waarde moet een veelvoud zijn van {{ compared_value }}.</target>
324+
</trans-unit>
321325
</body>
322326
</file>
323327
</xliff>
Lines changed: 75 ad 10000 ditions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints;
13+
14+
use Symfony\Component\Validator\Constraints\DivisibleBy;
15+
use Symfony\Component\Validator\Constraints\DivisibleByValidator;
16+
17+
/**
18+
* @author Colin O'Dell <colinodell@gmail.com>
19+
*/
20+
class DivisibleByValidatorTest extends AbstractComparisonValidatorTestCase
21+
{
22+
protected function createValidator()
23+
{
24+
return new DivisibleByValidator();
25+
}
26+
27+
protected function createConstraint(array $options = null)
28+
{
29+
return new DivisibleBy($options);
30+
}
31+
32+
protected function getErrorCode()
33+
{
34+
return DivisibleBy::NOT_DIVISIBLE_BY;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function provideValidComparisons()
41+
{
42+
return array(
43+
array(-7, 1),
44+
array(0, 3.1415),
45+
array(42, 42),
46+
array(42, 21),
47+
array(3.25, 0.25),
48+
array('100', '10'),
49+
);
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function provideValidComparisonsToPropertyPath()
56+
{
57+
return array(
58+
array(25),
59+
);
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function provideInvalidComparisons()
66+
{
67+
return array(
68+
array(1, '1', 2, '2', 'integer'),
69+
array(10, '10', 3, '3', 'integer'),
70+
array(10, '10', 0, '0', 'integer'),
71+
array(42, '42', INF, 'INF', 'double'),
72+
array('22', '"22"', '10', '"10"', 'string'),
73+
);
74+
}
75+
}

0 commit comments

Comments
 (0)
0