Skip to content

Commit 2e80502

Browse files
authored
Use separate constraint parameters instead of the options array to fix Symfony 7.3 deprecation triggers (#60)
1 parent 2ca7219 commit 2e80502

File tree

5 files changed

+44
-44
lines changed

5 files changed

+44
-44
lines changed

src/Constraint/ConstraintCollectionBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private function createAllConstraint($node)
7878
}
7979

8080
if ($required) {
81-
return [new Assert\Count(['min' => 1]), new Assert\All($constraints)];
81+
return [new Assert\Count(min: 1), new Assert\All($constraints)];
8282
}
8383

8484
return new Assert\All($constraints);
@@ -105,7 +105,7 @@ private function createCollectionConstraint(array $constraintTreeMap): Assert\Co
105105
$constraintMap[$key] = $this->getNodeConstraint($node, $optional);
106106
}
107107

108-
return new Assert\Collection(['fields' => $constraintMap, 'allowExtraFields' => $this->allowExtraFields]);
108+
return new Assert\Collection($constraintMap, allowExtraFields: $this->allowExtraFields);
109109
}
110110

111111
/**

src/Constraint/ConstraintResolver.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function resolveRuleList(RuleList $ruleList): array
2525
return $constraints;
2626
}
2727

28-
$nullable = false;
28+
$nullable = false;
2929
$constraints = [];
3030
foreach ($ruleList->getRules() as $rule) {
3131
if ($rule instanceof Constraint) {
@@ -70,27 +70,27 @@ private function resolveConstraint(RuleList $ruleList, Rule $rule): Constraint
7070
case Rule::RULE_ARRAY:
7171
return new Assert\Type('array');
7272
case Rule::RULE_ALPHA:
73-
return new Assert\Regex(['pattern' => '/^[a-zA-Z]*$/']);
73+
return new Assert\Regex('/^[a-zA-Z]*$/');
7474
case Rule::RULE_ALPHA_DASH:
75-
return new Assert\Regex(['pattern' => '/^[\w-]*$/']);
75+
return new Assert\Regex('/^[\w-]*$/');
7676
case Rule::RULE_ALPHA_NUM:
77-
return new Assert\Regex(['pattern' => '/^[a-zA-Z0-9]*$/']);
77+
return new Assert\Regex('/^[a-zA-Z0-9]*$/');
7878
case Rule::RULE_IN:
7979
return new Type\InConstraint(['values' => $rule->getParameters()]);
8080
case Rule::RULE_DATE:
8181
return new Assert\Date();
8282
case Rule::RULE_DATETIME:
8383
return new Assert\DateTime();
8484
case Rule::RULE_DATE_FORMAT:
85-
return new Assert\DateTime(['format' => $rule->getParameter(0)]);
85+
return new Assert\DateTime($rule->getParameter(0));
8686
case Rule::RULE_EMAIL:
8787
return new Assert\Email();
8888
case Rule::RULE_URL:
8989
return new Assert\Url();
9090
case Rule::RULE_REGEX:
91-
return new Assert\Regex(['pattern' => $rule->getParameter(0)]);
91+
return new Assert\Regex($rule->getParameter(0));
9292
case Rule::RULE_FILLED:
93-
return new Assert\NotBlank(['allowNull' => $ruleList->hasRule(Rule::RULE_NULLABLE)]);
93+
return new Assert\NotBlank(allowNull: $ruleList->hasRule(Rule::RULE_NULLABLE));
9494
case Rule::RULE_MIN:
9595
return $this->resolveMinConstraint($rule, $ruleList);
9696
case Rule::RULE_MAX:
@@ -117,7 +117,7 @@ private function resolveMinConstraint(Rule $rule, RuleList $ruleList): Constrain
117117
return new Assert\GreaterThanOrEqual($rule->getIntParam(0));
118118
}
119119

120-
return new Assert\Length(['min' => $rule->getIntParam(0)]);
120+
return new Assert\Length(min: $rule->getIntParam(0));
121121
}
122122

123123
/**
@@ -133,7 +133,7 @@ private function resolveMaxConstraint(Rule $rule, RuleList $ruleList): Constrain
133133
return new Assert\LessThanOrEqual($rule->getIntParam(0));
134134
}
135135

136-
return new Assert\Length(['max' => $rule->getIntParam(0)]);
136+
return new Assert\Length(max: $rule->getIntParam(0));
137137
}
138138

139139
/**
@@ -142,13 +142,13 @@ private function resolveMaxConstraint(Rule $rule, RuleList $ruleList): Constrain
142142
private function resolveBetweenConstraint(Rule $rule, RuleList $ruleList): Constraint
143143
{
144144
if ($ruleList->hasRule([Rule::RULE_DATE, Rule::RULE_DATETIME, Rule::RULE_DATE_FORMAT])) {
145-
return new Assert\Range(['min' => $rule->getParameter(0), 'max' => $rule->getParameter(1)]);
145+
return new Assert\Range(min: $rule->getParameter(0), max: $rule->getParameter(1));
146146
}
147147

148148
if ($ruleList->hasRule([Rule::RULE_INTEGER, Rule::RULE_FLOAT])) {
149-
return new Assert\Range(['min' => $rule->getIntParam(0), 'max' => $rule->getIntParam(1)]);
149+
return new Assert\Range(min: $rule->getIntParam(0), max: $rule->getIntParam(1));
150150
}
151151

152-
return new Assert\Length(['min' => $rule->getIntParam(0), 'max' => $rule->getIntParam(1)]);
152+
return new Assert\Length(min: $rule->getIntParam(0), max: $rule->getIntParam(1));
153153
}
154154
}

tests/Unit/Constraint/ConstraintCollectionBuilderTest.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
*/
2727
class ConstraintCollectionBuilderTest extends TestCase
2828
{
29-
/** @var ConstraintCollectionBuilder */
30-
private $builder;
29+
private ConstraintCollectionBuilder $builder;
3130

3231
protected function setUp(): void
3332
{
@@ -41,7 +40,7 @@ protected function setUp(): void
4140
*/
4241
public function testBuildSingleNonNestedConstraint(): void
4342
{
44-
$constraint = new NotNull();
43+
$constraint = new NotNull();
4544
$constraintMap = new ConstraintMap();
4645
$constraintMap->set('a', new ConstraintMapItem([$constraint], true));
4746

@@ -57,12 +56,12 @@ public function testBuildSingleNonNestedConstraint(): void
5756
*/
5857
public function testBuildSingleCollectionAllowExtraFieldsConstraint(): void
5958
{
60-
$constraint = new NotNull();
59+
$constraint = new NotNull();
6160
$constraintMap = new ConstraintMap();
6261
$constraintMap->set('a', new ConstraintMapItem([$constraint], true));
6362

6463
$result = $this->builder->setAllowExtraFields(true)->build($constraintMap);
65-
$expect = new Collection(['fields' => ['a' => new NotNull()], 'allowExtraFields' => true]);
64+
$expect = new Collection(['a' => new NotNull()], allowExtraFields: true);
6665
static::assertEquals($expect, $result);
6766
}
6867

@@ -72,7 +71,7 @@ public function testBuildSingleCollectionAllowExtraFieldsConstraint(): void
7271
*/
7372
public function testBuildSingleNestedConstraint(): void
7473
{
75-
$constraint = new NotNull();
74+
$constraint = new NotNull();
7675
$constraintMap = new ConstraintMap();
7776
$constraintMap->set('a.b', new ConstraintMapItem([$constraint], true));
7877

@@ -87,8 +86,8 @@ public function testBuildSingleNestedConstraint(): void
8786
*/
8887
public function testBuildMultipleNestedConstraints(): void
8988
{
90-
$constraintA = new NotNull();
91-
$constraintB = new Blank();
89+
$constraintA = new NotNull();
90+
$constraintB = new Blank();
9291
$constraintMap = new ConstraintMap();
9392
$constraintMap->set('a.a', new ConstraintMapItem([$constraintA], true));
9493
$constraintMap->set('a.b', new ConstraintMapItem([$constraintB], true));
@@ -104,7 +103,7 @@ public function testBuildMultipleNestedConstraints(): void
104103
*/
105104
public function testBuildOptionalConstraints(): void
106105
{
107-
$constraint = new NotNull();
106+
$constraint = new NotNull();
108107
$constraintMap = new ConstraintMap();
109108
$constraintMap->set('a?.b', new ConstraintMapItem([$constraint], true));
110109

@@ -121,7 +120,7 @@ public function testBuildOptionalConstraints(): void
121120
*/
122121
public function testBuildOptionalConstraintShouldNotOverwriteRequired(): void
123122
{
124-
$constraint = new NotNull();
123+
$constraint = new NotNull();
125124
$constraintMap = new ConstraintMap();
126125
$constraintMap->set('a.b?', new ConstraintMapItem([$constraint], true));
127126

@@ -136,13 +135,13 @@ public function testBuildOptionalConstraintShouldNotOverwriteRequired(): void
136135
*/
137136
public function testBuildWithNonEmptyAllConstraint(): void
138137
{
139-
$constraint = new NotNull();
138+
$constraint = new NotNull();
140139
$constraintMap = new ConstraintMap();
141140
$constraintMap->set('*', new ConstraintMapItem([$constraint], true));
142141

143142
$result = $this->builder->build($constraintMap);
144143
$expect = [
145-
new Count(['min' => 1]),
144+
new Count(min: 1),
146145
new All([new NotNull()])
147146
];
148147
static::assertEquals($expect, $result);
@@ -154,7 +153,7 @@ public function testBuildWithNonEmptyAllConstraint(): void
154153
*/
155154
public function testBuildWithEmptyAllConstraint(): void
156155
{
157-
$constraint = new NotNull();
156+
$constraint = new NotNull();
158157
$constraintMap = new ConstraintMap();
159158
$constraintMap->set('*', new ConstraintMapItem([$constraint], false));
160159

@@ -169,13 +168,13 @@ public function testBuildWithEmptyAllConstraint(): void
169168
*/
170169
public function testBuildWithAllAndCollectionConstraint(): void
171170
{
172-
$constraint = new NotNull();
171+
$constraint = new NotNull();
173172
$constraintMap = new ConstraintMap();
174173
$constraintMap->set('*.name', new ConstraintMapItem([$constraint], true));
175174

176175
$result = $this->builder->build($constraintMap);
177176
$expect =
178-
new All([new Collection(['fields' => ['name' => $constraint]])]);
177+
new All([new Collection(['name' => $constraint])]);
179178

180179
static::assertEquals($expect, $result);
181180
}

tests/Unit/Constraint/ConstraintResolverTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,22 @@ public function testResolveRuleSet(array $expected, array $rules): void
7272
public function dataProvider(): Generator
7373
{
7474
yield 'constraint' => [[new Assert\NotBlank()], [new Assert\NotBlank()]];
75-
yield 'rule + constraint' => [[new Assert\NotBlank(), new Assert\NotNull()],[new Rule('required'), new Assert\NotBlank()]];
75+
yield 'rule + constraint' => [[new Assert\NotBlank(), new Assert\NotNull()], [new Rule('required'), new Assert\NotBlank()]];
7676
yield 'boolean' => [[new BooleanValue(), new Assert\NotNull()], [new Rule('boolean')]];
7777
yield 'integer' => [[new IntegerNumber(), new Assert\NotNull()], [new Rule('integer')]];
7878
yield 'float' => [[new FloatNumber(), new Assert\NotNull()], [new Rule('float')]];
7979
yield 'array' => [[new Assert\Type('array'), new Assert\NotNull()], [new Rule('array')]];
8080
yield 'string' => [[new Assert\Type('string'), new Assert\NotNull()], [new Rule('string')]];
81-
yield 'alpha' => [[new Assert\Regex(['pattern' => '/^[a-zA-Z]*$/']), new Assert\NotNull()], [new Rule('alpha')]];
82-
yield 'alpha_dash' => [[new Assert\Regex(['pattern' => '/^[\w-]*$/']), new Assert\NotNull()], [new Rule('alpha_dash')]];
83-
yield 'alpha_num' => [[new Assert\Regex(['pattern' => '/^[a-zA-Z0-9]*$/']), new Assert\NotNull()], [new Rule('alpha_num')]];
81+
yield 'alpha' => [[new Assert\Regex('/^[a-zA-Z]*$/'), new Assert\NotNull()], [new Rule('alpha')]];
82+
yield 'alpha_dash' => [[new Assert\Regex('/^[\w-]*$/'), new Assert\NotNull()], [new Rule('alpha_dash')]];
83+
yield 'alpha_num' => [[new Assert\Regex('/^[a-zA-Z0-9]*$/'), new Assert\NotNull()], [new Rule('alpha_num')]];
8484
yield 'in' => [[new InConstraint(['values' => ['a', 'b']]), new Assert\NotNull()], [new Rule('in', ['a', 'b'])]];
8585
yield 'email' => [[new Assert\Email(), new Assert\NotNull()], [new Rule('email')]];
8686
yield 'url' => [[new Assert\Url(), new Assert\NotNull()], [new Rule('url')]];
8787
yield 'filled' => [[new Assert\NotBlank(), new Assert\NotNull()], [new Rule('filled')]];
88-
yield 'filled nullable' => [[new Assert\NotBlank(['allowNull' => true])], [new Rule('filled'), new Rule('nullable')]];
88+
yield 'filled nullable' => [[new Assert\NotBlank(allowNull: true)], [new Rule('filled'), new Rule('nullable')]];
8989
yield 'regex' => [
90-
[new Assert\Regex(['pattern' => '/^unittest$/']), new Assert\NotNull()],
90+
[new Assert\Regex('/^unittest$/'), new Assert\NotNull()],
9191
[new Rule('regex', ['/^unittest$/'])]
9292
];
9393
yield 'required' => [[new Assert\NotNull()], [new Rule('required')]];
@@ -96,7 +96,7 @@ public function dataProvider(): Generator
9696
// date, datetime, date_format
9797
yield 'date' => [[new Assert\Date(), new Assert\NotNull()], [new Rule('date')]];
9898
yield 'datetime' => [[new Assert\DateTime(), new Assert\NotNull()], [new Rule('datetime')]];
99-
yield 'date_format' => [[new Assert\DateTime(['format' => 'd/m/Y']), new Assert\NotNull()], [new Rule('date_format', ['d/m/Y'])]];
99+
yield 'date_format' => [[new Assert\DateTime('d/m/Y'), new Assert\NotNull()], [new Rule('date_format', ['d/m/Y'])]];
100100
yield 'date min' => [
101101
[new Assert\Date(), new Assert\GreaterThanOrEqual('now'), new Assert\NotNull()],
102102
[new Rule('date'), new Rule('min', ['now'])]
@@ -106,15 +106,15 @@ public function dataProvider(): Generator
106106
[new Rule('date'), new Rule('max', ['now'])]
107107
];
108108
yield 'date between' => [
109-
[new Assert\Date(), new Assert\Range(['min' => '-10 days', 'max' => '+10 days']), new Assert\NotNull()],
109+
[new Assert\Date(), new Assert\Range(min: '-10 days', max: '+10 days'), new Assert\NotNull()],
110110
[new Rule('date'), new Rule('between', ['-10 days', '+10 days'])]
111111
];
112112

113113
// min/max string or array lengths
114-
yield 'min length' => [[new Assert\Length(['min' => 10]), new Assert\NotNull()], [new Rule('min', ['10'])]];
115-
yield 'max length' => [[new Assert\Length(['max' => 10]), new Assert\NotNull()], [new Rule('max', ['10'])]];
114+
yield 'min length' => [[new Assert\Length(min: 10), new Assert\NotNull()], [new Rule('min', ['10'])]];
115+
yield 'max length' => [[new Assert\Length(max: 10), new Assert\NotNull()], [new Rule('max', ['10'])]];
116116
yield 'min/max length' => [
117-
[new Assert\Length(['min' => 10, 'max' => 20]), new Assert\NotNull()],
117+
[new Assert\Length(min: 10, max: 20), new Assert\NotNull()],
118118
[new Rule('between', ['10', '20'])]
119119
];
120120

@@ -128,7 +128,7 @@ public function dataProvider(): Generator
128128
[new Rule('integer'), new Rule('max', ['20'])]
129129
];
130130
yield 'min/max integer' => [
131-
[new IntegerNumber(), new Assert\Range(['min' => 10, 'max' => 20]), new Assert\NotNull()],
131+
[new IntegerNumber(), new Assert\Range(min: 10, max: 20), new Assert\NotNull()],
132132
[new Rule('integer'), new Rule('between', ['10', '20'])]
133133
];
134134
}

tests/Unit/ConstraintFactoryTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testFromRuleDefinitionsConstraintListOnly(): void
4545
public function testFromRuleDefinitionsWithRule(): void
4646
{
4747
$factory = new ConstraintFactory();
48-
$expect = new Assert\Collection(['email' => new Assert\Required([new Assert\Email(), new Assert\NotNull()])]);
48+
$expect = new Assert\Collection(['email' => new Assert\Required([new Assert\Email(), new Assert\NotNull()])]);
4949
static::assertEquals($expect, $factory->fromRuleDefinitions(['email' => 'required|email'], false));
5050
}
5151

@@ -56,8 +56,9 @@ public function testFromRuleDefinitionsWithRule(): void
5656
public function testFromRuleDefinitionsWithRuleAllowExtraFields(): void
5757
{
5858
$factory = new ConstraintFactory();
59-
$expect = new Assert\Collection(
60-
['fields' => ['email' => new Assert\Required([new Assert\Email(), new Assert\NotNull()])], 'allowExtraFields' => true]
59+
$expect = new Assert\Collection(
60+
['email' => new Assert\Required([new Assert\Email(), new Assert\NotNull()])],
61+
allowExtraFields: true
6162
);
6263
static::assertEquals($expect, $factory->fromRuleDefinitions(['email' => 'required|email'], true));
6364
}

0 commit comments

Comments
 (0)