PHP8
[m6w6/seekat] / lib / API / ContentType / Handler / Base64.php
diff --git a/lib/API/ContentType/Handler/Base64.php b/lib/API/ContentType/Handler/Base64.php
new file mode 100644 (file)
index 0000000..979c97d
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+
+namespace seekat\API\ContentType\Handler;
+
+use http\Message\Body;
+use seekat\API\ContentType\Handler;
+use seekat\Exception\InvalidArgumentException;
+use seekat\Exception\UnexpectedValueException;
+use function seekat\typeof;
+
+final class Base64 implements Handler {
+       /**
+        * @inheritdoc
+        */
+       function types() : array {
+               return ["base64"];
+       }
+
+       /**
+        * @inheritdoc
+        * @param string $data
+        */
+       function encode($data): Body {
+               if (!is_scalar($data)) {
+                       throw new InvalidArgumentException(
+                               "BASE64 encoding argument must be scalar, got ".typeof($data));
+               }
+               return (new Body)->append(base64_encode($data));
+       }
+
+       /**
+        * @inheritdoc
+        * @return string
+        */
+       function decode(Body $body) {
+               $data = base64_decode($body, true);
+
+               if (false === $data) {
+                       $e = error_get_last();
+                       throw new UnexpectedValueException("Could not decode BASE64: ".
+                               ($e ? $e["message"] : "Unknown error"));
+               }
+
+               return $data;
+       }
+}