function MyCheckIntegerArray($ParameterName, $MinValue = 1, $MaxValue = PHP_INT_MAX) { global ${$ParameterName}; $Parameter = ${$ParameterName}; if (!is_array($Parameter)) { MyResult('1038', $data = '', $tips = '参数' . $ParameterName . '的值格式错误', $description = '参数' . $ParameterName . '的值应为不小于' . $MinValue . ',不大于' . $MaxValue . '的整数的数组,请求传递的' . $ParameterName . '参数的值不是数组,参数的值为' . $Parameter); } for ($i = 0; $i < count($Parameter); $i++) { $P = $Parameter[$i]; if (isset($P) && is_numeric($P) && (int) $P == (double) $P && (double) $P >= $MinValue && (double) $P <= $MaxValue) { ${$ParameterName}[$i] = (int) $P; } else { MyResult('1039', $data = '', $tips = '参数' . $ParameterName . '的值格式错误', $description = '参数' . $ParameterName . '的值应为不小于' . $MinValue . ',不大于' . $MaxValue . '的整数的数组,请求传递的' . $ParameterName . '参数的数组元素的值不符合限制条件,参数的值为' . MyJsonEncode($Parameter)); } } }
function MyError($type = '', $description = '执行失败') { MyErrorReport($type, $description); $result = array("result" => "error", "type" => $type, "description" => (string) $description); //$result = json_encode($result); $result = MyJsonEncode($result); die($result); }
function MyJsonEncode($data, $option = []) { $option = array_merge(["STRING2HTML" => false], $option); $is_assoc = function ($array) { foreach (array_keys($array) as $k => $v) { if ($k !== $v) { return true; } } return false; }; switch (gettype($data)) { case "boolean": if ($data) { $out = 'true'; } else { $out = 'false'; } break; case "integer": $out = "" . $data; break; case "double": $out = "" . $data; break; case "string": if ($option['STRING2HTML']) { $out = '"' . html($data) . '"'; } else { $out = '"' . $data . '"'; } break; case "array": if ($is_assoc($data)) { $out = '{'; $start = true; foreach ($data as $key => $value) { if ($start) { $start = false; } else { $out .= ','; } $out .= MyJsonEncode($key, ["STRING2HTML" => false]) . ':'; $out .= MyJsonEncode($value, $option); } $out .= '}'; } else { $out = '['; $start = true; foreach ($data as $value) { if ($start) { $start = false; } else { $out .= ','; } $out .= MyJsonEncode($value, $option); } $out .= ']'; } break; case "object": $obj = new ReflectionObject($data); $properties = $obj->getProperty(ReflectionProperty::IS_PUBLIC); $out = '{'; $start = true; foreach ($properties as $property) { if ($start) { $start = false; } else { $out .= ','; } $out .= MyJsonEncode($property->name, ["STRING2HTML" => false]) . ':'; $out .= MyJsonEncode($property->getValue($data), $option); } $out .= '}'; break; case "NULL": $out = 'null'; break; default: throw new RuntimeException("La donnée passé en paramètre de la fonction MyJsonEncode n'est pas valide"); } return $out; }