/** * @param string $url * @param array $params * * @return array */ private function discoverLinks($url, array $params = array()) { $request = new Psr7\Request('get', $url); $response = $this->client->send($request); $xpath = isset($params['format']) ? self::xpathForFormat($params['format']) : self::LINK_ANY_XPATH; $links = self::responseBodyOEmbedLinks($response, $xpath); if (!empty($links) && isset($params['format'])) { $links = array_filter($links, function ($link) use($params) { return $this->negotiator->getFormat($link[1]) !== null && $params['format'] === $this->negotiator->getFormat($link[1]); }); } return $links; }
/** * @param array $params * @param ResponseInterface $response * * @return null|string * * @throws UnknownFormatException */ private function getFormat(array $params, ResponseInterface $response) { if ($response->hasHeader('content-type')) { $header = $response->getHeaderLine('content-type'); $format = $this->negotiator->getFormat($header); } if (isset($params['format'])) { $format = $params['format']; } if (!isset($format)) { throw new UnknownFormatException('Unable to figure out the format'); } return $format; }
/** * Assign the format to use to the _api_format Request attribute. * * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); if (!$request->attributes->get('_resource_type')) { return; } // Use the Symfony request format if available and applicable $format = $request->getRequestFormat(null); if (null === $format || !in_array($format, $this->supportedFormats)) { if (null !== ($accept = $request->headers->get('Accept'))) { // Try to guess the best format to use $format = $this->formatNegotiator->getBestFormat($accept, $this->supportedFormats); } } $request->attributes->set('_api_format', $format ?: $this->supportedFormats[0]); }
/** * Decodes the request body * * @param Request $request The request * * @return void */ protected function decodeBody(Request $request) { if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH', 'DELETE'])) { $contentType = $request->headers->get('Content-Type', ''); $format = $this->formatNegotiator->getFormat($contentType); if (!$this->contentDecoder->supportsDecoding($format)) { return; } $content = $request->getContent(); if (!empty($content)) { try { $data = $this->contentDecoder->decode($content, $format); } catch (Exception $exception) { $data = null; } if (is_array($data)) { $request->request->replace($data); } else { $message = sprintf('Invalid %s message received', $format); throw new BadRequestHttpException($message); } } } }