mirror of
https://github.com/Respect/Validation.git
synced 2026-03-14 22:35:45 +01:00
Create a few classes the will be present in the next version
This commit will deprecate some classes in favor of a couple of news ones. If there are no customisations to `assert()`, `check()`, and other methods, the migration should be pretty easy on users.
This commit is contained in:
parent
3c04d1da9d
commit
8d989a7bdb
11 changed files with 98 additions and 11 deletions
|
|
@ -44,7 +44,7 @@ create a validator that validates if a string is equal to "Hello World".
|
|||
|
||||
The rule itself needs to implement the `Validatable` interface but, it is
|
||||
convenient to just extend the `AbstractRule` class.
|
||||
Doing that, you'll only need to declare one method: `validate($input)`.
|
||||
Doing that, you'll only need to declare one method: `isValid(mixed $input): bool`.
|
||||
This method must return `true` or `false`.
|
||||
|
||||
If your validator class is `HelloWorld`, it will be available as `v::helloWorld()`
|
||||
|
|
@ -62,17 +62,16 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\Core\Simple;
|
||||
|
||||
/**
|
||||
* Explain in one sentence what this rule does.
|
||||
*
|
||||
* @author Your Name <youremail@yourdomain.tld>
|
||||
*/
|
||||
final class HelloWorld extends AbstractRule
|
||||
final class HelloWorld extends Simple
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
public function isValid(mixed $input): bool
|
||||
{
|
||||
return $input === 'Hello World';
|
||||
}
|
||||
|
|
@ -90,8 +89,8 @@ are able to use any methods of it. By extending `RuleTestCase` you should
|
|||
implement two methods that should return a [data provider][] with the rule as
|
||||
first item of the arrays:
|
||||
|
||||
- `providerForValidInput`: Will test when `validate()` should return `true`
|
||||
- `providerForInvalidInput`: Will test when `validate()` should return `false`
|
||||
- `providerForValidInput`: Will test when `isValid()` should return `true`
|
||||
- `providerForInvalidInput`: Will test when `isValid()` should return `false`
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
|
|
|||
|
|
@ -10,11 +10,11 @@ validate method will be executed. Here's how the class should look:
|
|||
```php
|
||||
namespace My\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Rules\Core\Simple;
|
||||
|
||||
final class Something extends AbstractRule
|
||||
final class Something extends Simple
|
||||
{
|
||||
public function validate($input): bool
|
||||
public function isValid(mixed $input): bool
|
||||
{
|
||||
// Do something here with the $input and return a boolean value
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@ use function array_map;
|
|||
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Wojciech Frącz <fraczwojciech@gmail.com>
|
||||
*
|
||||
* @deprecated This class is deprecated, and will be removed in the next major version. Use {@see \Respect\Validation\Rules\Core\Composite} instead.
|
||||
*/
|
||||
abstract class AbstractComposite extends AbstractRule
|
||||
{
|
||||
|
|
|
|||
|
|
@ -19,6 +19,8 @@ use Respect\Validation\Validatable;
|
|||
* having an custom message.
|
||||
*
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*
|
||||
* @deprecated This class is deprecated, and will be removed in the next major version. Use {@see \Respect\Validation\Rules\Core\Envelop} instead.
|
||||
*/
|
||||
abstract class AbstractEnvelope extends AbstractRule
|
||||
{
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ use Respect\Validation\Validatable;
|
|||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Nick Lombard <github@jigsoft.co.za>
|
||||
* @author Vicente Mendoza <vicentemmor@yahoo.com.mx>
|
||||
*
|
||||
* @deprecated This class is deprecated, and will be removed in the next major version. Use {@see \Respect\Validation\Rules\Core\Simple} instead.
|
||||
*/
|
||||
abstract class AbstractRule implements Validatable
|
||||
{
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ use Respect\Validation\Validatable;
|
|||
*
|
||||
* @author Alasdair North <alasdair@runway.io>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*
|
||||
* @deprecated This class is deprecated, and will be removed in the next major version. Use {@see \Respect\Validation\Rules\Core\Wrapper} instead.
|
||||
*/
|
||||
abstract class AbstractWrapper extends AbstractRule
|
||||
{
|
||||
|
|
|
|||
21
library/Rules/Core/Composite.php
Normal file
21
library/Rules/Core/Composite.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Rules\Core;
|
||||
|
||||
use Respect\Validation\Rules\AbstractComposite;
|
||||
use Respect\Validation\Validatable;
|
||||
|
||||
abstract class Composite extends AbstractComposite
|
||||
{
|
||||
public function __construct(Validatable $rule1, Validatable $rule2, Validatable ...$rules)
|
||||
{
|
||||
parent::__construct($rule1, $rule2, ...$rules);
|
||||
}
|
||||
}
|
||||
16
library/Rules/Core/Envelope.php
Normal file
16
library/Rules/Core/Envelope.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Rules\Core;
|
||||
|
||||
use Respect\Validation\Rules\AbstractEnvelope;
|
||||
|
||||
abstract class Envelope extends AbstractEnvelope
|
||||
{
|
||||
}
|
||||
25
library/Rules/Core/Simple.php
Normal file
25
library/Rules/Core/Simple.php
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Rules\Core;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
|
||||
abstract class Simple extends AbstractRule
|
||||
{
|
||||
abstract public function isValid(mixed $input): bool;
|
||||
|
||||
/**
|
||||
* @deprecated Calling `validate()` directly from rules is deprecated. Please use {@see Validator::isValid()} instead.
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
return $this->isValid($input);
|
||||
}
|
||||
}
|
||||
16
library/Rules/Core/Wrapper.php
Normal file
16
library/Rules/Core/Wrapper.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Rules\Core;
|
||||
|
||||
use Respect\Validation\Rules\AbstractWrapper;
|
||||
|
||||
abstract class Wrapper extends AbstractWrapper
|
||||
{
|
||||
}
|
||||
|
|
@ -34,6 +34,8 @@ parameters:
|
|||
# Why: Deprecations of version 3.0
|
||||
- message: '/Using rule exceptions directly is deprecated, and will be removed in the next major version\./'
|
||||
|
||||
- message: '/This class is deprecated, and will be removed in the next major version\./'
|
||||
|
||||
-
|
||||
message: '/Calling `.+\(\)` directly from rules is deprecated. Please use {@see \\Respect\\Validation\\Validator::.+\(\)} instead./'
|
||||
path: tests/unit/Rules
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue