PHP8
[m6w6/seekat] / lib / API / ContentType / Handler / Text.php
diff --git a/lib/API/ContentType/Handler/Text.php b/lib/API/ContentType/Handler/Text.php
new file mode 100644 (file)
index 0000000..25e6e80
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+
+namespace seekat\API\ContentType\Handler;
+
+use http\Message\Body;
+use seekat\API\ContentType\Handler;
+use seekat\Exception\InvalidArgumentException;
+use function seekat\typeof;
+
+final class Text implements Handler {
+       /**
+        * @inheritdoc
+        */
+       function types() : array {
+               return ["sha", "raw", "html", "diff", "patch", "text/plain"];
+       }
+
+       /**
+        * @inheritdoc
+        * @param string $data
+        */
+       function encode($data): Body {
+               if (isset($data) && !is_scalar($data)) {
+                       throw new InvalidArgumentException(
+                               "Text encoding argument must be scalar, got ".typeof($data));
+               }
+               return (new Body)->append($data);
+       }
+
+       /**
+        * @inheritdoc
+        * @return string
+        */
+       function decode(Body $body) {
+               return (string) $body;
+       }
+}