*/ public array $data; /** * @param Method $method HTTP method * @param Headers $headers Request headers * @param string $body Raw request body * @param array $parsedData Pre-parsed data (from parser) */ public function __construct( Method $method, Headers $headers, string $body, /** @var array $parsedData Pre-parsed data (from parser) */ array $parsedData ) { // For GET requests, data comes from query parameters (passed as parsedData) if ($method === Method::GET) { $this->data = $parsedData; return; } // For other methods, check if we have pre-parsed data if (! empty($parsedData)) { $this->data = $parsedData; return; } // If no pre-parsed data, parse based on content type $contentType = $headers->get(HeaderKey::CONTENT_TYPE); // Normalize content type if (is_array($contentType)) { $contentType = $contentType[0] ?? ''; } elseif ($contentType === null) { $contentType = ''; } // Parse based on content type if (str_contains($contentType, MimeType::APPLICATION_JSON->value)) { $this->data = json_decode($body, true) ?? []; } elseif (str_contains($contentType, MimeType::APPLICATION_FORM->value)) { parse_str($body, $parsedBody); $this->data = $parsedBody; } else { // For other content types or empty body $this->data = []; } } /** * Get a value from the parsed body * * @param string $key The key to retrieve * @param mixed $default Default value if key doesn't exist * @return mixed The value or default */ public function get(string $key, mixed $default = null): mixed { return $this->data[$key] ?? $default; } /** * Check if a key exists in the parsed body * * @param string $key The key to check * @return bool */ public function has(string $key): bool { return isset($this->data[$key]); } /** * Get all parsed data * * @return array */ public function all(): array { return $this->data; } /** * Get a subset of the data by keys * * @param array $keys Keys to include * @return array */ public function only(array $keys): array { return array_intersect_key($this->data, array_flip($keys)); } /** * Get all data except specified keys * * @param array $keys Keys to exclude * @return array */ public function except(array $keys): array { return array_diff_key($this->data, array_flip($keys)); } /** * Get all parsed data as array (alias for all() method) * * @return array */ public function toArray(): array { return $this->data; } }