Skip to content

Commit

Permalink
add array handling for length validation add php 8.3 to ci
Browse files Browse the repository at this point in the history
Signed-off-by: Thibault JUNIN <[email protected]>
  • Loading branch information
thibaultjunin committed Jun 12, 2024
1 parent 852067c commit 1e17fe0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ 8.1, 8.2 ]
php: [ 8.1, 8.2, 8.3 ]

name: PHP ${{ matrix.php }} tests

Expand Down
11 changes: 9 additions & 2 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,19 @@ public function length(string $key, int $min, ?int $max = NULL): self
return $this;
}

if (strlen($value) < $min) {
$length = 0;
if (is_array($value)) {
$length = count($value);
} else {
$length = strlen($value);
}

if ($length < $min) {
$this->addError($key, ValidationRules::LENGTH, $min, $max);
return $this;
}

if ($max != NULL && strlen($value) > $max) {
if ($max != NULL && $length > $max) {
$this->addError($key, ValidationRules::LENGTH, $min, $max);
return $this;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/SimpleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ public function testLength()
$this->assertCount(1, $validator->getErrors(), json_encode($validator->getErrors()));
}

public function testLengthArray()
{
$validator = $this->createValidator(["abc" => ["a", "b", "c"]]);
$validator->length("abc", 2, 4);
$this->assertCount(0, $validator->getErrors(), json_encode($validator->getErrors()));

$validator = $this->createValidator(["abc" => ["a", "b", "c", "d", "e", "f"]]);
$validator->length("abc", 2, 4);
$this->assertCount(1, $validator->getErrors(), json_encode($validator->getErrors()));
}

public function testDateTime()
{
$validator = $this->createValidator(["abc" => "2022-03-20 03:08:42"]);
Expand Down

0 comments on commit 1e17fe0

Please sign in to comment.