<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use App\Models\Config;

class FilterSpamKeyword 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)
    {
        $spamWord = array_filter(explode(',', trim((new Config)->item("spam_word"))));
        if ($spamWord) {
            for ($i = 0; $i < count($spamWord); $i++) {
                $str = trim($spamWord[$i]);
                if ($value) {
                    $pos = stripos($value, $str);
                    if ($pos !== false) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return ':attribute 에 금지 단어가 포함되었습니다.';
    }
}
