| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace App;
- use RuntimeException;
- class Request {
- private $data;
- // Static method
- public static function fromRequest(): self {
- $request = new self();
- $request->data = $_REQUEST;
- return $request;
- }
- public static function fromGet(): self {
- $request = new self();
- $request->data = $_GET;
- return $request;
- }
- public static function fromPost(): self {
- $request = new self();
- $request->data = $_POST;
- return $request;
- }
- /**
- * @param string $name
- * @param bool $validate Performs validation and returns only files that have been uploaded
- * @return array|array[]
- */
- public static function getFileArray(string $name, bool $validate = false): array {
- $form_files = $_FILES[$name] ?? [];
- $files = [];
- if($form_files) {
- // Single file field
- $names = $form_files["name"];
- if(!is_array($names)) {
- return [$form_files];
- }
- // Multiple file field
- $file_count = count($form_files["name"]);
- for($i = 0; $i < $file_count; $i++) {
- $files[] = [
- "name" => $form_files["name"][$i],
- "type" => $form_files["type"][$i],
- "size" => $form_files["size"][$i],
- "tmp_name" => $form_files["tmp_name"][$i],
- "error" => $form_files["error"][$i],
- ];
- }
- }
- if($validate) {
- foreach($files as $index => $file) {
- if(!in_array($file["error"], [UPLOAD_ERR_OK, UPLOAD_ERR_NO_FILE])) {
- throw new RuntimeException(__("File upload error."));
- }
- if($file["error"] === UPLOAD_ERR_NO_FILE) {
- unset($files[$index]);
- }
- }
- }
- return array_values($files);
- }
- // Object methods
- public function hasData(): bool {
- return !empty($this->data);
- }
- /**
- * @param string $name
- * @return int|int[]|null
- */
- public function int(string $name) {
- $value = $this->data[$name] ?? null;
- if(is_array($value)) {
- $values = $value;
- $output = [];
- foreach($values as $value) {
- $output[] = ($value !== "") ? (int) $value : null;
- }
- return $output;
- }
- return ($value !== "" && $value !== null) ? (int) $value : null;
- }
- /**
- * @param string $name
- * @param int $decimals
- * @return string|string[]|null
- */
- public function decimal(string $name, int $decimals) {
- $value = $this->data[$name] ?? null;
- if(is_array($value)) {
- $values = $value;
- $output = [];
- foreach($values as $value) {
- $output[] = ($value !== "") ? number_format($value, $decimals, ".", "") : null;
- }
- return $output;
- }
- return ($value !== "" && $value !== null) ? number_format($value, $decimals, ".", "") : null;
- }
- /**
- * @param string $name
- * @return float|float[]|null
- */
- public function float(string $name) {
- $value = $this->data[$name] ?? null;
- if(is_array($value)) {
- $values = $value;
- $output = [];
- foreach($values as $value) {
- $output[] = ($value !== "") ? (float) $value : null;
- }
- return $output;
- }
- return ($value !== "" && $value !== null) ? (float) $value : null;
- }
- /**
- * @param string $name
- * @return mixed
- */
- public function raw(string $name) {
- return $this->data[$name] ?? null;
- }
- /**
- * @param string $name
- * @return string|string[]|null
- */
- public function string(string $name) {
- $value = $this->data[$name] ?? null;
- if(is_array($value)) {
- $values = $value;
- $output = [];
- foreach($values as $value) {
- $output[] = ($value !== "") ? trim($value) : null;
- }
- return $output;
- }
- return ($value !== "" && $value !== null) ? trim($value) : null;
- }
- public function email(string $name) {
- $value = $this->data[$name] ?? null;
- if(is_array($value)) {
- $values = $value;
- $output = [];
- foreach($values as $value) {
- $value = filter_var($value, FILTER_VALIDATE_EMAIL);
- $output[] = ($value !== false) ? $value : null;
- }
- return $output;
- }
- $value = filter_var($value, FILTER_VALIDATE_EMAIL);
- return ($value !== false) ? $value : null;
- }
- }
|