diff options
| author | Murilo Ijanc <[email protected]> | 2026-07-26 14:58:55 -0300 |
|---|---|---|
| committer | Murilo Ijanc <[email protected]> | 2026-07-26 14:58:55 -0300 |
| commit | 64764fb9fac0227f8e0443e7c7519d73aec275b9 (patch) | |
| tree | cb4fc56eeb6ecce53854f9c530609cf01ab3349f /src/Response.php | |
| download | php-1fd1470890518a68130b3b9058fe1bebddca9f07.tar.gz | |
Initial importv0.1.0
Diffstat (limited to 'src/Response.php')
| -rw-r--r-- | src/Response.php | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/Response.php b/src/Response.php new file mode 100644 index 0000000..f0ecb8d --- /dev/null +++ b/src/Response.php @@ -0,0 +1,50 @@ +<?php + +declare(strict_types=1); + +namespace Cepis; + +/** + * What a {@see Transport} hands back: an HTTP status, the response headers and + * the body as a string. + * + * Deliberately not a PSR-7 message. This library makes one GET and needs three + * things out of it; a plain value object keeps the {@see Transport} interface + * implementable in a few lines against whatever HTTP layer the host + * application already has. + */ +final class Response +{ + /** + * @param array<string, string|list<string>> $headers Response headers. + * Keys may be in any case and values may be a single string or a list + * of them; {@see Response::header()} handles both, so an implementer + * can pass through whatever their HTTP layer produced. + */ + public function __construct( + public readonly int $status, + public readonly array $headers = [], + public readonly string $body = '', + ) { + } + + /** + * Look a header up case-insensitively, returning the first value or `null` + * when it is absent. + */ + public function header(string $name): ?string + { + foreach ($this->headers as $key => $value) { + if (strcasecmp((string) $key, $name) !== 0) { + continue; + } + if (is_array($value)) { + $value = $value[array_key_first($value)] ?? null; + } + + return is_scalar($value) ? (string) $value : null; + } + + return null; + } +} |