Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions docs/06-concrete-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ or magic methods. We'll use a traditional dependency injection approach.
```php
use Respect\Validation\Validator as v;

$usernameValidator = v::alnum()->noWhitespace()->length(1, 15);
$usernameValidator = v::alnum()->notSpaced()->length(1, 15);
$usernameValidator->isValid('alganet'); // true
```

If you `var_dump($usernameValidator)`, you'll see a composite of objects with
`Respect\Validation\Rules\Alnum`, `Respect\Validation\Rules\NoWhitespace` and
`Respect\Validation\Rules\Alnum`, `Respect\Validation\Rules\Spaced` wrapped in `Respect\Validation\Rules\Not` and
`Respect\Validation\Rules\Length`. There is a specific object for each rule, and
the chain only builds the structure. You can build it by yourself:

```php
use Respect\Validation\Rules;
use Respect\Validation\Validator;

$usernameValidator = new Rules\AllOf(
$usernameValidator = Validator::create(
new Rules\Alnum(),
new Rules\NoWhitespace(),
new Rules\Length(1, 15)
new Rules\Not(
new Rules\Spaced(),
),
new Rules\Length(1, 15),
);
$usernameValidator->isValid('alganet'); // true
```
Expand All @@ -33,12 +36,18 @@ container or test it in the way you want. Nesting is still possible:
```php
use Respect\Validation\Rules;

$usernameValidator = new Rules\AllOf(
new Rules\Alnum(),
new Rules\NoWhitespace(),
new Rules\Length(1, 15)
$usernameValidator = Validator::create(
new Rules\Key(
'name',
new Rules\AllOf(
new Rules\Alnum(),
new Rules\Not(
new Rules\Spaced(),
),
new Rules\Length(1, 15),
)
)
);
$userValidator = new Rules\Key('name', $usernameValidator);
$userValidator->isValid(['name' => 'alganet']); // true
```

Expand All @@ -63,7 +72,7 @@ something complex and returns for you.

> I really don't like static calls, can I avoid it?

Yes. Just use `$validator = new Validator();` each time you want a new validator,
Yes. Just use `$validator = Validator::create();` each time you want a new validator,
and continue from there.

