25e6e805875f1abed6ff3cd5d0834ab8ba3b61f2
[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 * @param string $data
21 */
22 function encode($data): Body {
23 if (isset($data) && !is_scalar($data)) {
24 throw new InvalidArgumentException(
25 "Text encoding argument must be scalar, got ".typeof($data));
26 }
27 return (new Body)->append($data);
28 }
29
30 /**
31 * @inheritdoc
32 * @return string
33 */
34 function decode(Body $body) {
35 return (string) $body;
36 }
37 }