mallardduck/extended-validator-laravel

View on GitHub
src/Rules/NotInIf.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

declare(strict_types=1);

namespace MallardDuck\ExtendedValidator\Rules;

use Closure;
use Illuminate\Validation\Validator;

final class NotInIf extends BaseRule
{
    public function __construct()
    {
        parent::__construct(
            $this->getRuleClosure(),
            __('validation.not_in')
        );
    }

    public function getRuleClosure(): Closure
    {
        $ruleName = $this->getImplicitRuleName();
        return static function (
            string $attribute,
            $value,
            $parameters,
            Validator $validator
        ) use ($ruleName) {
            $validator->requireParameterCount(2, $parameters, $ruleName);

            [$values, $other] = $validator->parseDependentRuleParameters($parameters);
            if ($other) {
                return ! in_array($value, $values);
            }

            return true;
        };
    }
}