Esempio n. 1
0
 /**
  * Detect the request format based on the priorities and the Accept header
  *
  * Note: Request "_format" parameter is considered the preferred Accept header
  *
  * @param   Request     $request          The request
  * @param   array       $priorities       Ordered array of formats (highest priority first)
  * @param   Boolean     $preferExtension  If to consider the extension last or first
  *
  * @return  void|string                 The format string
  */
 public function getBestFormat(Request $request, array $priorities, $preferExtension = false)
 {
     $mimetypes = $request->splitHttpAcceptHeader($request->headers->get('Accept'));
     $extension = $request->get('_format');
     if (null !== $extension && $request->getMimeType($extension)) {
         $mimetypes[$request->getMimeType($extension)] = $preferExtension ? reset($mimetypes) + 1 : end($mimetypes) - 1;
         arsort($mimetypes);
     }
     if (empty($mimetypes)) {
         return null;
     }
     $catchAllEnabled = in_array('*/*', $priorities);
     return $this->getFormatByPriorities($request, $mimetypes, $priorities, $catchAllEnabled);
 }
 /**
  * Detect the request format based on the priorities and the Accept header
  *
  * Note: Request "_format" parameter is considered the preferred Accept header
  *
  * @param   Request     $request          The request
  * @param   array       $priorities       Ordered array of formats (highest priority first)
  * @param   Boolean     $preferExtension  If to consider the extension last or first
  *
  * @return  void|string                 The format string
  */
 public function getBestFormat(Request $request, array $priorities, $preferExtension = false)
 {
     // BC - Maintain this while 2.0 and 2.1 dont reach their end of life
     // Note: Request::splitHttpAcceptHeader is deprecated since version 2.2, to be removed in 2.3.
     if (class_exists('Symfony\\Component\\HttpFoundation\\AcceptHeader')) {
         $mimetypes = array();
         foreach (AcceptHeader::fromString($request->headers->get('Accept'))->all() as $item) {
             $mimetypes[$item->getValue()] = $item->getQuality();
         }
     } else {
         $mimetypes = $request->splitHttpAcceptHeader($request->headers->get('Accept'));
     }
     $extension = $request->get('_format');
     if (null !== $extension && $request->getMimeType($extension)) {
         $mimetypes[$request->getMimeType($extension)] = $preferExtension ? reset($mimetypes) + 1 : end($mimetypes) - 1;
         arsort($mimetypes);
     }
     if (empty($mimetypes)) {
         return null;
     }
     $catchAllEnabled = in_array('*/*', $priorities);
     return $this->getFormatByPriorities($request, $mimetypes, $priorities, $catchAllEnabled);
 }
 /**
  * @dataProvider splitHttpAcceptHeaderData
  */
 public function testSplitHttpAcceptHeader($acceptHeader, $expected)
 {
     $request = new Request();
     $this->assertEquals($expected, $request->splitHttpAcceptHeader($acceptHeader));
 }
Esempio n. 4
0
 /**
  * @dataProvider splitHttpAcceptHeaderData
  */
 public function testSplitHttpAcceptHeader($acceptHeader, $expected)
 {
     $request = new Request();
     set_error_handler(array($this, "deprecationErrorHandler"));
     $this->assertEquals($expected, $request->splitHttpAcceptHeader($acceptHeader));
     restore_error_handler();
 }