for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Yiisoft\Validator\Rule\Trait;
use Closure;
use Yiisoft\Validator\WhenInterface;
/**
* An implementation for {@see WhenInterface} intended to be included in rules. Requires an additional private class
* property `$when`. In package rules it's `null` by default:
*
* ```php
* public function __construct(
* // ...
* private Closure|null $when = null
* ) {
* }
* ```
* @psalm-import-type WhenType from WhenInterface
*/
trait WhenTrait
{
* An immutable setter to change `$when` property.
* @psalm-param WhenType $value
* @param Closure|null $value A new value:
* - `null` - always apply the validation.
* - `callable` - apply the validation depending on a return value: `true` - apply, `false` - do not apply.
* @return $this The new instance with a changed value.
public function when(Closure|null $value): static
$new = clone $this;
$new->when = $value;
when
return $new;
}
* A getter for `$when` property.
* @psalm-return WhenType
* @return Closure|null Current value:
public function getWhen(): Closure|null
return $this->when;