Request.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace App;
  3. use RuntimeException;
  4. class Request {
  5. private $data;
  6. // Static method
  7. public static function fromRequest(): self {
  8. $request = new self();
  9. $request->data = $_REQUEST;
  10. return $request;
  11. }
  12. public static function fromGet(): self {
  13. $request = new self();
  14. $request->data = $_GET;
  15. return $request;
  16. }
  17. public static function fromPost(): self {
  18. $request = new self();
  19. $request->data = $_POST;
  20. return $request;
  21. }
  22. /**
  23. * @param string $name
  24. * @param bool $validate Performs validation and returns only files that have been uploaded
  25. * @return array|array[]
  26. */
  27. public static function getFileArray(string $name, bool $validate = false): array {
  28. $form_files = $_FILES[$name] ?? [];
  29. $files = [];
  30. if($form_files) {
  31. // Single file field
  32. $names = $form_files["name"];
  33. if(!is_array($names)) {
  34. return [$form_files];
  35. }
  36. // Multiple file field
  37. $file_count = count($form_files["name"]);
  38. for($i = 0; $i < $file_count; $i++) {
  39. $files[] = [
  40. "name" => $form_files["name"][$i],
  41. "type" => $form_files["type"][$i],
  42. "size" => $form_files["size"][$i],
  43. "tmp_name" => $form_files["tmp_name"][$i],
  44. "error" => $form_files["error"][$i],
  45. ];
  46. }
  47. }
  48. if($validate) {
  49. foreach($files as $index => $file) {
  50. if(!in_array($file["error"], [UPLOAD_ERR_OK, UPLOAD_ERR_NO_FILE])) {
  51. throw new RuntimeException(__("File upload error."));
  52. }
  53. if($file["error"] === UPLOAD_ERR_NO_FILE) {
  54. unset($files[$index]);
  55. }
  56. }
  57. }
  58. return array_values($files);
  59. }
  60. // Object methods
  61. public function hasData(): bool {
  62. return !empty($this->data);
  63. }
  64. /**
  65. * @param string $name
  66. * @return int|int[]|null
  67. */
  68. public function int(string $name) {
  69. $value = $this->data[$name] ?? null;
  70. if(is_array($value)) {
  71. $values = $value;
  72. $output = [];
  73. foreach($values as $value) {
  74. $output[] = ($value !== "") ? (int) $value : null;
  75. }
  76. return $output;
  77. }
  78. return ($value !== "" && $value !== null) ? (int) $value : null;
  79. }
  80. /**
  81. * @param string $name
  82. * @param int $decimals
  83. * @return string|string[]|null
  84. */
  85. public function decimal(string $name, int $decimals) {
  86. $value = $this->data[$name] ?? null;
  87. if(is_array($value)) {
  88. $values = $value;
  89. $output = [];
  90. foreach($values as $value) {
  91. $output[] = ($value !== "") ? number_format($value, $decimals, ".", "") : null;
  92. }
  93. return $output;
  94. }
  95. return ($value !== "" && $value !== null) ? number_format($value, $decimals, ".", "") : null;
  96. }
  97. /**
  98. * @param string $name
  99. * @return float|float[]|null
  100. */
  101. public function float(string $name) {
  102. $value = $this->data[$name] ?? null;
  103. if(is_array($value)) {
  104. $values = $value;
  105. $output = [];
  106. foreach($values as $value) {
  107. $output[] = ($value !== "") ? (float) $value : null;
  108. }
  109. return $output;
  110. }
  111. return ($value !== "" && $value !== null) ? (float) $value : null;
  112. }
  113. /**
  114. * @param string $name
  115. * @return mixed
  116. */
  117. public function raw(string $name) {
  118. return $this->data[$name] ?? null;
  119. }
  120. /**
  121. * @param string $name
  122. * @return string|string[]|null
  123. */
  124. public function string(string $name) {
  125. $value = $this->data[$name] ?? null;
  126. if(is_array($value)) {
  127. $values = $value;
  128. $output = [];
  129. foreach($values as $value) {
  130. $output[] = ($value !== "") ? trim($value) : null;
  131. }
  132. return $output;
  133. }
  134. return ($value !== "" && $value !== null) ? trim($value) : null;
  135. }
  136. public function email(string $name) {
  137. $value = $this->data[$name] ?? null;
  138. if(is_array($value)) {
  139. $values = $value;
  140. $output = [];
  141. foreach($values as $value) {
  142. $value = filter_var($value, FILTER_VALIDATE_EMAIL);
  143. $output[] = ($value !== false) ? $value : null;
  144. }
  145. return $output;
  146. }
  147. $value = filter_var($value, FILTER_VALIDATE_EMAIL);
  148. return ($value !== false) ? $value : null;
  149. }
  150. }