/** * Middleware callback used to check MIME types are OK. * It is not intended that you call this function yourself. * @throws \InvalidArgumentException Exception thrown if callback invoked incorrectly. */ public function callbackCheckFormats() { // get the acceptable MIME type from route info $args = func_get_args(); if (count($args) === 0 || !$args[0] instanceof \Slim\Route) { throw new \InvalidArgumentException('This method should not be invoked outside of the Slim Framework'); } $route = $args[0]; $id = $this->app->request()->getMethod() . $route->getPattern(); $acceptableMime = $this->routeInfo[$id]['mimeTypes']; // perform MIME-type matching on the requested and available formats // note that if there are no q-values, the *LAST* type "wins" $conneg = new \ptlis\ConNeg\Negotiation(); $this->mimeBest = $conneg->mimeBest($_SERVER['HTTP_ACCEPT'], $acceptableMime); if (!$this->mimeBest) { // TODO: need to verify that this is the expected return if there are no matches $this->outputError(406, 'Not Acceptable', '</p><p>This service cannot return information in the format(s) you requested.', 'Sorry, we cannot serve you data in your requested format'); } // store alternative MIME types foreach ($conneg->mimeAll('*/*', $acceptableMime) as $type) { $type = $type->getClientPreference(); // TODO: this stores objects of type ptlis\ConNeg\Preference\Preference // but we want to have a list of content types if ($type != $this->mimeBest) { $this->mimeAlternatives[] = $type; } } // return best match return $this->mimeBest; }
/** * @dataProvider mimeProvider * * @param string $clientField * @param string $serverField * @param string $best * @param MatchedPreferenceInterface[] $all */ public function testBest($clientField, $serverField, $best, array $all) { $negotiate = new Negotiation(); $resultType = $negotiate->mimeBest($clientField, $serverField); $this->assertEquals($best, $resultType); }