summaryrefslogtreecommitdiffstats
path: root/lib/cepis/Response.php
diff options
context:
space:
mode:
authorMurilo Ijanc <[email protected]>2026-07-26 16:41:48 -0300
committerMurilo Ijanc <[email protected]>2026-07-26 16:47:14 -0300
commit535dbda6b48171f8115b455e54790de94d97d42a (patch)
tree5c0410011e61a883bdf063ee612875a637fb1d22 /lib/cepis/Response.php
downloadwoocommerce-535dbda6b48171f8115b455e54790de94d97d42a.tar.gz
Initial importv0.1.0
Diffstat (limited to 'lib/cepis/Response.php')
-rw-r--r--lib/cepis/Response.php50
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/cepis/Response.php b/lib/cepis/Response.php
new file mode 100644
index 0000000..f0ecb8d
--- /dev/null
+++ b/lib/cepis/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;
+ }
+}