8000 [Validator] add number constraints by jschaedl · Pull Request #28637 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] add number constraints #28637

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 1 commit into from
Mar 31, 2019
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
[Validator] add number constraints
  • Loading branch information
jschaedl authored and fabpot committed Mar 31, 2019
commit 01870398eb211fed5dcf91adbbf4215234ab9316
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ CHANGELOG
* added `Json` constraint
* added `Unique` constraint
* added a new `normalizer` option to the string constraints and to the `NotBlank` constraint
* added `Positive` constraint
* added `PositiveOrZero` constraint
* added `Negative` constraint
* added `NegativeOrZero` constraint

4.2.0
-----
Expand Down
35 changes: 35 additions 10000 & 0 deletions src/Symfony/Component/Validator/Constraints/Negative.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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 Jan Schädlich <jan.schaedlich@sensiolabs.de>
*/
class Negative extends LessThan
{
use NumberConstraintTrait;

public $message = 'This value should be negative.';

public function __construct($options = null)
{
parent::__construct($this->configureNumberConstraintOptions($options));
}

public function validatedBy(): string
{
return LessThanValidator::class;
}
}
35 changes: 35 additions & 0 deletions src/Symfony/Component/Validator/Constraints/NegativeOrZero.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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 Jan Schädlich <jan.schaedlich@sensiolabs.de>
*/
class NegativeOrZero extends LessThanOrEqual
{
use NumberConstraintTrait;

public $message = 'This value should be either negative or zero.';

public function __construct($options = null)
{
parent::__construct($this->configureNumberConstraintOptions($options));
}

public function validatedBy(): string
{
return LessThanOrEqualValidator::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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;

use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

/**
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
*/
trait NumberConstraintTrait
{
private function configureNumberConstraintOptions($options): array
{
if (null === $options) {
$options = [];
} elseif (!\is_array($options)) {
$options = [$this->getDefaultOption() => $options];
}

if (isset($options['propertyPath'])) {
throw new ConstraintDefinitionException(sprintf('The "propertyPath" option of the "%s" constraint cannot be set.', \get_class($this)));
}

if (isset($options['value'])) {
throw new ConstraintDefinitionException(sprintf('The "value" option of the "%s" constraint cannot be set.', \get_class($this)));
}

$options['value'] = 0;

return $options;
}
}
35 changes: 35 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Positive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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 Jan Schädlich <jan.schaedlich@sensiolabs.de>
*/
class Positive extends GreaterThan
{
use NumberConstraintTrait;

public $message = 'This value should be positive.';

public function __construct($options = null)
{
parent::__construct($this->configureNumberConstraintOptions($options));
}

public function validatedBy(): string
{
return GreaterThanValidator::class;
}
}
35 changes: 35 additions & 0 deletions src/Symfony/Component/Validator/Constraints/PositiveOrZero.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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 Jan Schädlich <jan.schaedlich@sensiolabs.de>
*/
class PositiveOrZero extends GreaterThanOrEqual
{
use NumberConstraintTrait;

public $message = 'This value should be either positive or zero.';

public function __construct($options = null)
{
parent::__construct($this->configureNumberConstraintOptions($options));
}

public function validatedBy(): string
{
return GreaterThanOrEqualValidator::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,22 @@
<source>This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.</source>
<target>Diese internationale Bankleitzahl (BIC) ist nicht mit der IBAN {{ iban }} assoziiert.</target>
</trans-unit>
<trans-unit id="87">
<source>This value should be positive.</source>
<target>Dieser Wert sollte positiv sein.</target>
</trans-unit>
<trans-unit id="88">
<source>This value should be either positive or zero.</source>
<target>Dieser Wert sollte entweder positiv oder 0 sein.</target>
</trans-unit>
<trans-unit id="89">
<source>This value should be negative.</source>
<target>Dieser Wert sollte negativ sein.</target>
</trans-unit>
<trans-unit id="90">
<source>This value should be either negative or zero.</source>
<target>Dieser Wert sollte entweder negativ oder 0 sein.</target>
</trans-unit>
</body>
</file>
</xliff>
10000
Original file line numberDiff line number Diff line change
Expand Up @@ -334,6 +334,22 @@
<source>This value should be valid JSON.</source>
<target>This value should be valid JSON.</target>
</trans-unit>
<trans-unit id="87">
<source>This value should be positive.</source>
<target>This value should be positive.</target>
</trans-unit>
<trans-unit id="88">
<source>This value should be either positive or zero.</source>
<target>This value should be either positive or zero.</target>
</trans-unit>
<trans-unit id="89">
<source>This value should be negative.</source>
<target>This value should be negative.</target>
</trans-unit>
<trans-unit id="90">
<source>This value should be either negative or zero.</source>
<target>This value should be either negative or zero.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,22 @@
<source>This value should not be identical to {{ compared_value_type }} {{ compared_value }}.</source>
<target>Giá trị không được phép giống như {{ compared_value_type }} {{ compared_value }}.</target>
</trans-unit>
<trans-unit id="87">
<source>This value should be positive.</source>
<target>Giá trị này có thể thực hiện được.</target>
</trans-unit>
<trans-unit id="88">
<source>This value should be either positive or zero.</source>
<target>Giá trị này có thể thực hiện được hoặc bằng không.</target>
</trans-unit>
<trans-unit id="89">
<source>This value should be negative.</source>
<target>Giá trị này nên bị từ chối.</target>
</trans-unit>
<trans-unit id="90">
<source>This value should be either negative or zero.</source>
<target>Giá trị này nên bị từ chối hoặc bằng không.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?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\PositiveOrZero;

/**
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
*/
class GreaterThanOrEqualValidatorWithPositiveOrZeroConstraintTest extends GreaterThanOrEqualValidatorTest
{
protected function createConstraint(array $options = null)
{
return new PositiveOrZero();
}

/**
* {@inheritdoc}
*/
public function provideValidComparisons()
{
return [
[0, 0],
[1, 0],
[2, 0],
[2.5, 0],
['0', '0'],
['333', '0'],
[null, 0],
];
}

/**
* {@inheritdoc}
*/
public function provideInvalidComparisons()
{
return [
[-1, '-1', 0, '0', 'integer'],
[-2, '-2', 0, '0', 'integer'],
[-2.5, '-2.5', 0, '0', 'integer'],
];
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
* @expectedExceptionMessage The "propertyPath" option of the "Symfony\Component\Validator\Constraints\PositiveOrZero" constraint cannot be set.
*/
public function testThrowsConstraintExceptionIfPropertyPath()
{
return new PositiveOrZero(['propertyPath' => 'field']);
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
* @expectedExceptionMessage The "value" option of the "Symfony\Component\Validator\Constraints\PositiveOrZero" constraint cannot be set.
*/
public function testThrowsConstraintExceptionIfValue()
{
return new PositiveOrZero(['value' => 0]);
}

/**
* @dataProvider provideInvalidConstraintOptions
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
* @expectedExceptionMessage requires either the "value" or "propertyPath" option to be set.
*/
public function testThrowsConstraintExceptionIfNoValueOrPropertyPath($options)
{
$this->markTestSkipped('Value option always set for PositiveOrZero constraint');
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
* @expectedExceptionMessage requires only one of the "value" or "propertyPath" options to be set, not both.
*/
public function testThrowsConstraintExceptionIfBothValueAndPropertyPath()
{
$this->markTestSkipped('Value option is set for PositiveOrZero constraint automatically');
}

public function testInvalidValuePath()
{
$this->markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
}

/**
* @dataProvider provideValidComparisonsToPropertyPath
*/
public function testValidComparisonToPropertyPath($comparedValue)
{
$this->markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
}

/**
* @dataProvider provideValidComparisonsToPropertyPath
*/
public function testValidComparisonToPropertyPathOnArray($comparedValue)
{
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
}
}
Loading
0