update to PHP-8.1
[m6w6/seekat] / lib / API / ContentType / Handler / Text.php
1 <?php
2
3 namespace seekat\API\ContentType\Handler;
4
5 use http\Message\Body;
6 use seekat\API\ContentType\Handler;
7 use seekat\Exception\InvalidArgumentException;
8 use function seekat\typeof;
9
10 final class Text implements Handler {
11 /**
12 * @inheritdoc
13 */
14 function types() : array {
15 return ["sha", "raw", "html", "diff", "patch", "text/plain"];
16 }
17
18 /**
19 * @inheritdoc
20 */
21 function encode(mixed $data): Body {
22 if (isset($data) && !is_scalar($data)) {
23 throw new InvalidArgumentException(
24 "Text encoding argument must be scalar, got ".typeof($data));
25 }
26 return (new Body)->append((string) $data);
27 }
28
29 /**
30 * @inheritdoc
31 * @return string
32 */
33 function decode(Body $body) : string {
34 return (string) $body;
35 }
36 }