<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class SpecialCharLength implements Rule
{
    public int $passwordSpecialcharsLength;

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

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

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