<?php

namespace app\admin\validate\device;

use support\Validate;

class LedgerValidate extends Validate
{
    protected $rule = [
        'device_ledger_id' => 'require|integer',
        'join_device_ledger_category_id' => 'integer',
        'device_ledger_status|状态' => 'require|in:ACTIVED,DISABLED',
        'device_ledger_category|分类' => 'max:32',
        'device_ledger_type|类型' => 'max:32',
        'device_ledger_key|标识' => 'max:32',
        'device_ledger_name|名称' => 'max:32',
        'device_ledger_protocol_json|通信' => 'isJson',
        'device_ledger_identify_json|识别' => 'isJson',
        'device_ledger_metadata_json|元数据' => 'isJson',
        'device_ledger_command_json|指令集' => 'isJson',
        'device_ledger_extend_json' => 'isJson'
    ];

    protected $scene = [
        'add' => ['join_device_ledger_category_id', 'device_ledger_status', 'device_ledger_category', 'device_ledger_type', 'device_ledger_key',
            'device_ledger_name', 'device_ledger_protocol_json', 'device_ledger_identify_json', 'device_ledger_metadata_json',
            'device_ledger_command_json', 'device_ledger_extend_json'],
        'update' => ['device_ledger_id', 'join_device_ledger_category_id', 'device_ledger_status', 'device_ledger_category', 'device_ledger_type', 'device_ledger_key',
            'device_ledger_name', 'device_ledger_protocol_json', 'device_ledger_identify_json', 'device_ledger_metadata_json',
            'device_ledger_command_json', 'device_ledger_extend_json'],
    ];

    public function isJson($value)
    {
        if (is_json($value)) {
            return true;
        }
        return '数据格式错误~';
    }
}