<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class AllowTagString implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $value = aTrim($value); // 공백 제거
        $isAlphaCommaStr = ((mb_detect_encoding($value, 'UTF-8') === true) ? (bool)preg_match('/^[-\pL\pN_, .]++$/uD', (string)$value) : (bool)preg_match('/^[-a-z0-9_, .]++$/iD', (string)$value));
        if ($value && !$isAlphaCommaStr) {
            return false;
        }
        if (substr_count($value, ',') > 10) {
            return false;
        }
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return ':attribute 은(는) 콤마(,)로 구분하여 최대 10개까지 허용됩니다.';
    }
}
