chore: complete update
This commit is contained in:
61
src/Framework/Http/HeaderManipulator.php
Normal file
61
src/Framework/Http/HeaderManipulator.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Framework\Http;
|
||||
|
||||
/**
|
||||
* Hilfsklasse für erweiterte Header-Funktionen ohne Modifikation der Headers-Klasse
|
||||
*/
|
||||
final readonly class HeaderManipulator
|
||||
{
|
||||
/**
|
||||
* Konvertiert einen Header-String in ein Headers-Objekt
|
||||
*/
|
||||
public static function fromString(string $headersRaw): Headers
|
||||
{
|
||||
$headers = new Headers();
|
||||
// Zeilenumbrüche vereinheitlichen und in Zeilen aufteilen
|
||||
$lines = preg_split('/\r\n|\n|\r/', $headersRaw);
|
||||
|
||||
// Die erste Zeile kann die Status-Zeile sein, die überspringen wir
|
||||
$statusLineFound = false;
|
||||
|
||||
foreach ($lines as $line) {
|
||||
// Leere Zeilen überspringen
|
||||
if (trim($line) === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Status-Zeile (HTTP/1.1 200 OK) überspringen
|
||||
if (!$statusLineFound && preg_match('/^HTTP\/\d\.\d\s+\d+/', $line)) {
|
||||
$statusLineFound = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Header im Format "Name: Wert" parsen
|
||||
if (preg_match('/^([^:]+):\s*(.*)$/', $line, $matches)) {
|
||||
$name = $matches[1];
|
||||
$value = $matches[2];
|
||||
|
||||
$headers = $headers->withAdded($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formatiert Headers für cURL
|
||||
*/
|
||||
public static function formatForCurl(Headers $headers): array
|
||||
{
|
||||
$formattedHeaders = [];
|
||||
|
||||
foreach ($headers->all() as $name => $values) {
|
||||
foreach ($values as $value) {
|
||||
$formattedHeaders[] = "$name: $value";
|
||||
}
|
||||
}
|
||||
|
||||
return $formattedHeaders;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user