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

Commit 8667dcd

Browse files
committed
Merge branch '1.x' into YAPV-12-create-documentation
2 parents 20969e2 + 4747923 commit 8667dcd

16 files changed

+166
-69
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Exception;
4+
5+
class UnexpectedComparableException extends UnexpectedValueException
6+
{
7+
public function __construct(string $value1, string $value2)
8+
{
9+
$message = \sprintf('Cannot compare a type "%s" with a type "%s".', $value1, $value2);
10+
11+
parent::__construct($message);
12+
}
13+
}

src/Rule/All.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\AllException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
77
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
8-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsValidatableTrait;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ValidatableTrait;
99
use Symfony\Component\OptionsResolver\OptionsResolver;
1010

1111
class All extends AbstractRule implements RuleInterface
1212
{
13-
use AssertIsValidatableTrait;
13+
use ValidatableTrait;
1414

1515
private array $options;
1616

@@ -33,7 +33,11 @@ public function __construct(
3333

3434
public function assert(mixed $value, string $name): void
3535
{
36-
$this->assertIsValidatable($this->constraints);
36+
if (!$this->isValidatable($this->constraints)) {
37+
throw new UnexpectedValueException(
38+
'All constraints must be of type "RuleInterface".'
39+
);
40+
}
3741

3842
if (!\is_array($value)) {
3943
throw new UnexpectedValueException(

src/Rule/GreaterThan.php

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

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsComparableTrait;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedComparableException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ComparableTrait;
78
use Symfony\Component\OptionsResolver\OptionsResolver;
89

910
class GreaterThan extends AbstractRule implements RuleInterface
1011
{
11-
use AssertIsComparableTrait;
12+
use ComparableTrait;
1213

1314
private array $options;
1415

@@ -28,15 +29,23 @@ public function __construct(private readonly mixed $constraint, array $options =
2829
*/
2930
public function assert(mixed $value, string $name): void
3031
{
31-
$this->assertIsComparable($this->constraint, $value);
32+
$constraint = $this->convertToComparable($this->constraint);
33+
$value = $this->convertToComparable($value);
3234

33-
if (!($value > $this->constraint)) {
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)) {
3443
throw new GreaterThanException(
3544
message: $this->options['message'],
3645
parameters: [
3746
'value' => $value,
3847
'name' => $name,
39-
'constraint' => $this->constraint
48+
'constraint' => $constraint
4049
]
4150
);
4251
}

src/Rule/GreaterThanOrEqual.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Exception\GreaterThanOrEqualException;
7-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsComparableTrait;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedComparableException;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ComparableTrait;
89
use Symfony\Component\OptionsResolver\OptionsResolver;
910

1011
class GreaterThanOrEqual extends AbstractRule implements RuleInterface
1112
{
12-
use AssertIsComparableTrait;
13+
use ComparableTrait;
1314

1415
private array $options;
1516

@@ -29,15 +30,23 @@ public function __construct(private readonly mixed $constraint, array $options =
2930
*/
3031
public function assert(mixed $value, string $name): void
3132
{
32-
$this->assertIsComparable($this->constraint, $value);
33+
$constraint = $this->convertToComparable($this->constraint);
34+
$value = $this->convertToComparable($value);
3335

34-
if (!($value >= $this->constraint)) {
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)) {
3544
throw new GreaterThanOrEqualException(
3645
message: $this->options['message'],
3746
parameters: [
3847
'value' => $value,
3948
'name' => $name,
40-
'constraint' => $this->constraint
49+
'constraint' => $constraint
4150
]
4251
);
4352
}

src/Rule/LessThan.php

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

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\LessThanException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsComparableTrait;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedComparableException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ComparableTrait;
78
use Symfony\Component\OptionsResolver\OptionsResolver;
89

910
class LessThan extends AbstractRule implements RuleInterface
1011
{
11-
use AssertIsComparableTrait;
12+
use ComparableTrait;
1213

1314
private array $options;
1415

@@ -28,15 +29,23 @@ public function __construct(private readonly mixed $constraint, array $options =
2829
*/
2930
public function assert(mixed $value, string $name): void
3031
{
31-
$this->assertIsComparable($this->constraint, $value);
32+
$constraint = $this->convertToComparable($this->constraint);
33+
$value = $this->convertToComparable($value);
3234

33-
if (!($value < $this->constraint)) {
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)) {
3443
throw new LessThanException(
3544
message: $this->options['message'],
3645
parameters: [
3746
'value' => $value,
3847
'name' => $name,
39-
'constraint' => $this->constraint
48+
'constraint' => $constraint
4049
]
4150
);
4251
}

src/Rule/LessThanOrEqual.php

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

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\LessThanOrEqualException;
6-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsComparableTrait;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedComparableException;
7+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ComparableTrait;
78
use Symfony\Component\OptionsResolver\OptionsResolver;
89

910
class LessThanOrEqual extends AbstractRule implements RuleInterface
1011
{
11-
use AssertIsComparableTrait;
12+
use ComparableTrait;
1213

1314
private array $options;
1415

@@ -28,15 +29,23 @@ public function __construct(private readonly mixed $constraint, array $options =
2829
*/
2930
public function assert(mixed $value, string $name): void
3031
{
31-
$this->assertIsComparable($this->constraint, $value);
32+
$constraint = $this->convertToComparable($this->constraint);
33+
$value = $this->convertToComparable($value);
3234

33-
if (!($value <= $this->constraint)) {
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)) {
3443
throw new LessThanOrEqualException(
3544
message: $this->options['message'],
3645
parameters: [
3746
'value' => $value,
3847
'name' => $name,
39-
'constraint' => $this->constraint
48+
'constraint' => $constraint
4049
]
4150
);
4251
}

src/Rule/Range.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\RangeException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedComparableException;
67
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
7-
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\AssertIsComparableTrait;
8+
use ProgrammatorDev\YetAnotherPhpValidator\Rule\Util\ComparableTrait;
89
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
910
use Symfony\Component\OptionsResolver\OptionsResolver;
1011

1112
class Range extends AbstractRule implements RuleInterface
1213
{
13-
use AssertIsComparableTrait;
14+
use ComparableTrait;
1415

1516
private array $options;
1617

@@ -31,12 +32,21 @@ public function __construct(
3132

3233
public function assert(mixed $value, string $name): void
3334
{
34-
$this->assertIsComparable($this->minConstraint, $this->maxConstraint);
35+
$minConstraint = $this->convertToComparable($this->minConstraint);
36+
$maxConstraint = $this->convertToComparable($this->maxConstraint);
37+
$value = $this->convertToComparable($value);
38+
39+
if (!$this->isComparable($minConstraint, $maxConstraint)) {
40+
throw new UnexpectedComparableException(
41+
get_debug_type($minConstraint),
42+
get_debug_type($maxConstraint)
43+
);
44+
}
3545

3646
if (
3747
!Validator
38-
::greaterThan($this->minConstraint)
39-
->validate($this->maxConstraint)
48+
::greaterThan($minConstraint)
49+
->validate($maxConstraint)
4050
) {
4151
throw new UnexpectedValueException(
4252
'Max constraint value must be greater than min constraint value.'
@@ -45,17 +55,17 @@ public function assert(mixed $value, string $name): void
4555

4656
if (
4757
!Validator
48-
::greaterThanOrEqual($this->minConstraint)
49-
->lessThanOrEqual($this->maxConstraint)
58+
::greaterThanOrEqual($minConstraint)
59+
->lessThanOrEqual($maxConstraint)
5060
->validate($value)
5161
) {
5262
throw new RangeException(
5363
message: $this->options['message'],
5464
parameters: [
5565
'value' => $value,
5666
'name' => $name,
57-
'minConstraint' => $this->minConstraint,
58-
'maxConstraint' => $this->maxConstraint
67+
'minConstraint' => $minConstraint,
68+
'maxConstraint' => $maxConstraint
5969
]
6070
);
6171
}

src/Rule/Util/AssertIsComparableTrait.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Rule/Util/ComparableTrait.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Rule\Util;
4+
5+
trait ComparableTrait
6+
{
7+
private function isComparable(mixed $value1, mixed $value2): bool
8+
{
9+
if ($value1 instanceof \DateTimeInterface && $value2 instanceof \DateTimeInterface) {
10+
return true;
11+
}
12+
13+
if (\is_numeric($value1) && \is_numeric($value2)) {
14+
return true;
15+
}
16+
17+
if (\is_string($value1) && \is_string($value2)) {
18+
return true;
19+
}
20+
21+
return false;
22+
}
23+
24+
private function convertToComparable(mixed $value): mixed
25+
{
26+
if (\is_string($value)) {
27+
// If is string and has only one char,
28+
// return value to avoid conflicting with DateTime formats
29+
if (mb_strlen($value) === 1) {
30+
return $value;
31+
}
32+
33+
// Guess if is a DateTime string and convert
34+
// (like "yesterday" or "1985-07-19")
35+
try {
36+
return new \DateTimeImmutable($value);
37+
}
38+
catch (\Exception) {
39+
return $value;
40+
}
41+
}
42+
43+
return $value;
44+
}
45+
}

src/Rule/Util/AssertIsValidatableTrait.php renamed to src/Rule/Util/ValidatableTrait.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
77

8-
trait AssertIsValidatableTrait
8+
trait ValidatableTrait
99
{
10-
private function assertIsValidatable(array $constraints): bool
10+
private function isValidatable(array $constraints): bool
1111
{
1212
foreach ($constraints as $constraint) {
1313
if (!$constraint instanceof RuleInterface) {
14-
throw new UnexpectedValueException(
15-
\sprintf('Expected constraint of type "RuleInterface", "%s" given.', get_debug_type($constraint))
16-
);
14+
return false;
1715
}
1816
}
1917

0 commit comments

Comments
 (0)