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

Commit 0f59640

Browse files
committed
feat: added base GreaterThan rule
1 parent ee12cf3 commit 0f59640

5 files changed

Lines changed: 64 additions & 4 deletions

File tree

src/ChainedValidatorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ public function assert(mixed $value, string $name): void;
1919

2020
public function notBlank(?string $message = null): ChainedValidatorInterface;
2121

22-
// public function greaterThan(mixed $constraint): ChainedValidatorInterface;
22+
public function greaterThan(mixed $constraint, ?string $message = null): ChainedValidatorInterface;
2323
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Exception;
4+
5+
class GreaterThanException extends ValidationException {}

src/Rule/GreaterThan.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
6+
7+
class GreaterThan extends AbstractRule implements RuleInterface
8+
{
9+
public function __construct(
10+
private readonly mixed $constraint,
11+
private ?string $message = null)
12+
{
13+
$this->message ??= 'The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.';
14+
}
15+
16+
/**
17+
* @throws GreaterThanException
18+
*/
19+
public function validate(mixed $value): void
20+
{
21+
if (!$this->canBeCompared($this->constraint, $value)) {
22+
throw new \LogicException(
23+
\sprintf(
24+
'Cannot compare a constraint type "%s" with a value type "%s"',
25+
get_debug_type($this->constraint),
26+
get_debug_type($value)
27+
)
28+
);
29+
}
30+
31+
if (!($value > $this->constraint)) {
32+
throw new GreaterThanException(
33+
message: $this->message,
34+
parameters: [
35+
'name' => $this->getName(),
36+
'constraint' => $this->constraint,
37+
'value' => $value
38+
]
39+
);
40+
}
41+
}
42+
43+
protected function canBeCompared(mixed $constraint, mixed $value): bool
44+
{
45+
if ($constraint instanceof \DateTimeInterface && $value instanceof \DateTimeInterface) {
46+
return true;
47+
}
48+
49+
if (is_scalar($constraint) && is_scalar($value)) {
50+
return true;
51+
}
52+
53+
return false;
54+
}
55+
}

src/Rule/NotBlank.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public function validate(mixed $value): void
2424
throw new NotBlankException(
2525
message: $this->message,
2626
parameters: [
27-
'value' => $value,
28-
'name' => $this->getName()
27+
'name' => $this->getName(),
28+
'value' => $value
2929
]
3030
);
3131
}

src/StaticValidatorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ interface StaticValidatorInterface
66
{
77
public static function notBlank(?string $message = null): ChainedValidatorInterface;
88

9-
// public static function greaterThan(mixed $constraint): ChainedValidatorInterface;
9+
public static function greaterThan(mixed $constraint, ?string $message = null): ChainedValidatorInterface;
1010
}

0 commit comments

Comments
 (0)