10 use InvalidArgumentException
;
11 use UnexpectedValueException
;
19 static private $version = 3;
22 * Content type handler map
25 static private $types = [
26 "json" => "self::fromJson",
27 "base64" => "self::fromBase64",
28 "sha" => "self::fromData",
29 "raw" => "self::fromData",
30 "html" => "self::fromData",
31 "diff" => "self::fromData",
32 "patch" => "self::fromData",
33 "text/plain"=> "self::fromData",
37 * Content type abbreviation
43 * Register a content type handler
44 * @param string $type The content type (abbreviation)
45 * @param callable $handler The handler as function(Body $body):mixed;
47 static function register(string $type, callable
$handler) {
48 self
::$types[$type] = $handler;
52 * Check whether a handler is registered for a particular content type
53 * @param string $type The (abbreviated) content type
56 static function registered(string $type) : bool {
57 return isset(self
::$types[$type]);
61 * Unregister a content type handler
64 static function unregister(string $type) {
65 unset(self
::$types[$type]);
69 * Get/set the API version to use
70 * @param int $v if not null, update the API version
71 * @return int the previously set version
73 static function version(int $v = null) : int {
74 $api = self
::$version;
84 * @throws UnexpectedValueException
86 private static function fromJson(Body
$json) {
87 $decoded = json_decode($json);
88 if (!isset($decoded) && json_last_error()) {
89 throw new UnexpectedValueException("Could not decode JSON: ".
90 json_last_error_msg());
98 * @throws UnexpectedValueException
100 private static function fromBase64(Body
$base64) : string {
101 if (false === ($decoded = base64_decode($base64))) {
102 throw new UnexpectedValueException("Could not decode BASE64");
111 private static function fromData(Body
$data) : string {
112 return (string) $data;
116 * @param Header $contentType
117 * @throws InvalidArgumentException
119 function __construct(Header
$contentType) {
120 if (strcasecmp($contentType->name
, "Content-Type")) {
121 throw new InvalidArgumentException(
122 "Expected Content-Type header, got ". $contentType->name
);
124 $vapi = static::version();
125 $this->type
= preg_replace("/
126 (?:application\/(?:vnd\.github(?:\.v$vapi)?)?)
129 | (?:\.[^.+]+)?\+? (json)
130 )/x", "\\1", current(array_keys($contentType->getParams()->params
)));
134 * Get the (abbreviated) content type name
137 function getType() : string {
142 * Parse a response message's body according to its content type
145 * @throws UnexpectedValueException
147 function parseBody(Body
$data) {
148 $type = $this->getType();
149 if (static::registered($type)) {
150 return call_user_func(self
::$types[$type], $data, $type);
152 throw new UnexpectedValueException("Unhandled content type '$type'");