<?php

namespace App\Rpc;

class RpcResponse
{
    public int $code = 0;
    public string $msg = 'ok';
    public array $data = [];
    public array $meta = [];
    public string $title = '';
    public array $pageModule = [];
    public array $extra = [];


    public function setData(array $data)
    {
        $this->data = $data;
        return $this;
    }

    public function setExtra($extraKey = '', $value = '')
    {
        $this->extra[$extraKey] = $value;
        return $this;
    }

    public function setCode(int $code) 
    {
        $this->code = $code;
        return $this;
    }

    public function setMsg(string $message) 
    {
        $this->msg = $message;
        return $this;
    }

    public function send()
    {
        $resp = [
            'code' => $this->code,
            'msg' => $this->msg,
            'data' => $this->data,
        ];

        if ($this->extra) {
            foreach ($this->extra as $key => $item) {
                $resp[$key] = $item;
            }
        }
        return $resp;
    }
}