refactor
[m6w6/seekat] / lib / API / ContentType.php
index 929dc0c2c80907eea6ab59aba6dc489b98e1647b..4fccb9f4367362f3cd625a4942a78cfe5c40c077 100644 (file)
@@ -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