8000 Implement validation constraint for testing divisibility · symfony/symfony@36e369e · GitHub
[go: up one dir, main page]

Skip to content

Commit 36e369e

Browse files
committed
Implement validation constraint for testing divisibility
This constraint will check whether one number is a multiple of (aka divisible by) some other number.
1 parent a31f4aa commit 36e369e

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-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 MultipleOf extends AbstractComparison
21+
{
22+
const NOT_MULTIPLE_OF = '6d99d6c3-1464-4ccf-bdc7-14d083cf455c';
23+
24+
protected static $errorNames = array(
25+
self::NOT_MULTIPLE_OF => 'NOT_MULTIPLE_OF',
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 MultipleOfValidator extends AbstractComparisonValidator
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
protected function compareValues($value1, $value2)
25+
{
26+
return 0 == fmod($value1, $value2);
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
protected function getErrorCode()
33+
{
34+
return MultipleOf::NOT_MULTIPLE_OF;
35+
}
36+
}
Lines changed: 75 additions & 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\MultipleOf;
15+
use Symfony\Component\Validator\Constraints\MultipleOfValidator;
16+
17+
/**
18+
* @author Colin O'Dell <colinodell@gmail.com>
19+
*/
20+
class MultipleOfValidatorTest extends AbstractComparisonValidatorTestCase
21+
{
22+
protected function createValidator()
23+
{
24+
return new MultipleOfValidator();
25+
}
26+
27+
protected function createConstraint(array $options = null)
28+
{
29+
return new MultipleOf($options);
30+
}
31+
32+
protected function getErrorCode()
33+
{
34+
return MultipleOf::NOT_MULTIPLE_OF;
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