3 namespace seekat\API\ContentType\Handler
;
6 use seekat\API\ContentType\Handler
;
7 use seekat\Exception\InvalidArgumentException
;
8 use seekat\Exception\UnexpectedValueException
;
9 use function seekat\typeof
;
11 final class Base64
implements Handler
{
15 function types() : array {
23 function encode(mixed $data): Body
{
24 if (!is_scalar($data)) {
25 throw new InvalidArgumentException(
26 "BASE64 encoding argument must be scalar, got ".typeof($data));
28 return (new Body
)->append(base64_encode($data));
35 function decode(Body
$body) : string {
36 $data = base64_decode($body, true);
38 if (false === $data) {
39 $e = error_get_last();
40 throw new UnexpectedValueException("Could not decode BASE64: ".
41 ($e ?
$e["message"] : "Unknown error"));