Skip to content

Commit e0de91b

Browse files
vudaltsovfabpot
authored andcommitted
[Validator] Fix annotation default for @count and @Length
1 parent 8b59455 commit e0de91b

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

Constraints/Count.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public function __construct($options = null)
4343
'min' => $options,
4444
'max' => $options,
4545
];
46+
} elseif (\is_array($options) && isset($options['value']) && !isset($options['min']) && !isset($options['max'])) {
47+
$options['min'] = $options['max'] = $options['value'];
48+
unset($options['value']);
4649
}
4750

4851
parent::__construct($options);

Constraints/Length.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public function __construct($options = null)
4747
'min' => $options,
4848
'max' => $options,
4949
];
50+
} elseif (\is_array($options) && isset($options['value']) && !isset($options['min']) && !isset($options['max'])) {
51+
$options['min'] = $options['max'] = $options['value'];
52+
unset($options['value']);
5053
}
5154

5255
parent::__construct($options);

Tests/Constraints/CountValidatorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,13 @@ public function testDefaultOption()
195195
$this->assertEquals(5, $constraint->min);
196196
$this->assertEquals(5, $constraint->max);
197197
}
198+
199+
public function testConstraintAnnotationDefaultOption()
200+
{
201+
$constraint = new Count(['value' => 5, 'exactMessage' => 'message']);
202+
203+
$this->assertEquals(5, $constraint->min);
204+
$this->assertEquals(5, $constraint->max);
205+
$this->assertEquals('message', $constraint->exactMessage);
206+
}
198207
}

Tests/Constraints/LengthValidatorTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,20 @@ public function testOneCharset($value, $charset, $isValid)
237237
}
238238
}
239239

240-
public function testConstraintGetDefaultOption()
240+
public function testConstraintDefaultOption()
241241
{
242242
$constraint = new Length(5);
243243

244244
$this->assertEquals(5, $constraint->min);
245245
$this->assertEquals(5, $constraint->max);
246246
}
247+
248+
public function testConstraintAnnotationDefaultOption()
249+
{
250+
$constraint = new Length(['value' => 5, 'exactMessage' => 'message']);
251+
252+
$this->assertEquals(5, $constraint->min);
253+
$this->assertEquals(5, $constraint->max);
254+
$this->assertEquals('message', $constraint->exactMessage);
255+
}
247256
}

0 commit comments

Comments
 (0)