X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fseekat;a=blobdiff_plain;f=lib%2FAPI%2FContentType.php;h=4fccb9f4367362f3cd625a4942a78cfe5c40c077;hp=929dc0c2c80907eea6ab59aba6dc489b98e1647b;hb=2451d97f1cb7b97e445b4dd839835b8673a4d0fc;hpb=3958595e9ff27162ae918db1453ddecd4840d481 diff --git a/lib/API/ContentType.php b/lib/API/ContentType.php index 929dc0c..4fccb9f 100644 --- a/lib/API/ContentType.php +++ b/lib/API/ContentType.php @@ -2,14 +2,13 @@ namespace seekat\API; +use seekat\{ + API, Exception\InvalidArgumentException, Exception\UnexpectedValueException +}; use http\{ - Header, - Message\Body + Header, Message\Body }; -use InvalidArgumentException; -use UnexpectedValueException; - class ContentType { /** @@ -31,6 +30,7 @@ class ContentType "diff" => "self::fromData", "patch" => "self::fromData", "text/plain"=> "self::fromData", + "application/octet-stream" => "self::fromStream", ]; /** @@ -78,6 +78,31 @@ class ContentType return $api; } + /** + * @param API $api + * @param string $type + * @return API + */ + static function apply(API $api, string $type) : API { + $part = "[^()<>@,;:\\\"\/\[\]?.=[:space:][:cntrl:]]+"; + if (preg_match("/^$part\/$part\$/", $type)) { + $that = $api->withHeader("Accept", $type); + } else { + switch (substr($type, 0, 1)) { + case "+": + case ".": + case "": + break; + default: + $type = ".$type"; + break; + } + $vapi = static::version(); + $that = $api->withHeader("Accept", "application/vnd.github.v$vapi$type"); + } + return $that; + } + /** * @param Body $json * @return mixed @@ -104,6 +129,15 @@ class ContentType return $decoded; } + + /** + * @param Body $stream + * @return resource stream + */ + private static function fromStream(Body $stream) { + return $stream->getResource(); + } + /** * @param Body $data * @return string