Skip to content

Commit

Permalink
Docs, part 3 (#415)
Browse files Browse the repository at this point in the history
* Add docs to SkipOnErrorInterface

* Add docs to SkipOnErrorTrait

* Update src/Rule/Trait/SkipOnErrorTrait.php

Co-authored-by: Alexander Makarov <sam@rmcreative.ru>

* Update src/SkipOnErrorInterface.php

Co-authored-by: Alexander Makarov <sam@rmcreative.ru>

* Fix linebreak

Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
  • Loading branch information
arogachev and samdark committed Dec 2, 2022
1 parent 12811eb commit 0edc381
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Rule/Trait/SkipOnErrorTrait.php
Expand Up @@ -4,15 +4,42 @@

namespace Yiisoft\Validator\Rule\Trait;

/**
* An implementation for {@see SkipOnErrorInterface} intended to be included in rules. Requires an additional private
* class property `$skipOnError`. In package rules it's `false` by default:
*
* ```php
* public function __construct(
* // ...
* private bool $skipOnError = false,
* // ...
* ) {
* }
* ```
*/
trait SkipOnErrorTrait
{
/**
* An immutable setter to change `$skipOnError` property.
*
* @param bool $value A new value. `true` means to skip the current rule when the previous one errored and `false` -
* do not skip.
*
* @return $this The new instance with a changed value.
*/
public function skipOnError(bool $value): static
{
$new = clone $this;
$new->skipOnError = $value;
return $new;
}

/**
* A getter for `$skipOnError` property.
*
* @return bool Current value. `true` means to skip the current rule when the previous one errored and `false` - do
* not skip.
*/
public function shouldSkipOnError(): bool
{
return $this->skipOnError;
Expand Down
23 changes: 23 additions & 0 deletions src/SkipOnErrorInterface.php
Expand Up @@ -4,9 +4,32 @@

namespace Yiisoft\Validator;

use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;

/**
* An optional interface for rules to implement. It allows skipping validation for a rule within a group of other rules
* if a previous rule (not the current one!) did not pass the validation.
*
* The package ships with {@see SkipOnErrorTrait} which already implements that interface. All you have to do is include
* it in the rule class along with the interface.
*/
interface SkipOnErrorInterface
{
/**
* Changes current "skip on error" value. Must be immutable.
*
* @param bool $value A new value. `true` means to skip the current rule when the previous one errored and `false` -
* do not skip.
*
* @return $this The new instance of a rule with a changed value.
*/
public function skipOnError(bool $value): static;

/**
* Returns current "skip on error" value.
*
* @return bool Current value. `true` means to skip the current rule when the previous one errored and `false` - do
* not skip.
*/
public function shouldSkipOnError(): bool;
}

0 comments on commit 0edc381

Please sign in to comment.