Skip to content

Commit 1922a53

Browse files
sidzfabpot
authored andcommitted
[Validator] [WordCount] Treat 0 as one character word and do not exclude it
1 parent c041136 commit 1922a53

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

Constraints/WordCountValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function validate(mixed $value, Constraint $constraint): void
4444
$words = iterator_to_array($iterator->getPartsIterator());
4545

4646
// erase "blank words" and don't count them as words
47-
$wordsCount = \count(array_filter(array_map(trim(...), $words)));
47+
$wordsCount = \count(array_filter(array_map(trim(...), $words), fn ($word) => '' !== $word));
4848

4949
if (null !== $constraint->min && $wordsCount < $constraint->min) {
5050
$this->context->buildViolation($constraint->minMessage)

Tests/Constraints/WordCountValidatorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public static function provideValidValues()
8383
yield [new StringableValue('my ûtf 8'), 3];
8484
yield [null, 1]; // null should always pass and eventually be handled by NotNullValidator
8585
yield ['', 1]; // empty string should always pass and eventually be handled by NotBlankValidator
86+
yield ['My String 0', 3];
8687
}
8788

8889
public static function provideInvalidTypes()

0 commit comments

Comments
 (0)