123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?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
- {
- private $data;
- public function getData()
- {
- return $this->data;
- }
- public function setData($k, $v)
- {
- $this->data[$k] = $v;
- return $this;
- }
- public function __set($_name,$_val){
- $this->data[$_name] = $_val;
- }
- /**
- * 参数判断模型
- * @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', 'is_numeric', 'is_moblie'];
- 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]);
- }
- }
- if ($param[2] == 'is_numeric') {
- if (!is_numeric($value)) {
- throw new UtilException($param[3]);
- }
- }
- if ($param[2] == 'is_moblie') {
- if (!is_moblie($value)) {
- throw new UtilException($param[3]);
- }
- }
- } else {
- $parmValue = $request->param($name, $param[1]);
- //大于
- if (strrpos($param[2], "gt|") === 0) {
- $number = str_replace("gt|", "", $param[2]);
- if (!is_numeric($number) || floatval($parmValue) < floatval($number)) {
- throw new UtilException($param[3]);
- }
- continue;
- }
- //小于
- if (strrpos($param[2], "lt|") === 0) {
- $number = str_replace("lt|", "", $param[2]);
- if (!is_numeric($number) || floatval($parmValue) > floatval($number)) {
- throw new UtilException($param[3]);
- }
- continue;
- }
- //函数
- 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,$isAll = false)
- {
- $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) {
- if($isAll){
- $_val = $callStr($val,$vv);
- }else{
- $_val = $callStr($val);
- }
- if ($_val instanceof UtilService) {
- foreach ($_val->getData() as $mk => $kv) {
- $d[$mk] = $kv;
- }
- } else {
- $d[$alias] = $_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;
- }
- }
|