<?php

namespace Game\Logic;

use Framework\Logic\TemplateHelp;
use Game\Constant\TemplateDefine;

trait ShieldWordsLogic
{
    use TemplateHelp;

    //检查敏感词 true有敏感词
    public function checkFuckWord(string $str): bool
    {
        $config = $this->getTable(TemplateDefine::TYPE_SHIELD_WORDS);
        if (is_null($config)) {
            return false;
        }
        foreach ($config as $id => $arr) {
            foreach ($arr as $badWord => $star) {
                if (strpos($str, (string)$badWord) !== false) {
                    return true;
                }
            }
        }
        return false;
    }

    //替换敏感词,敏感词会替换为*
    public function replaceFuckWord(string $str): string
    {
        $config = $this->getTable(TemplateDefine::TYPE_SHIELD_WORDS);
        if (is_null($config)) {
            return $str;
        }
        foreach ($config as $id => $arr) {
            foreach ($arr as $badWord => $star) {
                if (strpos($str, trim($badWord)) !== false) {
                    $str = strtr($str, $arr);
                    continue;
                }
            }
        }
        return $str;
    }
}
