jbboehr / phpstan-laravel-validation

GNU Affero General Public License v3.0
4 stars 1 forks source link

Support custom rule #11

Open momala454 opened 3 weeks ago

momala454 commented 3 weeks ago

It will use @phpstan-assert

Exemple usage

class TrueArray implements \Illuminate\Contracts\Validation\ValidationRule
{
    public const string IDENTIFIER = 'validation.array';

    /**
     * Indicates whether the rule should be implicit.
     */
    public bool $implicit = true;

    /**
     * Run the validation rule.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     * @return void
     * @phpstan-assert array $value
     */
    public function validate(string $attribute, mixed $value, \Closure $fail): void
    {
        if (!is_array($value)) {
            $fail(static::IDENTIFIER)->translate();
        }
    }
}