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

Commit 751a467

Browse files
committed
chore: refactored comparable rules
1 parent 8667dcd commit 751a467

File tree

6 files changed

+93
-127
lines changed

6 files changed

+93
-127
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
4+
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedComparableException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ComparableTrait;
7+
8+
abstract class AbstractComparisonRule extends AbstractRule
9+
{
10+
use ComparableTrait;
11+
12+
public function assert(mixed $value, string $name): void
13+
{
14+
$constraint = $this->convertToComparable($this->constraint);
15+
$value = $this->convertToComparable($value);
16+
17+
if (!$this->isComparable($constraint, $value)) {
18+
throw new UnexpectedComparableException(
19+
get_debug_type($constraint),
20+
get_debug_type($value)
21+
);
22+
}
23+
24+
if (!$this->comparison($constraint, $value)) {
25+
throw new ($this->getException())(
26+
message: $this->options['message'],
27+
parameters: [
28+
'value' => $value,
29+
'name' => $name,
30+
'constraint' => $constraint
31+
]
32+
);
33+
}
34+
}
35+
36+
protected abstract function comparison(mixed $constraint, mixed $value): bool;
37+
38+
protected abstract function getException(): string;
39+
}

src/Rule/GreaterThan.php

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedComparableException;
7-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ComparableTrait;
86
use Symfony\Component\OptionsResolver\OptionsResolver;
97

