| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPIDE\App\Kernel; |
| 4 |
|
| 5 |
use Symfony\Component\HttpFoundation\Request as SymfonyRequest; |
| 6 |
|
| 7 |
class Request extends SymfonyRequest |
| 8 |
{ |
| 9 |
public function input($key, $default = null) |
| 10 |
{ |
| 11 |
// first try GET, then POST |
| 12 |
$value = $this->get($key, $this->query->get($key)); |
| 13 |
|
| 14 |
// then look into JSON content, fallback to default |
| 15 |
if ($value === null) { |
| 16 |
$content = json_decode((string) $this->getContent()); |
| 17 |
$value = isset($content->{$key}) ? $content->{$key} : $default; |
| 18 |
} |
| 19 |
|
| 20 |
return $value; |
| 21 |
} |
| 22 |
|
| 23 |
public function textInput($key, $default = null):? string |
| 24 |
{ |
| 25 |
|
| 26 |
$value = $this->input($key, $default); |
| 27 |
return $value ? sanitize_text_field($value) : $value; |
| 28 |
} |
| 29 |
|
| 30 |
public function boolInput($key, $default = null):? bool |
| 31 |
{ |
| 32 |
|
| 33 |
$value = $this->input($key, $default); |
| 34 |
return $value ? (bool) $value : $value; |
| 35 |
} |
| 36 |
|
| 37 |
public function intInput($key, $default = null):? int |
| 38 |
{ |
| 39 |
|
| 40 |
$value = $this->input($key, $default); |
| 41 |
return $value ? intval($value) : $value; |
| 42 |
} |
| 43 |
|
| 44 |
public function floatInput($key, $default = null):? float |
| 45 |
{ |
| 46 |
|
| 47 |
$value = $this->input($key, $default); |
| 48 |
return $value ? floatval($value) : $value; |
| 49 |
} |
| 50 |
|
| 51 |
public function enumInput($key, $enumValues = [], $default = null):? string |
| 52 |
{ |
| 53 |
|
| 54 |
$value = $this->textInput($key, $default); |
| 55 |
return in_array($value, $enumValues) ? $value : null; |
| 56 |
} |
| 57 |
|
| 58 |
public function all(): array |
| 59 |
{ |
| 60 |
$params = []; |
| 61 |
|
| 62 |
// first look into JSON content |
| 63 |
$content = json_decode((string) $this->getContent()); |
| 64 |
if (! empty($content)) { |
| 65 |
foreach ($content as $key => $param) { |
| 66 |
$params[$key] = $param; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// then try (and override) with POST |
| 71 |
foreach ($this->request as $key => $param) { |
| 72 |
$params[$key] = $param; |
| 73 |
} |
| 74 |
|
| 75 |
// finally try (and override) with GET |
| 76 |
foreach ($this->query as $key => $param) { |
| 77 |
$params[$key] = $param; |
| 78 |
} |
| 79 |
|
| 80 |
return $params; |
| 81 |
} |
| 82 |
} |
| 83 |
|