Skip to content

Commit

Permalink
Create a few classes the will be present in the next version
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
henriquemoody committed Jan 6, 2025
1 parent 3c04d1d commit 8d989a7
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 11 deletions.
15 changes: 7 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
Expand All @@ -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';
}
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions docs/06-custom-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions library/Rules/AbstractComposite.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* @author Alexandre Gomes Gaigalas <[email protected]>
* @author Henrique Moody <[email protected]>
* @author Wojciech Frącz <[email protected]>
*
* @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
{
Expand Down
2 changes: 2 additions & 0 deletions library/Rules/AbstractEnvelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* having an custom message.
*
* @author Henrique Moody <[email protected]>
*
* @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
{
Expand Down
2 changes: 2 additions & 0 deletions library/Rules/AbstractRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @author Henrique Moody <[email protected]>
* @author Nick Lombard <[email protected]>
* @author Vicente Mendoza <[email protected]>
*
* @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
{
Expand Down
2 changes: 2 additions & 0 deletions library/Rules/AbstractWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*
* @author Alasdair North <[email protected]>
* @author Henrique Moody <[email protected]>
*
* @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
{
Expand Down
21 changes: 21 additions & 0 deletions library/Rules/Core/Composite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* 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 changes: 16 additions & 0 deletions library/Rules/Core/Envelope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* SPDX-License-Identifier: MIT
*/

declare(strict_types=1);

namespace Respect\Validation\Rules\Core;

use Respect\Validation\Rules\AbstractEnvelope;

abstract class Envelope extends AbstractEnvelope
{
}
25 changes: 25 additions & 0 deletions library/Rules/Core/Simple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* 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 changes: 16 additions & 0 deletions library/Rules/Core/Wrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* SPDX-License-Identifier: MIT
*/

declare(strict_types=1);

namespace Respect\Validation\Rules\Core;

use Respect\Validation\Rules\AbstractWrapper;

abstract class Wrapper extends AbstractWrapper
{
}
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8d989a7

Please sign in to comment.