Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 8aaba7f

Browse files
committed
chore: improvements on message handling to not allow null
1 parent 8b67850 commit 8aaba7f

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

src/Rule/GreaterThan.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
class GreaterThan extends AbstractRule implements RuleInterface
88
{
9+
private string $message;
10+
911
public function __construct(
1012
private readonly mixed $constraint,
11-
private ?string $message = null)
13+
string $message = null)
1214
{
13-
$this->message ??= 'The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.';
15+
$this->message = $message ?? 'The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.';
1416
}
1517

1618
/**
@@ -40,17 +42,17 @@ public function validate(mixed $value): void
4042
}
4143
}
4244

43-
protected function canBeCompared(mixed $constraint, mixed $value): bool
45+
protected function canBeCompared(mixed $value1, mixed $value2): bool
4446
{
45-
if ($constraint instanceof \DateTimeInterface && $value instanceof \DateTimeInterface) {
47+
if ($value1 instanceof \DateTimeInterface && $value2 instanceof \DateTimeInterface) {
4648
return true;
4749
}
4850

49-
if (\is_numeric($constraint) && \is_numeric($value)) {
51+
if (\is_numeric($value1) && \is_numeric($value2)) {
5052
return true;
5153
}
5254

53-
if (\is_string($constraint) && \is_string($value)) {
55+
if (\is_string($value1) && \is_string($value2)) {
5456
return true;
5557
}
5658

src/Rule/NotBlank.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@
66

77
class NotBlank extends AbstractRule implements RuleInterface
88
{
9-
public function __construct(private ?string $message = null)
9+
private string $message;
10+
11+
public function __construct(string $message = null)
1012
{
11-
$this->message ??= 'The "{{ name }}" value should not be blank, "{{ value }}" given.';
13+
$this->message = $message ?? 'The "{{ name }}" value should not be blank, "{{ value }}" given.';
1214
}
1315

1416
/**
1517
* @throws NotBlankException
1618
*/
1719
public function validate(mixed $value): void
1820
{
19-
// Keep value unchanged for parameters
20-
$input = $value;
21-
2221
// Do not allow null, false, [] and ''
23-
if ($input === false || (empty($input) && $input != '0')) {
22+
if ($value === false || (empty($value) && $value != '0')) {
2423
throw new NotBlankException(
2524
message: $this->message,
2625
parameters: [

0 commit comments

Comments
 (0)