<?php

namespace App\Exception;

use Helpers\ApiResponse;
use Yaf\Registry;

/**
 * Class BaseException
 * 自定义异常类的基类
 */
class BaseException extends \Exception
{
    use ApiResponse;

    public $msg;
    public $code;

    /**
     * 构造函数，接收一个关联数组
     * @param array $params 关联数组只应包含code、msg和errorCode，且不应该是空值
     */
    public function __construct($params = [])
    {
        if (!is_array($params)) {
            return;
        }

        $this->code = $params['code'] ?? Registry::get('config')->exception->user->code;
        $this->msg =  $params['msg']  ?? Registry::get('config')->exception->user->msg;

        if (isset($params['cus']) && $this->cus && isset($this->cus[intval($params['cus'])]) && $this->base_code) {
            $cus_code = intval($params['cus']);
            $base_code = intval($this->base_code);

            $this->code = $base_code + $cus_code;
            $this->msg = $this->cus[$cus_code];
        }
    }

    public function __get($name): bool
    {
        return false;
    }
}
