Request.php 4.3 KB

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