183 lines
4.6 KiB
PHP
183 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Core;
|
|
|
|
class Request
|
|
{
|
|
private array $get;
|
|
private array $post;
|
|
private array $files;
|
|
private array $server;
|
|
private array $headers;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->get = $_GET;
|
|
$this->post = $_POST;
|
|
$this->files = $_FILES;
|
|
$this->server = $_SERVER;
|
|
$this->headers = $this->getRequestHeaders();
|
|
}
|
|
|
|
public function getMethod(): string
|
|
{
|
|
return strtoupper($this->server['REQUEST_METHOD'] ?? 'GET');
|
|
}
|
|
|
|
public function getPath(): string
|
|
{
|
|
$path = $this->server['REQUEST_URI'] ?? '/';
|
|
$position = strpos($path, '?');
|
|
|
|
if ($position !== false) {
|
|
$path = substr($path, 0, $position);
|
|
}
|
|
|
|
return $path;
|
|
}
|
|
|
|
public function get(string $key, $default = null)
|
|
{
|
|
return $this->get[$key] ?? $default;
|
|
}
|
|
|
|
public function post(string $key, $default = null)
|
|
{
|
|
return $this->post[$key] ?? $default;
|
|
}
|
|
|
|
public function file(string $key)
|
|
{
|
|
return $this->files[$key] ?? null;
|
|
}
|
|
|
|
public function all(): array
|
|
{
|
|
return array_merge($this->get, $this->post);
|
|
}
|
|
|
|
public function only(array $keys): array
|
|
{
|
|
$data = $this->all();
|
|
return array_intersect_key($data, array_flip($keys));
|
|
}
|
|
|
|
public function except(array $keys): array
|
|
{
|
|
$data = $this->all();
|
|
return array_diff_key($data, array_flip($keys));
|
|
}
|
|
|
|
public function has(string $key): bool
|
|
{
|
|
return isset($this->get[$key]) || isset($this->post[$key]);
|
|
}
|
|
|
|
public function isPost(): bool
|
|
{
|
|
return $this->getMethod() === 'POST';
|
|
}
|
|
|
|
public function isGet(): bool
|
|
{
|
|
return $this->getMethod() === 'GET';
|
|
}
|
|
|
|
public function isAjax(): bool
|
|
{
|
|
return isset($this->headers['X-Requested-With']) &&
|
|
$this->headers['X-Requested-With'] === 'XMLHttpRequest';
|
|
}
|
|
|
|
public function getHeader(string $name): ?string
|
|
{
|
|
return $this->headers[$name] ?? null;
|
|
}
|
|
|
|
public function getHeaders(): array
|
|
{
|
|
return $this->headers;
|
|
}
|
|
|
|
public function getIp(): string
|
|
{
|
|
return $this->server['REMOTE_ADDR'] ?? '0.0.0.0';
|
|
}
|
|
|
|
public function getUserAgent(): string
|
|
{
|
|
return $this->server['HTTP_USER_AGENT'] ?? '';
|
|
}
|
|
|
|
public function getContentType(): string
|
|
{
|
|
return $this->server['CONTENT_TYPE'] ?? '';
|
|
}
|
|
|
|
public function getContentLength(): int
|
|
{
|
|
return (int) ($this->server['CONTENT_LENGTH'] ?? 0);
|
|
}
|
|
|
|
public function validate(array $rules): array
|
|
{
|
|
$errors = [];
|
|
$data = $this->all();
|
|
|
|
foreach ($rules as $field => $rule) {
|
|
$value = $data[$field] ?? null;
|
|
|
|
if (strpos($rule, 'required') !== false && empty($value)) {
|
|
$errors[$field][] = 'Das Feld ist erforderlich.';
|
|
continue;
|
|
}
|
|
|
|
if (!empty($value)) {
|
|
if (strpos($rule, 'email') !== false && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
|
$errors[$field][] = 'Ungültige E-Mail-Adresse.';
|
|
}
|
|
|
|
if (strpos($rule, 'min:') !== false) {
|
|
preg_match('/min:(\d+)/', $rule, $matches);
|
|
$min = (int) $matches[1];
|
|
if (strlen($value) < $min) {
|
|
$errors[$field][] = "Mindestens {$min} Zeichen erforderlich.";
|
|
}
|
|
}
|
|
|
|
if (strpos($rule, 'max:') !== false) {
|
|
preg_match('/max:(\d+)/', $rule, $matches);
|
|
$max = (int) $matches[1];
|
|
if (strlen($value) > $max) {
|
|
$errors[$field][] = "Maximal {$max} Zeichen erlaubt.";
|
|
}
|
|
}
|
|
|
|
if (strpos($rule, 'numeric') !== false && !is_numeric($value)) {
|
|
$errors[$field][] = 'Nur Zahlen erlaubt.';
|
|
}
|
|
|
|
if (strpos($rule, 'date') !== false && !strtotime($value)) {
|
|
$errors[$field][] = 'Ungültiges Datum.';
|
|
}
|
|
}
|
|
}
|
|
|
|
return $errors;
|
|
}
|
|
|
|
private function getRequestHeaders(): array
|
|
{
|
|
$headers = [];
|
|
|
|
foreach ($this->server as $key => $value) {
|
|
if (strpos($key, 'HTTP_') === 0) {
|
|
$header = str_replace('_', '-', strtolower(substr($key, 5)));
|
|
$headers[$header] = $value;
|
|
}
|
|
}
|
|
|
|
return $headers;
|
|
}
|
|
}
|