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

Commit fd10fad

Browse files
authored
Merge pull request #3 from programmatordev/YAPV-10-create-greaterthan-rule
Create GreaterThan rule
2 parents 59df963 + 97fa94d commit fd10fad

18 files changed

Lines changed: 289 additions & 98 deletions

src/ChainedValidatorInterface.php

Lines changed: 5 additions & 7 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 $input): bool;
12-
139
/**
1410
* @throws ValidationException
1511
*/
16-
public function assert(mixed $input, string $name): void;
12+
public function assert(mixed $value, string $name): void;
13+
14+
public function validate(mixed $value): bool;
1715

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): ChainedValidatorInterface;
20+
public function greaterThan(mixed $constraint, string $message = null): ChainedValidatorInterface;
2321
}
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/Exception/InvalidRuleException.php

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Exception;
4+
5+
class RuleNotFoundException extends \Exception
6+
{
7+
protected $message = 'Rule does not exist.';
8+
}

src/Exception/ValidationException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private function formatValue(mixed $value): string
3838
}
3939

4040
if (\is_string($value)) {
41-
return \sprintf('"%s"', $value);
41+
return $value;
4242
}
4343

4444
if (\is_resource($value)) {
@@ -66,6 +66,6 @@ private function formatValues(array $values): string
6666
$values[$key] = $this->formatValue($value);
6767
}
6868

69-
return \implode(', ', $values);
69+
return \sprintf('[%s]', \implode(', ', $values));
7070
}
7171
}

src/Factory.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22

33
namespace ProgrammatorDev\YetAnotherPhpValidator;
44

5-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\InvalidRuleException;
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\RuleNotFoundException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
77

88
class Factory
99
{
1010
private string $namespace = 'ProgrammatorDev\\YetAnotherPhpValidator\\Rule';
1111

1212
/**
13-
* @throws InvalidRuleException
13+
* @throws RuleNotFoundException
1414
*/
1515
public function createRule(string $ruleName, array $arguments = []): RuleInterface
1616
{
17-
try {
18-
$className = \sprintf('%s\\%s', $this->namespace, \ucfirst($ruleName));
19-
return new $className(...$arguments);
20-
}
21-
catch (\Error) {
22-
throw new InvalidRuleException(
17+
$className = \sprintf('%s\\%s', $this->namespace, \ucfirst($ruleName));
18+
19+
if (!class_exists($className)) {
20+
throw new RuleNotFoundException(
2321
\sprintf('"%s" rule does not exist.', $ruleName)
2422
);
2523
}
24+
25+
return new $className(...$arguments);
2626
}
2727
}

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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertComparableTrait;
7+
8+
class GreaterThan extends AbstractRule implements RuleInterface
9+
{
10+
use AssertComparableTrait;
11+
12+
private string $message;
13+
14+
public function __construct(
15+
private readonly mixed $constraint,
16+
string $message = null
17+
)
18+
{
19+
$this->message = $message ?? 'The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.';
20+
}
21+
22+
/**
23+
* @throws GreaterThanException
24+
*/
25+
public function assert(mixed $value, string $name): void
26+
{
27+
// Assert if constraint and value can be compared
28+
$this->assertComparable($this->constraint, $value, GreaterThanException::class);
29+
30+
if (!($value > $this->constraint)) {
31+
throw new GreaterThanException(
32+
message: $this->message,
33+
parameters: [
34+
'name' => $name,
35+
'constraint' => $this->constraint,
36+
'value' => $value
37+
]
38+
);
39+
}
40+
}
41+
}

src/Rule/NotBlank.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,25 @@
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.';
13+
$this->message = $message ?? 'The "{{ name }}" value should not be blank, "{{ value }}" given.';
1214
}
1315

1416
/**
1517
* @throws NotBlankException
1618
*/
17-
public function validate(mixed $input): void
19+
public function assert(mixed $value, string $name): void
1820
{
19-
// Strip whitespace in case of a string
20-
if (\is_string($input)) {
21-
$input = trim($input);
22-
}
23-
2421
// Do not allow null, false, [] and ''
25-
if ($input === false || (empty($input) && $input != '0')) {
22+
if ($value === false || (empty($value) && $value != '0')) {
2623
throw new NotBlankException(
2724
message: $this->message,
2825
parameters: [
29-
'input' => $input,
30-
'name' => $this->getName()
26+
'name' => $name,
27+
'value' => $value
3128
]
3229
);
3330
}

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 $input): 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
}

0 commit comments

Comments
 (0)