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;
use Yiisoft\Validator\Exception\InvalidCallbackReturnTypeException;
use Yiisoft\Validator\FormatterInterface;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\Rule;
use Yiisoft\Validator\ValidationContext;
final class Callback extends Rule
{
public function __construct(
/**
* @var callable
*/
private $callback,
?FormatterInterface $formatter = null,
bool $skipOnEmpty = false,
bool $skipOnError = false,
$when = null
) {
parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
}
protected function validateValue($value, ?ValidationContext $context = null): Result
$callback = $this->callback;
$callbackResult = $callback($value, $context);
if (!$callbackResult instanceof Result) {
throw new InvalidCallbackReturnTypeException($callbackResult);
$result = new Result();
if ($callbackResult->isValid()) {
return $result;
foreach ($callbackResult->getErrors() as $error) {
$result->addError($this->formatMessage($error->getMessage()), $error->getValuePath());