8000 [Validator] move duplicated code into trait · symfony/symfony@c77ef3b · GitHub
[go: up one dir, main page]

Skip to content

Commit c77ef3b

Browse files
committed
[Validator] move duplicated code into trait
1 parent 0e31c3b commit c77ef3b

File tree

5 files changed

+53
-76
lines changed

5 files changed

+53
-76
lines changed

src/Symfony/Component/Validator/Constraints/Negative.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
15-
1614
/**
1715
* @Annotation
1816
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
@@ -21,27 +19,13 @@
2119
*/
2220
class Negative extends LessThan
2321
{
22+
use NumberConstraintTrait;
23+
2424
public $message = 'This value should be negative.';
2525

2626
public function __construct($options = null)
2727
{
28-
if (null === $options) {
29-
$options = [];
30-
} elseif (!\is_array($options)) {
31-
$options = [$this->getDefaultOption() => $options];
32-
}
33-
34-
if (isset($options['propertyPath'])) {
35-
throw new ConstraintDefinitionException(sprintf('The "propertyPath" option of the "%s" constraint cannot be set.', \get_class($this)));
36-
}
37-
38-
if (isset($options['value'])) {
39-
throw new ConstraintDefinitionException(sprintf('The "value" option of the "%s" constraint cannot be set.', \get_class($this)));
40-
}
41-
42-
$options['value'] = 0;
43-
44-
parent::__construct($options);
28+
parent::__construct($this->configureNumberConstraintOptions($options));
4529
}
4630

4731
public function validatedBy(): string

src/Symfony/Component/Validator/Constraints/NegativeOrZero.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
15-
1614
/**
1715
* @Annotation
1816
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
@@ -21,27 +19,13 @@
2119
*/
2220
class NegativeOrZero extends LessThanOrEqual
2321
{
22+
use NumberConstraintTrait;
23+
2424
public $message = 'This value should be either negative or zero.';
2525

2626
public function __construct($options = null)
2727
{
28-
if (null === $options) {
29-
$options = [];
30-
} elseif (!\is_array($options)) {
31-
$options = [$this->getDefaultOption() => $options];
32-
}
33-
34-
if (isset($options['propertyPath'])) {
35-
throw new ConstraintDefinitionException(sprintf('The "propertyPath" option of the "%s" constraint cannot be set.', \get_class($this)));
36-
}
37-
38-
if (isset($options['value'])) {
39-
throw new ConstraintDefinitionException(sprintf('The "value" option of the "%s" constraint cannot be set.', \get_class($this)));
40-
}
41-
42-
$options['value'] = 0;
43-
44-
parent::__construct($options);
28+
parent::__construct($this->configureNumberConstraintOptions($options));
4529
}
4630

4731
public function validatedBy(): string
Lines changed: 41 additions & 0 deletions
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+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
18+
*/
19+
trait NumberConstraintTrait
20+
{
21+
private function configureNumberConstraintOptions($options): array
22+
{
23+
if (null === $options) {
24+
$options = [];
25+
} elseif (!\is_array($options)) {
26+
$options = [$this->getDefaultOption() => $options];
27+
}
28+
29+
if (isset($options['propertyPath'])) {
30+
throw new ConstraintDefinitionException(sprintf('The "propertyPath" option of the "%s" constraint cannot be set.', \get_class($this)));
31+
}
32+
33+
if (isset($options['value'])) {
34+
throw new ConstraintDefinitionException(sprintf('The "value" option of the "%s" constraint cannot be set.', \get_class($this)));
35+
}
36+
37+
$options['value'] = 0;
38+
39+
return $options;
40+
}
41+
}

src/Symfony/Component/Validator/Constraints/Positive.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
15-
1614
/**
1715
* @Annotation
1816
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
@@ -21,27 +19,13 @@
2119
*/
2220
class Positive extends GreaterThan
2321
{
22+
use NumberConstraintTrait;
23+
2424
public $message = 'This value should be positive.';
2525

2626
public function __construct($options = null)
2727
{
28-
if (null === $options) {
29-
$options = [];
30-
} elseif (!\is_array($options)) {
31-
$options = [$this->getDefaultOption() => $options];
32-
}
33-
34-
if (isset($options['propertyPath'])) {
35-
throw new ConstraintDefinitionException(sprintf('The "propertyPath" option of the "%s" constraint cannot be set.', \get_class($this)));
36-
}
37-
38-
if (isset($options['value'])) {
39-
throw new ConstraintDefinitionException(sprintf('The "value" option of the "%s" constraint cannot be set.', \get_class($this)));
40-
}
41-
42-
$options['value'] = 0;
43-
44-
parent::__construct($options);
28+
parent::__construct($this->configureNumberConstraintOptions($options));
4529
}
4630

4731
public function validatedBy(): string

src/Symfony/Component/Validator/Constraints/PositiveOrZero.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
15-
1614
/**
1715
* @Annotation
1816
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
@@ -21,27 +19,13 @@
2119
*/
2220
class PositiveOrZero extends GreaterThanOrEqual
2321
{
22+
use NumberConstraintTrait;
23+
2424
public $message = 'This value should be either positive or zero.';
2525

2626
public function __construct($options = null)
2727
{
28-
if (null === $options) {
29-
$options = [];
30-
} elseif (!\is_array($options)) {
31-
$options = [$this->getDefaultOption() => $options];
32-
}
33-
34-
if (isset($options['propertyPath'])) {
35-
throw new ConstraintDefinitionException(sprintf('The "propertyPath" option of the "%s" constraint cannot be set.', \get_class($this)));
36-
}
37-
38-
if (isset($options['value'])) {
39-
throw new ConstraintDefinitionException(sprintf('The "value" option of the "%s" constraint cannot be set.', \get_class($this)));
40-
}
41-
42-
$options['value'] = 0;
43-
44-
parent::__construct($options);
28+
parent::__construct($this->configureNumberConstraintOptions($options));
4529
}
4630

4731
public function validatedBy(): string

0 commit comments

Comments
 (0)
0