8000 [Validator] add number constraints · symfony/symfony@77ba93a · GitHub
[go: up one dir, main page]

Skip to content

Commit 77ba93a

Browse files
committed
[Validator] add number constraints
- @positive - @PositiveOrZero - @Negative - @NegativeOrZero
1 parent 15aa25a commit 77ba93a

16 files changed

+778
-0
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ CHANGELOG
1010
* made `ValidatorBuilder` final
1111
* marked `format` the default option in `DateTime` constraint
1212
* deprecated validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`.
13+
* added `Positive` constraint
14+
* added `PositiveOrZero` constraint
15+
* added `Negative` constraint
16+
* added `NegativeOrZero` constraint
1317

1418
4.1.0
1519
-----
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Constraint;
15+
16+
/**
17+
* @Annotation
18+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19+
*
20+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
21+
*/
22+
class Negative extends Constraint
23+
{
24+
const IS_POSITIVE_ERROR = '50430dfd-503a-4451-a9b6-3eb348a1d777';
25+
const IS_ZERO_ERROR = '5013ce74-a117-42d9-84ec-f076f52a6179';
26+
27+
protected static $errorNames = array(
28+
self::IS_POSITIVE_ERROR => 'IS_POSITIVE_ERROR',
29+
self::IS_ZERO_ERROR => 'IS_ZERO_ERROR',
30+
);
31+
32+
public $message = 'This value should be negative.';
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\Constraint;
15+
16+
/**
17+
* @Annotation
18+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19+
*
20+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
21+
*/
22+
class NegativeOrZero extends Constraint
23+
{
24+
const IS_POSITIVE_ERROR = '7b17bb87-4fce-4817-8167-3bb039dcae8e';
25+
26+
protected static $errorNames = array(
27+
self::IS_POSITIVE_ERROR => 'IS_POSITIVE_ERROR',
28+
);
29+
30+
public $message = 'This value should be either negative or zero.';
31+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
18+
/**
19+
* Validates that values are negative or zero.
20+
*
21+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
22+
*/
23+
class NegativeOrZeroValidator extends ConstraintValidator
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function validate($value, Constraint $constraint)
29+
{
30+
if (!$constraint instanceof NegativeOrZero) {
31+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\NegativeOrZero');
32+
}
33+
34+
if (!(\is_int($value) || \is_float($value) || \is_string($value))) {
35+
throw new UnexpectedTypeException($value, 'int|float|string');
36+
}
37+
38+
$value = (float) $value;
39+
40+
if ($value > 0) {
41+
$this->context->buildViolation($constraint->message)
42+
->setParameter('{{ value }}', $this->formatValue($value))
43+
->setCode(NegativeOrZero::IS_POSITIVE_ERROR)
44+
->addViolation();
45+
}
46+
}
47+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
18+
/**
19+
* Validates that values are negative.
20+
*
21+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
22+
*/
23+
class NegativeValidator extends ConstraintValidator
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function validate($value, Constraint $constraint)
29+
{
30+
if (!$constraint instanceof Negative) {
31+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Negative');
32+
}
33+
34+
if (!(\is_int($value) || \is_float($value) || \is_string($value))) {
35+
throw new UnexpectedTypeException($value, 'int|float|string');
36+
}
37+
38+
$value = (float) $value;
39+
40+
if (empty($value)) {
41+
$this->context->buildViolation($constraint->message)
42+
->setParameter('{{ value }}', $this->formatValue($value))
43+
->setCode(Negative::IS_ZERO_ERROR)
44+
->addViolation();
45+
}
46+
47+
if ($value > 0) {
48+
$this->context->buildViolation($constraint->message)
49+
->setParameter('{{ value }}', $this->formatValue($value))
50+
->setCode(Negative::IS_POSITIVE_ERROR)
51+
->addViolation();
52+
}
53+
}
54+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Constraint;
15+
16+
/**
17+
* @Annotation
18+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19+
*
20+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
21+
*/
22+
class Positive extends Constraint
23+
{
24+
const IS_NEGATIVE_ERROR = 'da93d4fe-371c-4bc2-99a3-9201efe7e1a4';
25+
const IS_ZERO_ERROR = '3a10c504-6039-424f-833e-f1f805da1d7a';
26+
27+
protected static $errorNames = array(
28+
self::IS_NEGATIVE_ERROR => 'IS_NEGATIVE_ERROR',
29+
self::IS_ZERO_ERROR => 'IS_ZERO_ERROR',
30+
);
31+
32+
public $message = 'This value should be positive.';
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\Constraint;
15+
16+
/**
17+
* @Annotation
18+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19+
*
20+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
21+
*/
22+
class PositiveOrZero extends Constraint
23+
{
24+
const IS_NEGATIVE_ERROR = 'd3b156b2-a386-4113-96be-76947bc7f16a';
25+
26+
protected static $errorNames = array(
27+
self::IS_NEGATIVE_ERROR => 'IS_NEGATIVE_ERROR',
28+
);
29+
30+
public $message = 'This value should be either positive or zero.';
31+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
18+
/**
19+
* Validates that values are positive or zero.
20+
*
21+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
22+
*/
23+
class PositiveOrZeroValidator extends ConstraintValidator
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function validate($value, Constraint $constraint)
29+
{
30+
if (!$constraint instanceof PositiveOrZero) {
31+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\PositiveOrZero');
32+
}
33+
34+
if (!(\is_int($value) || \is_float($value) || \is_string($value))) {
35+
throw new UnexpectedTypeException($value, 'int|float|string');
36+
}
37+
38+
$value = (float) $value;
39+
40+
if ($value < 0) {
41+
$this->context->buildViolation($constraint->message)
42+
->setParameter('{{ value }}', $this->formatValue($value))
43+
->setCode(PositiveOrZero::IS_NEGATIVE_ERROR)
44+
->addViolation();
45+
}
46+
}
47+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
18+
/**
19+
* Validates that values are positive.
20+
*
21+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
22+
*/
23+
class PositiveValidator extends ConstraintValidator
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function validate($value, Constraint $constraint)
29+
{
30+
if (!$constraint instanceof Positive) {
31+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Positive');
32+
}
33+
34+
if (!(\is_int($value) || \is_float($value) || \is_string($value))) {
35+
throw new UnexpectedTypeException($value, 'int|float|string');
36+
}
37+
38+
$value = (float) $value;
39+
40+
if (empty($value)) {
41+
$this->context->buildViolation($constraint->message)
42+
->setParameter('{{ value }}', $this->formatValue($value))
43+
->setCode(Positive::IS_ZERO_ERROR)
44+
->addViolation();
45+
}
46+
47+
if ($value < 0) {
48+
$this->context->buildViolation($constraint->message)
49+
->setParameter('{{ value }}', $this->formatValue($value))
50+
->setCode(Positive::IS_NEGATIVE_ERROR)
51+
->addViolation();
52+
}
53+
}
54+
}

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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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>

0 commit comments

Comments
 (0)
0