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\AtLeast;
use Yiisoft\Validator\Exception\UnexpectedRuleException;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\Rule\EmptyCheckTrait;
use Yiisoft\Validator\Rule\RuleHandlerInterface;
use Yiisoft\Validator\ValidationContext;
use Yiisoft\Validator\ValidatorInterface;
/**
* Checks if at least {@see AtLeast::$min} of many attributes are filled.
*/
final class AtLeastHandler implements RuleHandlerInterface
{
use EmptyCheckTrait;
public function validate(mixed $value, object $rule, ValidatorInterface $validator, ?ValidationContext $context = null): Result
if (!$rule instanceof AtLeast) {
throw new UnexpectedRuleException(AtLeast::class, $rule);
}
$filledCount = 0;
foreach ($rule->attributes as $attribute) {
if (!$this->isEmpty($value->{$attribute})) {
$filledCount++;
$result = new Result();
if ($filledCount < $rule->min) {
$result->addError($rule->message, ['min' => $rule->min]);
return $result;