<?php

declare(strict_types=1);

namespace app\admin\validate;

use app\admin\model\Domain as DomainModel;
use think\Validate;

/**
 * 域名验证器
 */
class Domain extends Validate
{
    protected $rule = [
        'domain' => 'require|checkDomain',
    ];

    protected $message = [
        'domain.require' => '请输入根域名',
    ];

    protected $scene = [
        'add' => ['domain'],
    ];

    /**
     * @param string $value
     * @return bool|string
     */
    protected function checkDomain($value)
    {
        $domain = DomainModel::normalizeDomain((string)$value);
        if (!DomainModel::isValidRootDomain($domain)) {
            return '请输入合法根域名，不要填写 www 或二级子域名，且不支持中国域名后缀';
        }
        return true;
    }
}