> Do you have a static method for each rule?
Expand Down
4 changes: 2 additions & 2 deletions docs/09-list-of-rules-by-category.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@
- [Json](rules/Json.md)
- [Lowercase](rules/Lowercase.md)
- [NotEmoji](rules/NotEmoji.md)
- [NoWhitespace](rules/NoWhitespace.md)
- [Phone](rules/Phone.md)
- [PhpLabel](rules/PhpLabel.md)
- [PostalCode](rules/PostalCode.md)
Expand All @@ -246,6 +245,7 @@
- [Slug](rules/Slug.md)
- [Sorted](rules/Sorted.md)
- [Space](rules/Space.md)
- [Spaced](rules/Spaced.md)
- [StartsWith](rules/StartsWith.md)
- [StringType](rules/StringType.md)
- [StringVal](rules/StringVal.md)
Expand Down Expand Up @@ -406,7 +406,6 @@
- [Not](rules/Not.md)
- [NotEmoji](rules/NotEmoji.md)
- [NotEmpty](rules/NotEmpty.md)
- [NoWhitespace](rules/NoWhitespace.md)
- [NullOr](rules/NullOr.md)
- [NullType](rules/NullType.md)
- [Number](rules/Number.md)
Expand Down Expand Up @@ -439,6 +438,7 @@
- [Slug](rules/Slug.md)
- [Sorted](rules/Sorted.md)
- [Space](rules/Space.md)
- [Spaced](rules/Spaced.md)
- [StartsWith](rules/StartsWith.md)
- [StringType](rules/StringType.md)
- [StringVal](rules/StringVal.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/Alnum.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ See also:
- [Digit](Digit.md)
- [Lowercase](Lowercase.md)
- [NotEmoji](NotEmoji.md)
- [NoWhitespace](NoWhitespace.md)
- [Regex](Regex.md)
- [Spaced](Spaced.md)
- [StringType](StringType.md)
- [StringVal](StringVal.md)
- [Uppercase](Uppercase.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/Alpha.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ See also:
- [Digit](Digit.md)
- [Lowercase](Lowercase.md)
- [NotEmoji](NotEmoji.md)
- [NoWhitespace](NoWhitespace.md)
- [Regex](Regex.md)
- [Spaced](Spaced.md)
- [Uppercase](Uppercase.md)
- [Vowel](Vowel.md)
2 changes: 1 addition & 1 deletion docs/rules/Blank.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ It's similar to [NotEmpty](NotEmpty.md), but way stricter.
See also:

- [NotEmpty](NotEmpty.md)
- [NoWhitespace](NoWhitespace.md)
- [NullType](NullType.md)
- [Number](Number.md)
- [Spaced](Spaced.md)
- [Undef](Undef.md)
- [UndefOr](UndefOr.md)
4 changes: 2 additions & 2 deletions docs/rules/CreditCard.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The current supported brands are:
- RuPay (`'RuPay'` or `CreditCard::RUPAY`)

It ignores any non-numeric characters, use [Digit](Digit.md),
[NoWhitespace](NoWhitespace.md), or [Regex](Regex.md) when appropriate.
[Spaced](Spaced.md), or [Regex](Regex.md) when appropriate.

```php
v::digit()->creditCard()->isValid('5376747397208720'); // true
Expand Down Expand Up @@ -79,5 +79,5 @@ See also:
- [Digit](Digit.md)
- [Iban](Iban.md)
- [Luhn](Luhn.md)
- [NoWhitespace](NoWhitespace.md)
- [Regex](Regex.md)
- [Spaced](Spaced.md)
49 changes: 0 additions & 49 deletions docs/rules/NoWhitespace.md

This file was deleted.

4 changes: 2 additions & 2 deletions docs/rules/NotEmpty.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- `NotEmpty()`

Validates wether the given input is not empty. This function also takes whitespace
into account, use `noWhitespace()` if no spaces or linebreaks and other
into account, use `notSpaced()` if no spaces or linebreaks and other
whitespace anywhere in the input is desired.

```php
Expand Down Expand Up @@ -68,8 +68,8 @@ See also:
- [Each](Each.md)
- [Max](Max.md)
- [Min](Min.md)
- [NoWhitespace](NoWhitespace.md)
- [NullType](NullType.md)
- [Number](Number.md)
- [Spaced](Spaced.md)
- [Undef](Undef.md)
- [UndefOr](UndefOr.md)
55 changes: 55 additions & 0 deletions docs/rules/Spaced.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Spaced

- `Spaced()`

Validates if a string contains at least one whitespace (spaces, tabs, or line breaks);

```php
v::spaced()->isValid('foo bar'); // true
v::spaced()->isValid("foo\nbar"); // true
```

This is most useful when inverting the validator as `notSpaced()`, and chaining with other validators such as [Alnum](Alnum.md) or [Alpha](Alpha.md) to ensure that a string contains no whitespace characters:

```php
v::notSpaced()->alnum()->isValid('username'); // true
v::notSpaced()->alnum()->isValid('user name'); // false
```

## Templates

### `Spaced::TEMPLATE_STANDARD`

| Mode | Template |
| ---------- | ------------------------------------------------ |
| `default` | {{subject}} must contain at least one whitespace |
| `inverted` | {{subject}} must not contain whitespaces |

## Template placeholders

| Placeholder | Description |
| ----------- | ---------------------------------------------------------------- |
| `subject` | The validated input or the custom validator name (if specified). |

## Categorization

- Strings

## Changelog

| Version | Description |
| ------: | -------------------------------------------- |
| 3.0.0 | Renamed to `Spaced` and changed the behavior |
| 0.3.9 | Created as `NoWhitespace` |

---

See also:

- [Alnum](Alnum.md)
- [Alpha](Alpha.md)
- [Blank](Blank.md)
- [CreditCard](CreditCard.md)
- [NotEmpty](NotEmpty.md)
- [Undef](Undef.md)
- [UndefOr](UndefOr.md)
2 changes: 1 addition & 1 deletion docs/rules/Undef.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ See also:

- [Blank](Blank.md)
- [NotEmpty](NotEmpty.md)
- [NoWhitespace](NoWhitespace.md)
- [NullType](NullType.md)
- [Number](Number.md)
- [Spaced](Spaced.md)
- [UndefOr](UndefOr.md)
2 changes: 1 addition & 1 deletion docs/rules/UndefOr.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ See also:

- [Blank](Blank.md)
- [NotEmpty](NotEmpty.md)
- [NoWhitespace](NoWhitespace.md)
- [NullOr](NullOr.md)
- [NullType](NullType.md)
- [Spaced](Spaced.md)
- [Undef](Undef.md)
4 changes: 2 additions & 2 deletions library/Mixins/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@ public static function nip(): Chain;

public static function no(bool $useLocale = false): Chain;

public static function noWhitespace(): Chain;

public static function noneOf(Rule $rule1, Rule $rule2, Rule ...$rules): Chain;

public static function not(Rule $rule): Chain;
Expand Down Expand Up @@ -313,6 +311,8 @@ public static function sorted(string $direction): Chain;

public static function space(string ...$additionalChars): Chain;

public static function spaced(): Chain;

public static function startsWith(mixed $startValue, bool $identical = false): Chain;

public static function stringType(): Chain;
Expand Down
4 changes: 2 additions & 2 deletions library/Mixins/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,6 @@ public function nip(): Chain;

public function no(bool $useLocale = false): Chain;

public function noWhitespace(): Chain;

public function noneOf(Rule $rule1, Rule $rule2, Rule ...$rules): Chain;

public function not(Rule $rule): Chain;
Expand Down Expand Up @@ -316,6 +314,8 @@ public function sorted(string $direction): Chain;

public function space(string ...$additionalChars): Chain;

public function spaced(): Chain;

public function startsWith(mixed $startValue, bool $identical = false): Chain;

public function stringType(): Chain;
Expand Down
4 changes: 2 additions & 2 deletions library/Mixins/KeyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@ public static function keyNip(int|string $key): Chain;

public static function keyNo(int|string $key, bool $useLocale = false): Chain;

public static function keyNoWhitespace(int|string $key): Chain;

public static function keyNoneOf(int|string $key, Rule $rule1, Rule $rule2, Rule ...$rules): Chain;

public static function keyNot(int|string $key, Rule $rule): Chain;
Expand Down Expand Up @@ -284,6 +282,8 @@ public static function keySorted(int|string $key, string $direction): Chain;

public static function keySpace(int|string $key, string ...$additionalChars): Chain;

public static function keySpaced(int|string $key): Chain;

public static function keyStartsWith(int|string $key, mixed $startValue, bool $identical = false): Chain;

public static function keyStringType(int|string $key): Chain;
Expand Down
4 changes: 2 additions & 2 deletions library/Mixins/KeyChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@ public function keyNip(int|string $key): Chain;

public function keyNo(int|string $key, bool $useLocale = false): Chain;

public function keyNoWhitespace(int|string $key): Chain;

public function keyNoneOf(int|string $key, Rule $rule1, Rule $rule2, Rule ...$rules): Chain;

public function keyNot(int|string $key, Rule $rule): Chain;
Expand Down Expand Up @@ -284,6 +282,8 @@ public function keySorted(int|string $key, string $direction): Chain;

public function keySpace(int|string $key, string ...$additionalChars): Chain;

public function keySpaced(int|string $key): Chain;

public function keyStartsWith(int|string $key, mixed $startValue, bool $identical = false): Chain;

public function keyStringType(int|string $key): Chain;
Expand Down
4 changes: 2 additions & 2 deletions library/Mixins/NotBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ public static function notNip(): Chain;

public static function notNo(bool $useLocale = false): Chain;

public static function notNoWhitespace(): Chain;

public static function notNoneOf(Rule $rule1, Rule $rule2, Rule ...$rules): Chain;

public static function notNullType(): Chain;
Expand Down Expand Up @@ -292,6 +290,8 @@ public static function notSorted(string $direction): Chain;

public static function notSpace(string ...$additionalChars): Chain;

public static function notSpaced(): Chain;

public static function notStartsWith(mixed $startValue, bool $identical = false): Chain;

public static function notStringType(): Chain;
Expand Down
4 changes: 2 additions & 2 deletions library/Mixins/NotChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ public function notNip(): Chain;

public function notNo(bool $useLocale = false): Chain;

public function notNoWhitespace(): Chain;

public function notNoneOf(Rule $rule1, Rule $rule2, Rule ...$rules): Chain;

public function notNullType(): Chain;
Expand Down Expand Up @@ -292,6 +290,8 @@ public function notSorted(string $direction): Chain;

public function notSpace(string ...$additionalChars): Chain;

public function notSpaced(): Chain;

public function notStartsWith(mixed $startValue, bool $identical = false): Chain;

public function notStringType(): Chain;
Expand Down
Loading