Skip to content

Commit cc77d5d

Browse files
committed
Fix deprecations in symfony/validator, including the fix in #2417
Fixes #2416
1 parent d247368 commit cc77d5d

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

Controller/Annotations/AbstractScalarParam.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ public function getConstraints()
4141
if ($this->requirements instanceof Constraint) {
4242
$constraints[] = $this->requirements;
4343
} elseif (is_scalar($this->requirements)) {
44-
$constraints[] = new Regex([
45-
'pattern' => '#^(?:'.$this->requirements.')$#xsu',
46-
'message' => sprintf(
44+
$constraints[] = new Regex(
45+
'#^(?:'.$this->requirements.')$#xsu',
46+
sprintf(
4747
'Parameter \'%s\' value, does not match requirements \'%s\'',
4848
$this->getName(),
4949
$this->requirements
5050
),
51-
]);
51+
);
5252
} elseif (is_array($this->requirements) && isset($this->requirements['rule']) && $this->requirements['error_message']) {
53-
$constraints[] = new Regex([
54-
'pattern' => '#^(?:'.$this->requirements['rule'].')$#xsu',
55-
'message' => $this->requirements['error_message'],
56-
]);
53+
$constraints[] = new Regex(
54+
'#^(?:'.$this->requirements['rule'].')$#xsu',
55+
$this->requirements['error_message'],
56+
);
5757
} elseif (is_array($this->requirements)) {
5858
foreach ($this->requirements as $index => $requirement) {
5959
if ($requirement instanceof Constraint) {
@@ -75,9 +75,12 @@ public function getConstraints()
7575
// If the user wants to map the value, apply all constraints to every
7676
// value of the map
7777
if ($this->map) {
78-
$constraints = [
79-
new All(['constraints' => $constraints]),
80-
];
78+
if ([] !== $constraints) {
79+
$constraints = [
80+
new All($constraints),
81+
];
82+
}
83+
8184
if (false === $this->nullable) {
8285
$constraints[] = new NotNull();
8386
}

Controller/Annotations/FileParam.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,19 @@ public function getConstraints()
7979

8080
$options = is_array($this->requirements) ? $this->requirements : [];
8181
if ($this->image) {
82-
$constraints[] = new Image($options);
82+
$constraint = new Image();
8383
} else {
84-
$constraints[] = new File($options);
84+
$constraint = new File();
8585
}
86+
foreach ($options as $name => $value) {
87+
$constraint->$name = $value;
88+
}
89+
$constraints[] = $constraint;
8690

8791
// If the user wants to map the value
8892
if ($this->map) {
8993
$constraints = [
90-
new All(['constraints' => $constraints]),
94+
new All($constraints),
9195
];
9296
}
9397

Tests/Controller/Annotations/AbstractScalarParamTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ public function testArrayWithNoConstraintsDoesNotCreateInvalidConstraint()
133133
{
134134
$this->param->nullable = true;
135135
$this->param->map = true;
136-
$this->assertEquals([new All([
137-
'constraints' => [],
138-
])], $this->param->getConstraints());
136+
$this->assertEquals([], $this->param->getConstraints());
139137
}
140138
}

Tests/Controller/Annotations/FileParamTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,40 +75,40 @@ public function testComplexRequirements()
7575
public function testFileRequirements()
7676
{
7777
$this->param->nullable = true;
78-
$this->param->requirements = $requirements = ['mimeTypes' => 'application/json'];
78+
$this->param->requirements = ['mimeTypes' => 'application/json'];
7979
$this->assertEquals([
80-
new File($requirements),
80+
new File(null, null, null, 'application/json'),
8181
], $this->param->getConstraints());
8282
}
8383

8484
public function testImageRequirements()
8585
{
8686
$this->param->image = true;
87-
$this->param->requirements = $requirements = ['mimeTypes' => 'image/gif'];
87+
$this->param->requirements = ['mimeTypes' => ['image/gif']];
8888
$this->assertEquals([
8989
new NotNull(),
90-
new Image($requirements),
90+
new Image(null, null, null, ['image/gif']),
9191
], $this->param->getConstraints());
9292
}
9393

9494
public function testImageConstraintsTransformWhenParamIsAnArray()
9595
{
9696
$this->param->image = true;
9797
$this->param->map = true;
98-
$this->param->requirements = $requirements = ['mimeTypes' => 'image/gif'];
98+
$this->param->requirements = ['mimeTypes' => ['image/gif']];
9999
$this->assertEquals([new All([
100100
new NotNull(),
101-
new Image($requirements),
101+
new Image(null, null, null, ['image/gif']),
102102
])], $this->param->getConstraints());
103103
}
104104

105105
public function testFileConstraintsWhenParamIsAnArray()
106106
{
107107
$this->param->map = true;
108-
$this->param->requirements = $requirements = ['mimeTypes' => 'application/pdf'];
108+
$this->param->requirements = ['mimeTypes' => 'application/pdf'];
109109
$this->assertEquals([new All([
110110
new NotNull(),
111-
new File($requirements),
111+
new File(null, null, null, 'application/pdf'),
112112
])], $this->param->getConstraints());
113113
}
114114
}

0 commit comments

Comments
 (0)