@@ -349,13 +349,16 @@ protected function setAcceptType()
349
349
{
350
350
$ this ->acceptType = 'html ' ;
351
351
352
- $ rawAcceptType = $ this ->request ->getHeader ('Accept ' );
353
- if (strpos ($ rawAcceptType , 'json ' ) !== false ) {
354
- $ this ->acceptType = 'json ' ;
355
- } elseif (strpos ($ rawAcceptType , 'html ' ) !== false ) {
356
- $ this ->acceptType = 'html ' ;
357
- } elseif (strpos ($ rawAcceptType , 'xml ' ) !== false ) {
358
- $ this ->acceptType = 'xml ' ;
352
+ $ AcceptTypes = $ this ->getSortedAcceptHeader ();
353
+ foreach ($ AcceptTypes as $ key => $ value ) {
354
+ if (strpos ($ key , 'json ' ) !== false ) {
355
+ $ this ->acceptType = 'json ' ;
356
+ } elseif (strpos ($ key , 'html ' ) !== false ) {
357
+ $ this ->acceptType = 'html ' ;
358
+ } elseif (strpos ($ key , 'xml ' ) !== false ) {
359
+ $ this ->acceptType = 'xml ' ;
360
+ }
361
+ break ;
359
362
}
360
363
}
361
364
@@ -408,4 +411,27 @@ public function getUiComponentFactory()
408
411
{
409
412
return $ this ->uiComponentFactory ;
410
413
}
414
+
415
+ /**
416
+ * Returns sorted accept header based on q value
417
+ *
418
+ * @return array
419
+ */
420
+ private function getSortedAcceptHeader ()
421
+ {
422
+ $ AcceptTypes = [];
423
+ $ rawAcceptType = $ this ->request ->getHeader ('Accept ' );
424
+ $ accept = explode (', ' , $ rawAcceptType );
425
+ foreach ($ accept as $ a ) {
426
+ // the default quality is 1.
427
+ $ q = 1 ;
428
+ // check if there is a different quality
429
+ if (strpos ($ a , ';q= ' ) !== false ) {
430
+ list ($ a , $ q ) = explode (';q= ' , $ a );
431
+ }
432
+ $ AcceptTypes [$ a ] = $ q ;
433
+ }
434
+ arsort ($ AcceptTypes );
435
+ return $ AcceptTypes ;
436
+ }
411
437
}
0 commit comments