10-
class GreaterThan extends AbstractRule implements RuleInterface
8+
class GreaterThan extends AbstractComparisonRule implements RuleInterface
119
{
12-
use ComparableTrait;
10+
protected array $options;
1311

14-
private array $options;
15-
16-
public function __construct(private readonly mixed $constraint, array $options = [])
12+
public function __construct(
13+
protected readonly mixed $constraint,
14+
array $options = []
15+
)
1716
{
1817
$resolver = new OptionsResolver();
1918

@@ -24,30 +23,13 @@ public function __construct(private readonly mixed $constraint, array $options =
2423
$this->options = $resolver->resolve($options);
2524
}
2625

27-
/**
28-
* @throws GreaterThanException
29-
*/
30-
public function assert(mixed $value, string $name): void
26+
protected function comparison(mixed $constraint, mixed $value): bool
27+
{
28+
return $value > $constraint;
29+
}
30+
31+
protected function getException(): string
3132
{
32-
$constraint = $this->convertToComparable($this->constraint);
33-
$value = $this->convertToComparable($value);
34-
35-
if (!$this->isComparable($constraint, $value)) {
36-
throw new UnexpectedComparableException(
37-
get_debug_type($constraint),
38-
get_debug_type($value)
39-
);
40-
}
41-
42-
if (!($value > $constraint)) {
43-
throw new GreaterThanException(
44-
message: $this->options['message'],
45-
parameters: [
46-
'value' => $value,
47-
'name' => $name,
48-
'constraint' => $constraint
49-
]
50-
);
51-
}
33+
return GreaterThanException::class;
5234
}
5335
}

src/Rule/GreaterThanOrEqual.php

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22

33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

5-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
65
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanOrEqualException;
7-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedComparableException;
8-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ComparableTrait;
96
use Symfony\Component\OptionsResolver\OptionsResolver;
107

11-
class GreaterThanOrEqual extends AbstractRule implements RuleInterface
8+
class GreaterThanOrEqual extends AbstractComparisonRule implements RuleInterface
129
{
13-
use ComparableTrait;
10+
protected array $options;
1411

15-
private array $options;
16-
17-
public function __construct(private readonly mixed $constraint, array $options = [])
12+
public function __construct(
13+
protected readonly mixed $constraint,
14+
array $options = []
15+
)
1816
{
1917
$resolver = new OptionsResolver();
2018

@@ -25,30 +23,13 @@ public function __construct(private readonly mixed $constraint, array $options =
2523
$this->options = $resolver->resolve($options);
2624
}
2725

28-
/**
29-
* @throws GreaterThanOrEqualException
30-
*/
31-
public function assert(mixed $value, string $name): void
26+
protected function comparison(mixed $constraint, mixed $value): bool
27+
{
28+
return $value >= $constraint;
29+
}
30+
31+
protected function getException(): string
3232
{
33-
$constraint = $this->convertToComparable($this->constraint);
34-
$value = $this->convertToComparable($value);
35-
36-
if (!$this->isComparable($constraint, $value)) {
37-
throw new UnexpectedComparableException(
38-
get_debug_type($constraint),
39-
get_debug_type($value)
40-
);
41-
}
42-
43-
if (!($value >= $constraint)) {
44-
throw new GreaterThanOrEqualException(
45-
message: $this->options['message'],
46-
parameters: [
47-
'value' => $value,
48-
'name' => $name,
49-
'constraint' => $constraint
50-
]
51-
);
52-
}
33+
return GreaterThanOrEqualException::class;
5334
}
5435
}

src/Rule/LessThan.php

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\LessThanException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedComparableException;
7-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ComparableTrait;
86
use Symfony\Component\OptionsResolver\OptionsResolver;
97

10-
class LessThan extends AbstractRule implements RuleInterface
8+
class LessThan extends AbstractComparisonRule implements RuleInterface
119
{
12-
use ComparableTrait;
10+
protected array $options;
1311

14-
private array $options;
15-
16-
public function __construct(private readonly mixed $constraint, array $options = [])
12+
public function __construct(
13+
protected readonly mixed $constraint,
14+
array $options = []
15+
)
1716
{
1817
$resolver = new OptionsResolver();
1918

@@ -24,30 +23,13 @@ public function __construct(private readonly mixed $constraint, array $options =
2423
$this->options = $resolver->resolve($options);
2524
}
2625

27-
/**
28-
* @throws LessThanException
29-
*/
30-
public function assert(mixed $value, string $name): void
26+
protected function comparison(mixed $constraint, mixed $value): bool
27+
{
28+
return $value < $constraint;
29+
}
30+
31+
protected function getException(): string
3132
{
32-
$constraint = $this->convertToComparable($this->constraint);
33-
$value = $this->convertToComparable($value);
34-
35-
if (!$this->isComparable($constraint, $value)) {
36-
throw new UnexpectedComparableException(
37-
get_debug_type($constraint),
38-
get_debug_type($value)
39-
);
40-
}
41-
42-
if (!($value < $constraint)) {
43-
throw new LessThanException(
44-
message: $this->options['message'],
45-
parameters: [
46-
'value' => $value,
47-
'name' => $name,
48-
'constraint' => $constraint
49-
]
50-
);
51-
}
33+
return LessThanException::class;
5234
}
5335
}

src/Rule/LessThanOrEqual.php

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\LessThanOrEqualException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedComparableException;
7-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ComparableTrait;
86
use Symfony\Component\OptionsResolver\OptionsResolver;
97

10-
class LessThanOrEqual extends AbstractRule implements RuleInterface
8+
class LessThanOrEqual extends AbstractComparisonRule implements RuleInterface
119
{
12-
use ComparableTrait;
10+
protected array $options;
1311

14-
private array $options;
15-
16-
public function __construct(private readonly mixed $constraint, array $options = [])
12+
public function __construct(
13+
protected readonly mixed $constraint,
14+
array $options = []
15+
)
1716
{
1817
$resolver = new OptionsResolver();
1918

@@ -24,30 +23,13 @@ public function __construct(private readonly mixed $constraint, array $options =
2423
$this->options = $resolver->resolve($options);
2524
}
2625

27-
/**
28-
* @throws LessThanOrEqualException
29-
*/
30-
public function assert(mixed $value, string $name): void
26+
protected function comparison(mixed $constraint, mixed $value): bool
27+
{
28+
return $value <= $constraint;
29+
}
30+
31+
protected function getException(): string
3132
{
32-
$constraint = $this->convertToComparable($this->constraint);
33-
$value = $this->convertToComparable($value);
34-
35-
if (!$this->isComparable($constraint, $value)) {
36-
throw new UnexpectedComparableException(
37-
get_debug_type($constraint),
38-
get_debug_type($value)
39-
);
40-
}
41-
42-
if (!($value <= $constraint)) {
43-
throw new LessThanOrEqualException(
44-
message: $this->options['message'],
45-
parameters: [
46-
'value' => $value,
47-
'name' => $name,
48-
'constraint' => $constraint
49-
]
50-
);
51-
}
33+
return LessThanOrEqualException::class;
5234
}
5335
}

src/Rule/Util/ComparableTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ private function isComparable(mixed $value1, mixed $value2): bool
2424
private function convertToComparable(mixed $value): mixed
2525
{
2626
if (\is_string($value)) {
27-
// If is string and has only one char,
27+
// If is string and has only one char or is empty,
2828
// return value to avoid conflicting with DateTime formats
29-
if (mb_strlen($value) === 1) {
29+
if (\mb_strlen($value) <= 1) {
3030
return $value;
3131
}
3232

0 commit comments

Comments
 (0)