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

Commit 1818f7e

Browse files
authored
Merge pull request #69 from programmatordev/YAPV-54-create-blank-rule
Create Blank rule
2 parents d2a1175 + 41a2a32 commit 1818f7e

11 files changed

+174
-28
lines changed

README.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,12 @@ PHP validator with expressive error messages.
1212

1313
## Installation
1414

15-
You can install the library via [Composer](https://getcomposer.org/):
15+
Install the library via [Composer](https://getcomposer.org/):
1616

1717
```bash
1818
composer require programmatordev/yet-another-php-validator
1919
```
2020

21-
To use the library, use Composer's [autoload](https://getcomposer.org/doc/01-basic-usage.md#autoloading):
22-
23-
```php
24-
require_once 'vendor/autoload.php';
25-
```
26-
2721
## Basic Usage
2822

2923
Simple usage looks like:

docs/01-get-started.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,12 @@
1010

1111
## Installation
1212

13-
You can install the library via [Composer](https://getcomposer.org/):
13+
Install the library via [Composer](https://getcomposer.org/):
1414

1515
```bash
1616
composer require programmatordev/yet-another-php-validator
1717
```
1818

19-
To use the library, use Composer's [autoload](https://getcomposer.org/doc/01-basic-usage.md#autoloading):
20-
21-
```php
22-
require_once 'vendor/autoload.php';
23-
```
24-
2519
## Basic Usage
2620

2721
Simple usage looks like:

docs/03-rules.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
## Basic Rules
1212

13+
- [Blank](03-rules_blank.md)
1314
- [Count](03-rules_count.md)
1415
- [NotBlank](03-rules_not-blank.md)
1516
- [Type](03-rules_type.md)

docs/03-rules_blank.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Blank
2+
3+
Validates that a value is equal to an empty string, empty array, `false` or `null`.
4+
5+
Check the [NotBlank](03-rules_not-blank.md) rule for the opposite validation.
6+
7+
```php
8+
Blank(
9+
?callable $normalizer = null,
10+
?string $message = null
11+
);
12+
```
13+
14+
## Basic Usage
15+
16+
Bellow are the *only* cases where the rule will succeed by default,
17+
everything else is considered invalid (you may want to check the [`normalizer`](#normalizer) option for a different behaviour):
18+
19+
```php
20+
Validator::blank()->validate(''); // true
21+
Validator::blank()->validate([]); // true
22+
Validator::blank()->validate(false); // true
23+
Validator::blank()->validate(null); // true
24+
```
25+
26+
## Options
27+
28+
### `normalizer`
29+
30+
type: `?callable` default: `null`
31+
32+
Allows to define a `callable` that will be applied to the value before checking if it is valid.
33+
34+
For example, use `trim`, or pass your own function, to allow a string with whitespaces only:
35+
36+
```php
37+
Validator::blank(normalizer: 'trim')->validate(' '); // true
38+
Validator::blank(normalizer: fn($value) => trim($value))->validate(' '); // true
39+
```
40+
41+
### `message`
42+
43+
type: `?string` default: `The {{ name }} value should be blank, {{ value }} given.`
44+
45+
Message that will be shown if the value is not blank.
46+
47+
The following parameters are available:
48+
49+
| Parameter | Description |
50+
|---------------|---------------------------|
51+
| `{{ value }}` | The current invalid value |
52+
| `{{ name }}` | Name of the invalid value |
53+
54+
## Changelog
55+
56+
- `1.2.0` Created

docs/03-rules_not-blank.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# NotBlank
22

3-
Validates that a value is not equal to a blank string, blank array, `false` or `null`.
3+
Validates that a value is not equal to an empty string, empty array, `false` or `null`.
4+
5+
Check the [Blank](03-rules_blank.md) rule for the opposite validation.
46

57
```php
68
NotBlank(
@@ -25,17 +27,14 @@ Validator::notBlank()->validate(null); // false
2527

2628
### `normalizer`
2729

28-
type: `callable` default: `null`
30+
type: `?callable` default: `null`
2931

3032
Allows to define a `callable` that will be applied to the value before checking if it is valid.
3133

3234
For example, use `trim`, or pass your own function, to not allow a string with whitespaces only:
3335

3436
```php
35-
// Existing PHP callables
3637
Validator::notBlank(normalizer: 'trim')->validate(' '); // false
37-
38-
// Function
3938
Validator::notBlank(normalizer: fn($value) => trim($value))->validate(' '); // false
4039
```
4140

src/ChainedValidatorInterface.php

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
interface ChainedValidatorInterface
99
{
10+
public function blank(
11+
?callable $normalizer = null,
12+
?string $message = null
13+
): ChainedValidatorInterface&Validator;
14+
1015
public function choice(
1116
array $constraints,
1217
bool $multiple = false,

src/Exception/BlankException.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Validator\Exception;
4+
5+
class BlankException extends ValidationException {}

src/Rule/Blank.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Validator\Rule;
4+
5+
use ProgrammatorDev\Validator\Exception\BlankException;
6+
7+
class Blank extends AbstractRule implements RuleInterface
8+
{
9+
/** @var ?callable */
10+
private $normalizer;
11+
private string $message = 'The {{ name }} value should be blank, {{ value }} given.';
12+
13+
public function __construct(
14+
?callable $normalizer = null,
15+
?string $message = null
16+
)
17+
{
18+
$this->normalizer = $normalizer;
19+
$this->message = $message ?? $this->message;
20+
}
21+
22+
public function assert(mixed $value, ?string $name = null): void
23+
{
24+
if ($this->normalizer !== null) {
25+
$value = ($this->normalizer)($value);
26+
}
27+
28+
if ($value !== null && $value !== false && $value !== '' && $value !== []) {
29+
throw new BlankException(
30+
message: $this->message,
31+
parameters: [
32+
'value' => $value,
33+
'name' => $name
34+
]
35+
);
36+
}
37+
}
38+
}

src/Rule/NotBlank.php

+2-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ProgrammatorDev\Validator\Rule;
44

55
use ProgrammatorDev\Validator\Exception\NotBlankException;
6+
use ProgrammatorDev\Validator\Validator;
67

78
class NotBlank extends AbstractRule implements RuleInterface
89
{
@@ -19,17 +20,9 @@ public function __construct(
1920
$this->message = $message ?? $this->message;
2021
}
2122

22-
/**
23-
* @throws NotBlankException
24-
*/
2523
public function assert(mixed $value, ?string $name = null): void
2624
{
27-
if ($this->normalizer !== null) {
28-
$value = ($this->normalizer)($value);
29-
}
30-
31-
// Do not allow null, false, [] and ''
32-
if ($value === false || (empty($value) && $value != '0')) {
25+
if (Validator::blank(normalizer: $this->normalizer)->validate($value) === true) {
3326
throw new NotBlankException(
3427
message: $this->message,
3528
parameters: [

src/StaticValidatorInterface.php

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
interface StaticValidatorInterface
88
{
9+
public static function blank(
10+
?callable $normalizer = null,
11+
?string $message = null
12+
): ChainedValidatorInterface&Validator;
13+
914
public static function choice(
1015
array $constraints,
1116
bool $multiple = false,

tests/BlankTest.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Validator\Test;
4+
5+
use ProgrammatorDev\Validator\Exception\BlankException;
6+
use ProgrammatorDev\Validator\Rule\Blank;
7+
use ProgrammatorDev\Validator\Test\Util\TestRuleMessageOptionTrait;
8+
use ProgrammatorDev\Validator\Test\Util\TestRuleFailureConditionTrait;
9+
use ProgrammatorDev\Validator\Test\Util\TestRuleSuccessConditionTrait;
10+
11+
class BlankTest extends AbstractTest
12+
{
13+
use TestRuleFailureConditionTrait;
14+
use TestRuleSuccessConditionTrait;
15+
use TestRuleMessageOptionTrait;
16+
17+
public static function provideRuleFailureConditionData(): \Generator
18+
{
19+
$exception = BlankException::class;
20+
$message = '/The (.*) value should be blank, (.*) given\./';
21+
22+
yield 'true' => [new Blank(), true, $exception, $message];
23+
24+
yield 'string' => [new Blank(), 'string', $exception, $message];
25+
yield 'whitespace string' => [new Blank(), ' ', $exception, $message];
26+
yield 'zero string' => [new Blank(), '0', $exception, $message];
27+
28+
yield 'array' => [new Blank(), ['string'], $exception, $message];
29+
yield 'blank string array' => [new Blank(), [''], $exception, $message];
30+
yield 'whitespace array' => [new Blank(), [' '], $exception, $message];
31+
yield 'zero array' => [new Blank(), [0], $exception, $message];
32+
33+
yield 'number' => [new Blank(), 10, $exception, $message];
34+
yield 'zero number' => [new Blank(), 0, $exception, $message];
35+
}
36+
37+
public static function provideRuleSuccessConditionData(): \Generator
38+
{
39+
yield 'null' => [new Blank(), null];
40+
yield 'false' => [new Blank(), false];
41+
yield 'blank string' => [new Blank(), ''];
42+
yield 'blank array' => [new Blank(), []];
43+
44+
yield 'normalizer whitespace' => [new Blank(normalizer: 'trim'), ' '];
45+
yield 'normalizer whitespace function' => [new Blank(normalizer: fn($value) => trim($value)), ' '];
46+
}
47+
48+
public static function provideRuleMessageOptionData(): \Generator
49+
{
50+
yield 'message' => [
51+
new Blank(message: '{{ name }} | {{ value }}'),
52+
'string',
53+
'test | "string"'
54+
];
55+
}
56+
}

0 commit comments

Comments
 (0)