<?php

namespace app\validate;

use app\model\CommentRule;
use think\Validate;
use app\model\Comment as CommentModel;

class Comment extends Validate
{
    protected $rule = [
        'comment|评论内容'                 => 'require|checkComment|max:1024',
        'user_code|用户'                   => 'require|length:32',
        'url|图片文件地址'                 => 'length:0,255',
        'type|类型'                        => 'require|checkType',
        'lng|定位信息'                      => 'require',
        'lat|定位信息'                      => 'require',
        'location|位置信息'                 => 'length:0,200',
    ];

    protected $scene = [
        'edit_password' => ['old_password', 'password', 'confirm_password'], //修改密码
    ];

    protected function checkType($value,$rule,$data=[])
    {
        return isset(CommentModel::allType()[$value]) ? true : '评论类型错误';
    }

    /**
     * 检查评论规则
     * */
    protected function checkComment($value,$rule,$data=[])
    {
        $rule = CommentRule::where("state",CommentRule::COMMON_ON)->select();
        if($rule->isEmpty()){
            return true;
        }
        foreach ($rule as $item){
            $font = explode("|",$item["rule"]);
            foreach ($font as $ruleItem){
                if(empty(trim($ruleItem))){
                    continue;
                }
                if(strpos($value,trim($ruleItem))!=false){
                    return "评论内容不能包含敏感词语";
                }
            }
        }
        return true;

    }
}