/** * This helper is called when you need directly rendering a data structure * according http accept header request. * It is used in error management to render errors. */ public static function render($data, array $renderers) { $header = array_key_exists('HTTP_ACCEPT', $_SERVER) ? $_SERVER['HTTP_ACCEPT'] : false; $best_match = $header ? Mimeparse::bestMatch(array_keys($renderers), $header) : reset($renderers); $renderer = $best_match ? $renderers[$best_match] : 'json_encode'; //fallback to json return call_user_func($renderer, $data); }
/** * @covers Bitworking\Mimeparse::bestMatch */ public function testBestMatchWithTies() { $supportedMimeTypes1 = array('text/html', 'application/json', 'application/hal+xml', 'application/hal+json'); $supportedMimeTypes2 = array('text/html', 'application/hal+json', 'application/json', 'application/hal+xml'); $httpAcceptHeader = 'application/*, text/*;q=0.8'; $this->assertEquals('application/json', Mimeparse::bestMatch($supportedMimeTypes1, $httpAcceptHeader)); $this->assertEquals('application/hal+json', Mimeparse::bestMatch($supportedMimeTypes2, $httpAcceptHeader)); }