Skip to content

Commit d2073e5

Browse files
committed
[Validator] add number constraints
- @positive - @PositiveOrZero - @Negative - @NegativeOrZero
1 parent 8d277ce commit d2073e5

12 files changed

+636
-0
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ CHANGELOG
1515
* deprecated using the `Bic`, `Country`, `Currency`, `Language` and `Locale` constraints without `symfony/intl`
1616
* deprecated using the `Email` constraint without `egulias/email-validator`
1717
* deprecated using the `Expression` constraint without `symfony/expression-language`
18+
* deprecated using the `Bic` constraint without `symfony/intl`
19+
* added `Positive` constraint
20+
* added `PositiveOrZero` constraint
21+
* added `Negative` constraint
22+
* added `NegativeOrZero` constraint
1823

1924
4.1.0
2025
-----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
15+
16+
/**
17+
* @Annotation
18+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19+
*
20+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
21+
*/
22+
class Negative extends LessThan
23+
{
24+
public $message = 'This value should be negative.';
25+
26+
public function __construct($options = null)
27+
{
28+
if (null !== $options) {
29+
throw new ConstraintDefinitionException(sprintf('The "%s" constraint can not be configured via options.', \get_class($this)));
30+
}
31+
32+
$options = array('value' => 0);
33+
34+
parent::__construct($options);
35+
}
36+
37+
public function validatedBy()
38+
{
39+
return 'LessThanValidator';
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
15+
16+
/**
17+
* @Annotation
18+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19+
*
20+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
21+
*/
22+
class NegativeOrZero extends LessThanOrEqual
23+
{
24+
public $message = 'This value should be either negative or zero.';
25+
26+
public function __construct($options = null)
27+
{
28+
if (null !== $options) {
29+
throw new ConstraintDefinitionException(sprintf('The "%s" constraint can not be configured via options.', \get_class($this)));
30+
}
31+
32+
$options = array('value' => 0);
33+
34+
parent::__construct($options);
35+
}
36+
37+
public function validatedBy()
38+
{
39+
return 'LessThanOrEqualValidator';
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
15+
16+
/**
17+
* @Annotation
18+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19+
*
20+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
21+
*/
22+
class Positive extends GreaterThan
23+
{
24+
public $message = 'This value should be positive.';
25+
26+
public function __construct($options = null)
27+
{
28+
if (null !== $options) {
29+
throw new ConstraintDefinitionException(sprintf('The "%s" constraint can not be configured via options.', \get_class($this)));
30+
}
31+
32+
$options = array('value' => 0);
33+
34+
parent::__construct($options);
35+
}
36+
37+
public function validatedBy()
38+
{
39+
return 'GreaterThanValidator';
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
15+
16+
/**
17+
* @Annotation
18+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19+
*
20+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
21+
*/
22+
class PositiveOrZero extends GreaterThanOrEqual
23+
{
24+
public $message = 'This value should be either positive or zero.';
25+
26+
public function __construct($options = null)
27+
{
28+
if (null !== $options) {
29+
throw new ConstraintDefinitionException(sprintf('The "%s" constraint can not be configured via options.', \get_class($this)));
30+
}
31+
32+
$options = array('value' => 0);
33+
34+
parent::__construct($options);
35+
}
36+
37+
public function validatedBy()
38+
{
39+
return 'GreaterThanOrEqualValidator';
40+
}
41+
}

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

+16
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,22 @@
326326
<source>This value should be a multiple of {{ compared_value }}.</source>
327327
<target>Dieser Wert sollte ein Vielfaches von {{ compared_value }} sein.</target>
328328
</trans-unit>
329+
<trans-unit id="85">
330+
<source>This value should be positive.</source>
331+
<target>Dieser Wert sollte positiv sein.</target>
332+
</trans-unit>
333+
<trans-unit id="86">
334+
<source>This value should be either positive or zero.</source>
335+
<target>Dieser Wert sollte entweder positiv oder 0 sein.</target>
336+
</trans-unit>
337+
<trans-unit id="87">
338+
<source>This value should be negative.</source>
339+
<target>Dieser Wert sollte negativ sein.</target>
340+
</trans-unit>
341+
<trans-unit id="88">
342+
<source>This value should be either negative or zero.</source>
343+
<target>Dieser Wert sollte entweder negativ oder 0 sein.</target>
344+
</trans-unit>
329345
</body>
330346
</file>
331347
</xliff>

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

+16
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,22 @@
326326
<source>This value should be a multiple of {{ compared_value }}.</source>
327327
<target>This value should be a multiple of {{ compared_value }}.</target>
328328
</trans-unit>
329+
<trans-unit id="85">
330+
<source>This value should be positive.</source>
331+
<target>This value should be positive.</target>
332+
</trans-unit>
333+
<trans-unit id="86">
334+
<source>This value should be either positive or zero.</source>
335+
<target>This value should be either positive or zero.</target>
336+
</trans-unit>
337+
<trans-unit id="87">
338+
<source>This value should be negative.</source>
339+
<target>This value should be negative.</target>
340+
</trans-unit>
341+
<trans-unit id="88">
342+
<source>This value should be either negative or zero.</source>
343+
<target>This value should be either negative or zero.</target>
344+
</trans-unit>
329345
</body>
330346
</file>
331347
</xliff>

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

+16
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,22 @@
278278
<source>This value should not be identical to {{ compared_value_type }} {{ compared_value }}.</source>
279279
<target>Giá trị không được phép giống như {{ compared_value_type }} {{ compared_value }}.</target>
280280
</trans-unit>
281+
<trans-unit id="85">
282+
<source>This value should be positive.</source>
283+
<target>Giá trị này có thể thực hiện được.</target>
284+
</trans-unit>
285+
<trans-unit id="86">
286+
<source>This value should be either positive or zero.</source>
287+
<target>Giá trị này có thể thực hiện được hoặc bằng không.</target>
288+
</trans-unit>
289+
<trans-unit id="87">
290+
<source>This value should be negative.</source>
291+
<target>Giá trị này nên bị từ chối.</target>
292+
</trans-unit>
293+
<trans-unit id="88">
294+
<source>This value should be either negative or zero.</source>
295+
<target>Giá trị này nên bị từ chối hoặc bằng không.</target>
296+
</trans-unit>
281297
</body>
282298
</file>
283299
</xliff>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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\PositiveOrZero;
15+
16+
/**
17+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
18+
*/
19+
class GreaterThanOrEqualValidatorWithPositiveOrZeroConstraintTest extends GreaterThanOrEqualValidatorTest
20+
{
21+
protected function createConstraint(array $options = null)
22+
{
23+
return new PositiveOrZero();
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function provideValidComparisons()
30+
{
31+
return array(
32+
array(0, 0),
33+
array(1, 0),
34+
array(2, 0),
35+
array(2.5, 0),
36+
array('0', '0'),
37+
array('333', '0'),
38+
array(null, 0),
39+
);
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function provideInvalidComparisons()
46+
{
47+
return array(
48+
array(-1, '-1', 0, '0', 'integer'),
49+
array(-2, '-2', 0, '0', 'integer'),
50+
array(-2.5, '-2.5', 0, '0', 'integer'),
51+
);
52+
}
53+
54+
/**
55+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
56+
* @expectedExceptionMessage The "Symfony\Component\Validator\Constraints\PositiveOrZero" constraint can not be configured via options.
57+
*/
58+
public function testThrowsConstraintDefinitionExceptionIfConfiguredViaOptions()
59+
{
60+
new PositiveOrZero(array('value' => 1));
61+
}
62+
63+
/**
64+
* @dataProvider provideInvalidConstraintOptions
65+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
66+
* @expectedExceptionMessage requires either the "value" or "propertyPath" option to be set.
67+
*/
68+
public function testThrowsConstraintExceptionIfNoValueOrPropertyPath($options)
69+
{
70+
$this->markTestSkipped('Value property always set for PositiveOrZero constraint');
71+
}
72+
73+
/**
74+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
75+
* @expectedExceptionMessage requires only one of the "value" or "propertyPath" options to be set, not both.
76+
*/
77+
public function testThrowsConstraintExceptionIfBothValueAndPropertyPath()
78+
{
79+
$this->markTestSkipped('Value property is set for PositiveOrZero constraint automatically');
80+
}
81+
82+
public function testInvalidValuePath()
83+
{
84+
$this->markTestSkipped('PropertyPath property is not used in PositiveOrZero constraint');
85+
}
86+
87+
/**
88+
* @dataProvider provideValidComparisonsToPropertyPath
89+
*/
90+
public function testValidComparisonToPropertyPath($comparedValue)
91+
{
92+
$this->markTestSkipped('PropertyPath property is not used in PositiveOrZero constraint');
93+
}
94+
95+
/**
96+
* @dataProvider provideValidComparisonsToPropertyPath
97+
*/
98+
public function testValidComparisonToPropertyPathOnArray($comparedValue)
99+
{
100+
$this->markTestSkipped('PropertyPath property is not used in Positive constraint');
101+
}
102+
}

0 commit comments

Comments
 (0)