Skip to content

Commit 567245f

Browse files
authored
refactor(validation): add type guards and Unicode support across rules (#1780)
1 parent dc701e3 commit 567245f

File tree

15 files changed

+46
-6
lines changed

15 files changed

+46
-6
lines changed

packages/validation/src/Rules/DoesNotEndWith.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public function __construct(
2020

2121
public function isValid(mixed $value): bool
2222
{
23+
if (! is_string($value)) {
24+
return false;
25+
}
26+
2327
return ! str_ends_with($value, $this->needle);
2428
}
2529

packages/validation/src/Rules/DoesNotStartWith.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public function __construct(
2020

2121
public function isValid(mixed $value): bool
2222
{
23+
if (! is_string($value)) {
24+
return false;
25+
}
26+
2327
return ! str_starts_with($value, $this->needle);
2428
}
2529

packages/validation/src/Rules/EndsWith.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public function __construct(
2020

2121
public function isValid(mixed $value): bool
2222
{
23+
if (! is_string($value)) {
24+
return false;
25+
}
26+
2327
return str_ends_with($value, $this->needle);
2428
}
2529

packages/validation/src/Rules/HasCount.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public function __construct(
2626

2727
public function isValid(mixed $value): bool
2828
{
29+
if (! is_array($value) && ! $value instanceof \Countable) {
30+
return false;
31+
}
32+
2933
$length = count($value);
3034

3135
$min = $this->min ?? $length;

packages/validation/src/Rules/HasLength.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function isValid(mixed $value): bool
3030
return false;
3131
}
3232

33-
$length = strlen($value);
33+
$length = mb_strlen($value);
3434

3535
$min = $this->min ?? $length;
3636
$max = $this->max ?? $length;

packages/validation/src/Rules/IsBetween.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public function __construct(
2121

2222
public function isValid(mixed $value): bool
2323
{
24+
if (! is_numeric($value)) {
25+
return false;
26+
}
27+
2428
return $value >= $this->min && $value <= $this->max;
2529
}
2630

packages/validation/src/Rules/IsDivisibleBy.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ public function __construct(
2020

2121
public function isValid(mixed $value): bool
2222
{
23-
if (! is_numeric($value) || $value === 0) {
23+
if (! is_numeric($value)) {
2424
return false;
2525
}
2626

27-
return new IsMultipleOf($this->divisor)->isValid($value);
27+
$intValue = (int) $value;
28+
29+
return ($intValue % $this->divisor) === 0;
2830
}
2931

3032
public function getTranslationVariables(): array

packages/validation/src/Rules/IsEnum.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ enum: $this->enum,
6262
...$this->only,
6363
...(is_array($values) ? $values : func_get_args()),
6464
],
65+
except: $this->except,
66+
orNull: $this->orNull,
6567
);
6668
}
6769

packages/validation/src/Rules/IsJsonString.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public function __construct(
2020

2121
public function isValid(mixed $value): bool
2222
{
23+
if (! is_string($value)) {
24+
return false;
25+
}
26+
2327
$arguments = ['json' => $value];
2428

2529
if ($this->depth !== null) {

packages/validation/src/Rules/IsLowercase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
{
1616
public function isValid(mixed $value): bool
1717
{
18+
if (! is_string($value)) {
19+
return false;
20+
}
21+
1822
return $value === mb_strtolower($value);
1923
}
2024
}

0 commit comments

Comments
 (0)