Request.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App;
  3. class Request {
  4. private $data;
  5. // Static method
  6. public static function fromRequest(): self {
  7. $request = new self();
  8. $request->data = $_REQUEST;
  9. return $request;
  10. }
  11. public static function fromGet(): self {
  12. $request = new self();
  13. $request->data = $_GET;
  14. return $request;
  15. }
  16. public static function fromPost(): self {
  17. $request = new self();
  18. $request->data = $_POST;
  19. return $request;
  20. }
  21. // Object methods
  22. public function hasData(): bool {
  23. return !empty($this->data);
  24. }
  25. /**
  26. * @param string $name
  27. * @return int|int[]|null
  28. */
  29. public function int(string $name) {
  30. $value = $this->data[$name] ?? null;
  31. if(is_array($value)) {
  32. $values = $value;
  33. $output = [];
  34. foreach($values as $value) {
  35. $output[] = ($value !== "") ? (int) $value : null;
  36. }
  37. return $output;
  38. }
  39. return ($value !== "" && $value !== null) ? (int) $value : null;
  40. }
  41. /**
  42. * @param string $name
  43. * @param int $decimals
  44. * @return string|string[]|null
  45. */
  46. public function decimal(string $name, int $decimals) {
  47. $value = $this->data[$name] ?? null;
  48. if(is_array($value)) {
  49. $values = $value;
  50. $output = [];
  51. foreach($values as $value) {
  52. $output[] = ($value !== "") ? number_format($value, $decimals, ".", "") : null;
  53. }
  54. return $output;
  55. }
  56. return ($value !== "" && $value !== null) ? number_format($value, $decimals, ".", "") : null;
  57. }
  58. /**
  59. * @param string $name
  60. * @return float|float[]|null
  61. */
  62. public function float(string $name) {
  63. $value = $this->data[$name] ?? null;
  64. if(is_array($value)) {
  65. $values = $value;
  66. $output = [];
  67. foreach($values as $value) {
  68. $output[] = ($value !== "") ? (float) $value : null;
  69. }
  70. return $output;
  71. }
  72. return ($value !== "" && $value !== null) ? (float) $value : null;
  73. }
  74. /**
  75. * @param string $name
  76. * @return mixed
  77. */
  78. public function raw(string $name) {
  79. return $this->data[$name] ?? null;
  80. }
  81. /**
  82. * @param string $name
  83. * @return string|string[]|null
  84. */
  85. public function string(string $name) {
  86. $value = $this->data[$name] ?? null;
  87. if(is_array($value)) {
  88. $values = $value;
  89. $output = [];
  90. foreach($values as $value) {
  91. $output[] = ($value !== "") ? trim($value) : null;
  92. }
  93. return $output;
  94. }
  95. return ($value !== "" && $value !== null) ? trim($value) : null;
  96. }
  97. public function email(string $name) {
  98. $value = $this->data[$name] ?? null;
  99. if(is_array($value)) {
  100. $values = $value;
  101. $output = [];
  102. foreach($values as $value) {
  103. $value = filter_var($value, FILTER_VALIDATE_EMAIL);
  104. $output[] = ($value !== false) ? $value : null;
  105. }
  106. return $output;
  107. }
  108. $value = filter_var($value, FILTER_VALIDATE_EMAIL);
  109. return ($value !== false) ? $value : null;
  110. }
  111. }