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

Commit aeaa911

Browse files
authored
Merge pull request #16 from programmatordev/YAPV-12-create-documentation
Create documentation (in progress)
2 parents 4747923 + e8c905c commit aeaa911

12 files changed

+160
-139
lines changed

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
1-
# yet-another-php-validator
1+
# Yet Another PHP Validator
2+
3+
Focused on validating development code with expressive error messages.
4+
5+
## Requirements
6+
7+
- PHP 8.1 or higher.
8+
9+
## Installation
10+
11+
You can install the library via [Composer](https://getcomposer.org/):
12+
13+
```bash
14+
composer require programmatordev/yet-another-php-validator
15+
```
16+
17+
To use the library, use Composer's [autoload](https://getcomposer.org/doc/01-basic-usage.md#autoloading):
18+
19+
```php
20+
require_once 'vendor/autoload.php';
21+
```
22+
23+
## Basic Usage
24+
25+
Simple usage looks like:
26+
27+
```php
28+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
29+
30+
$ageValidator = Validator::notBlank()->greaterThanOrEqual(18);
31+
32+
$ageValidator->validate(16); // returns false
33+
$ageValidator->assert(16, 'Age'); // throws exception: The "Age" value should be greater than or equal to "18", "16" given.
34+
```
35+
36+
## Documentation
37+
38+
39+
## Contributing
40+
41+
Any form of contribution to improve this library will be welcome and appreciated.
42+
Make sure to open a pull request or issue.
43+
44+
## Acknowledgments
45+
46+
This library is inspired by [Respect's Validation](https://github.com/Respect/Validation) and [Symfony's Validator](https://symfony.com/doc/current/validation.html).
47+
48+
## License
49+
50+
This project is licensed under the MIT license.
51+
Please see the [LICENSE](LICENSE) file distributed with this source code for further information regarding copyright and licensing.
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/Range.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public function assert(mixed $value, string $name): void
4444
}
4545

4646
if (
47-
!Validator
48-
::greaterThan($minConstraint)
47+
!Validator::greaterThan($minConstraint)
4948
->validate($maxConstraint)
5049
) {
5150
throw new UnexpectedValueException(
@@ -54,8 +53,7 @@ public function assert(mixed $value, string $name): void
5453
}
5554

5655
if (
57-
!Validator
58-
::greaterThanOrEqual($minConstraint)
56+
!Validator::greaterThanOrEqual($minConstraint)
5957
->lessThanOrEqual($maxConstraint)
6058
->validate($value)
6159
) {

src/Rule/Util/ComparableTrait.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,10 @@ private function isComparable(mixed $value1, mixed $value2): bool
2323

2424
private function convertToComparable(mixed $value): mixed
2525
{
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")
26+
// Try to guess if it is a valid datetime format,
27+
// like "yesterday" or "1985-07-19"
28+
// https://www.php.net/manual/en/datetime.formats.php
29+
if (\is_string($value) && \mb_strlen($value) > 1) {
3530
try {
3631
return new \DateTimeImmutable($value);
3732
}

0 commit comments

Comments
 (0)