Skip to content

Commit e31339f

Browse files
sandermarechalGuilhemN
authored andcommitted
Allow arrays of constraints on parameters (#1752)
* Allow arrays of constraints on parameters * Add deprecation notices * Silence deprecation notices * Tag tests that produce deprecation notices * Use class constant instead of FQCN string * Limit deprecations
1 parent 92a7586 commit e31339f

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

Controller/Annotations/AbstractScalarParam.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ public function getConstraints()
5454
'pattern' => '#^(?:'.$this->requirements['rule'].')$#xsu',
5555
'message' => $this->requirements['error_message'],
5656
));
57+
} elseif (is_array($this->requirements)) {
58+
foreach ($this->requirements as $requirement) {
59+
if ($requirement instanceof Constraint) {
60+
$constraints[] = $requirement;
61+
} else {
62+
@trigger_error('Using an array not only containing `Constraint`s as requirements is deprecated since version 2.6.', E_USER_DEPRECATED);
63+
}
64+
}
5765
}
5866

5967
if (false === $this->allowBlank) {

Resources/doc/param_fetcher_listener.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ configured for the matched controller so that the user does not need to do this
4949
* Note that if the value matches the default then no validation is run.
5050
* So make sure the default value really matches your expectations.
5151
*
52+
* @RequestParam(name="simpleEmail", requirements=@Constraints\Email)
53+
* @RequestParam(name="complexEmail", requirements={@Constraints\Email, @Constraints\NotEqualTo("[email protected]")})
54+
* You can use one or multiple Symfony Validator constraints for more complex requirements checking. The first
55+
* example above checks for a correctly formatted e-mail adress. The second example also ensures that it does not
56+
* matches some default example value.
57+
*
5258
* @RequestParam(name="search", requirements="[a-z]+", description="search")
5359
* @RequestParam(name="byauthor", requirements="[a-z]+", description="by author", incompatibles={"search"})
5460
* Imagine you have an api for a blog with to get Articles with two ways of filtering

Tests/Controller/Annotations/AbstractScalarParamTest.php

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

1212
namespace FOS\RestBundle\Tests\Controller\Annotations;
1313

14+
use FOS\RestBundle\Controller\Annotations\AbstractScalarParam;
1415
use FOS\RestBundle\Validator\Constraints\Regex;
1516
use PHPUnit\Framework\TestCase;
17+
use Symfony\Component\Validator\Constraint;
1618
use Symfony\Component\Validator\Constraints;
1719

1820
/**
@@ -24,12 +26,12 @@ class AbstractScalarParamTest extends TestCase
2426
{
2527
public function setUp()
2628
{
27-
$this->param = $this->getMockForAbstractClass('FOS\RestBundle\Controller\Annotations\AbstractScalarParam');
29+
$this->param = $this->getMockForAbstractClass(AbstractScalarParam::class);
2830
}
2931

3032
public function testInterface()
3133
{
32-
$this->assertInstanceOf('FOS\RestBundle\Controller\Annotations\AbstractParam', $this->param);
34+
$this->assertInstanceOf(AbstractScalarParam::class, $this->param);
3335
}
3436

3537
public function testDefaultValues()
@@ -48,13 +50,26 @@ public function testScalarConstraint()
4850

4951
public function testComplexRequirements()
5052
{
51-
$this->param->requirements = $requirement = $this->getMockBuilder('Symfony\Component\Validator\Constraint')->getMock();
53+
$this->param->requirements = $requirement = $this->getMockBuilder(Constraint::class)->getMock();
5254
$this->assertEquals(array(
5355
new Constraints\NotNull(),
5456
$requirement,
5557
), $this->param->getConstraints());
5658
}
5759

60+
public function testMultipleComplexRequirements()
61+
{
62+
$requirement1 = $this->getMockBuilder(Constraint::class)->getMock();
63+
$requirement2 = $this->getMockBuilder(Constraint::class)->getMock();
64+
$this->param->requirements = array($requirement1, $requirement2);
65+
66+
$this->assertEquals(array(
67+
new Constraints\NotNull(),
68+
$requirement1,
69+
$requirement2,
70+
), $this->param->getConstraints());
71+
}
72+
5873
public function testScalarRequirements()
5974
{
6075
$this->param->name = 'bar';

0 commit comments

Comments
 (0)