<?php
// +----------------------------------------------------------------------
// | [ UtilService  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-25 17:30
// +----------------------------------------------------------------------
namespace library\services;

use library\exceptions\UtilException;

class  UtilService {

    /**
     * 参数判断模型
     * @param $params   [
     *      [ 'parm1'   =>  'post|get',
     *        'parm2'   =>  '',//默认值
     *        'parm3'   =>  'empty',//函数
     *        'parm4'   =>  '错误信息',
     *      ]
     * ]
     * @param null $request
     * @param bool $suffix
     */
    public static function getMore($params, $request = null, $suffix = false)
    {
        $fiAr = ['empty','is_null','isset'];
        if ($request === null) {
            $request = request();
        }
        $i = 0;
        foreach ($params as $param) {
            if (!is_array($param)) {
                $p[$suffix == true ? $i++ : $param] = $request->param($param);
            } else {
                if (!isset($param[1])) $param[1] = null;
                if (!isset($param[2])) $param[2] = '';
                if (!isset($param[3])) $param[3] = '';
                if (is_array($param[0])) {
                    $name = $param[0][0];
                    $keyName = $param[0][0];
                    foreach ($param[0] as $k=>$v) {
                        $name = $k;
                        $keyName = $v;
                    }
                } else {
                    $name = $param[0];
                    $keyName = $param[0];
                }
                $value = $request->param($name, $param[1]);
                $p[$suffix == true ? $i++ : $keyName ] = $value;
                if(in_array($param[2],$fiAr)) {
                    if($param[2] == 'empty') {
                        if(empty($value)) {
                            throw new UtilException($param[3]);
                        }
                    }
                    if($param[2] == 'is_null') {
                        if(is_null($value)) {
                            throw new UtilException($param[3]);
                        }
                    }
                    if($param[2] == 'isset') {
                        if(!isset($value)) {
                            throw new UtilException($param[3]);
                        }
                    }
                } else {
                    if (!empty($param[2]) && is_string($param[2]) && !function_exists($param[2])) {
                        throw new UtilException('方法' . $param[2] . '不存在');
                    }

                    if (!empty($param[2]) && !$param[2]($request->param($name, $param[1]))) {
                        throw new UtilException($param[3]);
                    }
                }
            }
        }
        return $p;
    }

    /**
     * 参数解析 | 返回
     * @param $params
     * @param $data
     */
    public static function getParam($params,$data) {
        $result = [];
        foreach ($data as $k => $vv) {
            $d = [];
            foreach ($params as $v) {
                if (is_array($v)) {
                    $alias   = empty($v[0]) ? $v[1] : $v[0];
                    $key     = $v[1];
                    $callStr = empty($v[2]) ? '' : $v[2];
                    $val     = $vv[$key];
                    if($callStr instanceof \Closure) {
                        $d[$alias] = $callStr($val);
                    }
                    else if(!empty($callStr)){
                        $e       = null;
                        $callStr = str_replace('$1', '$val', $callStr);
                        eval('$e =' . $callStr . ';');
                        $d[$alias] = $e;
                    }
                    else {
                        $d[$alias] = $val;
                    }
                } else {
                    $d[$v] = $vv[$v];
                }
            }
            $result[] = $d;
        }

        return $result;
    }

    /**
     * 获取POST请求的数据
     * @param $params
     * @param null $request
     * @param bool $suffix
     * @return array
     * @throws \Exception
     */
    public static function postMore($params, $request = null, $suffix = false)
    {
        if ($request === null) $request = app('request');
        $p = [];
        $i = 0;
        foreach ($params as $param) {
            if (!is_array($param)) {
                $p[$suffix == true ? $i++ : $param] = $request->param($param);
            } else {
                if (!isset($param[1])) $param[1] = null;
                if (!isset($param[2])) $param[2] = '';
                if (!isset($param[3])) $param[3] = '';
                if (!isset($param[4])) $param[4] = '';
                if (!isset($param[5])) $param[5] = '';
                if (is_array($param[0])) {
                    $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
                    $keyName = $param[0][0];
                } else {
                    $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
                    $keyName = $param[0];
                }
                $p[$suffix == true ? $i++ : (isset($param[3]) && $param[3] ? $param[3] : $keyName)] = $request->param($name, $param[1], $param[2]);
                if (!empty_check($param[4])) {
                    if (!is_array($param[4])) {
                        if (is_string($param[4]) && !function_exists($param[4])) {
                            throw new UtilException('验证的方法' . $param[4] . '未定义');
                        }
                        if ($param[4]($request->param($name, $param[1], $param[2]))) {
                            throw new UtilException($param[5]);
                        }
                    } else {
                        foreach ($param[4] as $k => $v) {
                            if (is_string($v) && !function_exists($v)) {
                                throw new UtilException('验证的方法' . $v . '未定义');
                            }
                            if ($v($request->param($name, $param[1], $param[2]))) {
                                throw new UtilException(is_array($param[5]) ? $param[5][$k] : $param[5]);
                            }
                        }
                    }
                }
            }
        }
        return $p;
    }

}
