HttpParameter.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: JZ
  5. * Date: 2019/7/18
  6. * Time: 17:23
  7. */
  8. class HttpParameter {
  9. private $params; // 参数
  10. private $listFiles; // 文件列表
  11. private $httpMethod; // 请求方式
  12. private $jsonParams; // json参数
  13. /**
  14. * HttpParameter constructor.
  15. * @param $httpMethod
  16. */
  17. public function __construct($httpMethod) {
  18. $this->httpMethod = $httpMethod;
  19. }
  20. /**
  21. * post请求
  22. * @return HttpParameter
  23. */
  24. public static function httpPostParamer(){
  25. return new HttpParameter(HttpMethod::POST);
  26. }
  27. /**
  28. * get请求
  29. * @return HttpParameter
  30. */
  31. public static function httpGetParamer(){
  32. return new HttpParameter(HttpMethod::GET);
  33. }
  34. /**
  35. * 获取url参数(一般用于get请求)
  36. * @return null|string
  37. */
  38. public function getQueryString(){
  39. $urlParams = $this->getParams();
  40. if(is_null($urlParams)){
  41. return null;
  42. }
  43. $string = [];
  44. if($urlParams && is_array($urlParams)){
  45. foreach ($urlParams as $key=> $value){
  46. $string[] = $key.'='.$value;
  47. }
  48. }
  49. return implode('&',$string);
  50. }
  51. /**
  52. * @return mixed
  53. */
  54. public function getParams()
  55. {
  56. return $this->params;
  57. }
  58. /**
  59. * @param mixed $params
  60. */
  61. public function setParams($params)
  62. {
  63. $this->params = $params;
  64. }
  65. public function addParam($name, $value) {
  66. $params = $this->params;
  67. if (is_null($params)) {
  68. $params = array();
  69. }
  70. if(is_array($params)){
  71. $params[$name] = $value;
  72. }
  73. $this->params = $params;
  74. }
  75. /**
  76. * @return mixed
  77. */
  78. public function getHttpMethod()
  79. {
  80. return $this->httpMethod;
  81. }
  82. /**
  83. * @param mixed $httpMethod
  84. */
  85. public function setHttpMethod($httpMethod)
  86. {
  87. $this->httpMethod = $httpMethod;
  88. }
  89. /**
  90. * @return mixed
  91. */
  92. public function getJsonParams()
  93. {
  94. return $this->jsonParams;
  95. }
  96. /**
  97. * @param mixed $jsonParams
  98. */
  99. public function setJsonParams($jsonParams)
  100. {
  101. $this->jsonParams = $jsonParams;
  102. }
  103. /**
  104. * 是否是json请求
  105. */
  106. public function isJson() {
  107. if(is_null($this->jsonParams)){
  108. return false;
  109. }
  110. return true;
  111. }
  112. /**
  113. * @return mixed
  114. */
  115. public function getListFiles()
  116. {
  117. return $this->listFiles;
  118. }
  119. public function addListFiles($name, $files) {
  120. $listFiles = $this->listFiles;
  121. if (is_null($listFiles)) {
  122. $listFiles = array();
  123. }
  124. if(is_array($listFiles)){
  125. $listFiles[$name] = $files;
  126. }
  127. $this->listFiles = $listFiles;
  128. }
  129. /**
  130. * 请求是否包含多文件
  131. */
  132. public function isFiles(){
  133. if(is_null($this->listFiles)){
  134. return false;
  135. }
  136. return true;
  137. }
  138. }