<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class NumberLength implements Rule
{
    public int $passwordNumbersLength;

    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->passwordNumbersLength = config('password_numbers_length', 0);
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        // 숫자 포함 여부
        return ($this->passwordNumbersLength > 0 ? countNumbers($value) >= $this->passwordNumbersLength : true);
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return sprintf("비밀번호에는 %d개 이상의 숫자를 포함해야 합니다.", $this->passwordNumbersLength);
    }
}
