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\UnexpectedRuleException;
use Yiisoft\Validator\Formatter;
use Yiisoft\Validator\FormatterInterface;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\ValidationContext;
/**
* Checks if the value is a boolean value or a value corresponding to it.
*/
final class BooleanHandler implements RuleHandlerInterface
{
private FormatterInterface $formatter;
public function __construct(?FormatterInterface $formatter = null)
$this->formatter = $formatter ?? new Formatter();
}
public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
if (!$rule instanceof Boolean) {
throw new UnexpectedRuleException(Boolean::class, $rule);
if ($rule->isStrict()) {
$valid = $value === $rule->getTrueValue() || $value === $rule->getFalseValue();
} else {
$valid = $value == $rule->getTrueValue() || $value == $rule->getFalseValue();
$result = new Result();
if ($valid) {
return $result;
$formattedMessage = $this->formatter->format(
$rule->getMessage(),
[
'true' => $rule->getTrueValue() === true ? '1' : $rule->getTrueValue(),
'false' => $rule->getFalseValue() === false ? '0' : $rule->getFalseValue(),
'attribute' => $context?->getAttribute(),
'value' => $value,
]
);
$result->addError($formattedMessage);