major update
This commit is contained in:
101
lib/Http/Input/FormErrors.php
Normal file
101
lib/Http/Input/FormErrors.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Http\Input;
|
||||
|
||||
final class FormErrors
|
||||
{
|
||||
/** @var array<string, array<int, string>> */
|
||||
private array $errors = [];
|
||||
|
||||
public function add(string $field, string $message): self
|
||||
{
|
||||
$field = trim($field);
|
||||
if ($field === '') {
|
||||
$field = 'input';
|
||||
}
|
||||
|
||||
$message = trim($message);
|
||||
if ($message === '') {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->errors[$field] ??= [];
|
||||
$this->errors[$field][] = $message;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $messages
|
||||
*/
|
||||
public function addMany(string $field, array $messages): self
|
||||
{
|
||||
foreach ($messages as $message) {
|
||||
$this->add($field, (string) $message);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addGlobal(string $message): self
|
||||
{
|
||||
return $this->add('input', $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, array<int, string>|string>|array<int, string>|self $errors
|
||||
*/
|
||||
public function merge(array|self $errors): self
|
||||
{
|
||||
if ($errors instanceof self) {
|
||||
$errors = $errors->toArray();
|
||||
}
|
||||
|
||||
if (array_is_list($errors)) {
|
||||
foreach ($errors as $message) {
|
||||
$this->addGlobal((string) $message);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
foreach ($errors as $field => $messages) {
|
||||
if (is_array($messages)) {
|
||||
$this->addMany((string) $field, array_values(array_map('strval', $messages)));
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->add((string) $field, (string) $messages);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasAny(): bool
|
||||
{
|
||||
return $this->errors !== [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<int, string>>
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function toFlatList(): array
|
||||
{
|
||||
$flat = [];
|
||||
foreach ($this->errors as $messages) {
|
||||
foreach ($messages as $message) {
|
||||
$flat[] = $message;
|
||||
}
|
||||
}
|
||||
|
||||
return $flat;
|
||||
}
|
||||
}
|
||||
137
lib/Http/Input/RequestInput.php
Normal file
137
lib/Http/Input/RequestInput.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Http\Input;
|
||||
|
||||
use MintyPHP\Http\Request;
|
||||
|
||||
final class RequestInput
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $query
|
||||
* @param array<string, mixed> $body
|
||||
* @param array<string, mixed> $files
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly string $method,
|
||||
private readonly array $query,
|
||||
private readonly array $body,
|
||||
private readonly array $files
|
||||
) {
|
||||
}
|
||||
|
||||
public function method(): string
|
||||
{
|
||||
return $this->method;
|
||||
}
|
||||
|
||||
public function isMethod(string ...$methods): bool
|
||||
{
|
||||
foreach ($methods as $method) {
|
||||
if ($this->method === strtoupper(trim($method))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function queryAll(): array
|
||||
{
|
||||
return $this->query;
|
||||
}
|
||||
|
||||
public function query(string $key, mixed $default = null): mixed
|
||||
{
|
||||
return array_key_exists($key, $this->query) ? $this->query[$key] : $default;
|
||||
}
|
||||
|
||||
public function hasQuery(string $key): bool
|
||||
{
|
||||
return array_key_exists($key, $this->query);
|
||||
}
|
||||
|
||||
public function queryString(string $key, string $default = ''): string
|
||||
{
|
||||
return trim((string) $this->query($key, $default));
|
||||
}
|
||||
|
||||
public function queryInt(string $key, int $default = 0): int
|
||||
{
|
||||
return (int) $this->query($key, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int|string, mixed>
|
||||
*/
|
||||
public function queryArray(string $key): array
|
||||
{
|
||||
$value = $this->query($key, []);
|
||||
return is_array($value) ? $value : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function bodyAll(): array
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
public function body(string $key, mixed $default = null): mixed
|
||||
{
|
||||
return array_key_exists($key, $this->body) ? $this->body[$key] : $default;
|
||||
}
|
||||
|
||||
public function hasBody(string $key): bool
|
||||
{
|
||||
return array_key_exists($key, $this->body);
|
||||
}
|
||||
|
||||
public function bodyString(string $key, string $default = ''): string
|
||||
{
|
||||
return trim((string) $this->body($key, $default));
|
||||
}
|
||||
|
||||
public function bodyInt(string $key, int $default = 0): int
|
||||
{
|
||||
return (int) $this->body($key, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int|string, mixed>
|
||||
*/
|
||||
public function bodyArray(string $key): array
|
||||
{
|
||||
$value = $this->body($key, []);
|
||||
return is_array($value) ? $value : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function filesAll(): array
|
||||
{
|
||||
return $this->files;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function file(string $key, mixed $default = null): mixed
|
||||
{
|
||||
return array_key_exists($key, $this->files) ? $this->files[$key] : $default;
|
||||
}
|
||||
|
||||
public function hasFile(string $key): bool
|
||||
{
|
||||
return array_key_exists($key, $this->files);
|
||||
}
|
||||
|
||||
public function wantsJson(): bool
|
||||
{
|
||||
return Request::wantsJson();
|
||||
}
|
||||
}
|
||||
47
lib/Http/Input/RequestInputFactory.php
Normal file
47
lib/Http/Input/RequestInputFactory.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Http\Input;
|
||||
|
||||
final class RequestInputFactory
|
||||
{
|
||||
/** @var array<string, mixed>|null */
|
||||
private ?array $cachedApiBody = null;
|
||||
|
||||
public function create(): RequestInput
|
||||
{
|
||||
$method = strtoupper(trim((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET')));
|
||||
if ($method === '') {
|
||||
$method = 'GET';
|
||||
}
|
||||
|
||||
$query = $_GET;
|
||||
$files = $_FILES;
|
||||
|
||||
if (defined('MINTY_API_REQUEST')) {
|
||||
return new RequestInput($method, $query, $this->readApiBody(), $files);
|
||||
}
|
||||
|
||||
$body = $_POST;
|
||||
return new RequestInput($method, $query, $body, $files);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function readApiBody(): array
|
||||
{
|
||||
if ($this->cachedApiBody !== null) {
|
||||
return $this->cachedApiBody;
|
||||
}
|
||||
|
||||
$raw = file_get_contents('php://input');
|
||||
if (!is_string($raw) || $raw === '') {
|
||||
$this->cachedApiBody = [];
|
||||
return $this->cachedApiBody;
|
||||
}
|
||||
|
||||
$decoded = json_decode($raw, true);
|
||||
$this->cachedApiBody = is_array($decoded) ? $decoded : [];
|
||||
return $this->cachedApiBody;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user