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

Commit 2d16271

Browse files
committed
chore: changed rule validate to assert for coherence with validator and added bool validate
1 parent 8aaba7f commit 2d16271

7 files changed

Lines changed: 29 additions & 28 deletions

File tree

src/ChainedValidatorInterface.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@
66

77
interface ChainedValidatorInterface
88
{
9-
// --- Common ---
10-
11-
public function validate(mixed $value): bool;
12-
139
/**
1410
* @throws ValidationException
1511
*/
1612
public function assert(mixed $value, string $name): void;
1713

14+
public function validate(mixed $value): bool;
15+
1816
// --- Rules ---
1917

20-
public function notBlank(?string $message = null): ChainedValidatorInterface;
18+
public function notBlank(string $message = null): ChainedValidatorInterface;
2119

22-
public function greaterThan(mixed $constraint, ?string $message = null): ChainedValidatorInterface;
20+
public function greaterThan(mixed $constraint, string $message = null): ChainedValidatorInterface;
2321
}

src/Rule/AbstractRule.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22

33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

5-
class AbstractRule
6-
{
7-
private string $name;
8-
9-
public function getName(): string
10-
{
11-
return $this->name;
12-
}
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
136

14-
public function setName(string $name): static
7+
abstract class AbstractRule
8+
{
9+
public function validate($value): bool
1510
{
16-
$this->name = $name;
11+
try {
12+
$this->assert($value, 'null');
13+
}
14+
catch (ValidationException) {
15+
return false;
16+
}
1717

18-
return $this;
18+
return true;
1919
}
20+
21+
/**
22+
* @throws ValidationException
23+
*/
24+
public abstract function assert(mixed $value, string $name): void;
2025
}

src/Rule/GreaterThan.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __construct(
1818
/**
1919
* @throws GreaterThanException
2020
*/
21-
public function validate(mixed $value): void
21+
public function assert(mixed $value, string $name): void
2222
{
2323
if (!$this->canBeCompared($this->constraint, $value)) {
2424
throw new \LogicException(
@@ -34,7 +34,7 @@ public function validate(mixed $value): void
3434
throw new GreaterThanException(
3535
message: $this->message,
3636
parameters: [
37-
'name' => $this->getName(),
37+
'name' => $name,
3838
'constraint' => $this->constraint,
3939
'value' => $value
4040
]

src/Rule/NotBlank.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public function __construct(string $message = null)
1616
/**
1717
* @throws NotBlankException
1818
*/
19-
public function validate(mixed $value): void
19+
public function assert(mixed $value, string $name): void
2020
{
2121
// Do not allow null, false, [] and ''
2222
if ($value === false || (empty($value) && $value != '0')) {
2323
throw new NotBlankException(
2424
message: $this->message,
2525
parameters: [
26-
'name' => $this->getName(),
26+
'name' => $name,
2727
'value' => $value
2828
]
2929
);

src/Rule/RuleInterface.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ interface RuleInterface
99
/**
1010
* @throws ValidationException
1111
*/
12-
public function validate(mixed $value): void;
12+
public function assert(mixed $value, string $name): void;
1313

14-
public function getName(): string;
15-
16-
public function setName(string $name): static;
14+
public function validate(mixed $value): bool;
1715
}

src/StaticValidatorInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
interface StaticValidatorInterface
66
{
7-
public static function notBlank(?string $message = null): ChainedValidatorInterface;
7+
public static function notBlank(string $message = null): ChainedValidatorInterface;
88

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

src/Validator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __call(string $ruleName, array $arguments = []): self
4848
public function assert(mixed $value, string $name): void
4949
{
5050
foreach ($this->getRules() as $rule) {
51-
$rule->setName($name)->validate($value);
51+
$rule->assert($value, $name);
5252
}
5353
}
5454

0 commit comments

Comments
 (0)