AcceptHeader.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Note : Code is released under the GNU LGPL
  4. *
  5. * Please do not change the header of this file
  6. *
  7. * This library is free software; you can redistribute it and/or modify it under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License for more details.
  15. */
  16. /**
  17. * The AcceptHeader page will parse and sort the different
  18. * allowed types for the content negociations
  19. *
  20. * @author Pierrick Charron <pierrick@webstart.fr>
  21. */
  22. class AcceptHeader extends \ArrayObject
  23. {
  24. /**
  25. * Constructor
  26. *
  27. * @param string $header Value of the Accept header
  28. * @return void
  29. */
  30. public function __construct($header)
  31. {
  32. $acceptedTypes = $this->_parse($header);
  33. usort($acceptedTypes, [$this, '_compare']);
  34. parent::__construct($acceptedTypes);
  35. }
  36. /**
  37. * Parse the accept header and return an array containing
  38. * all the informations about the Accepted types
  39. *
  40. * @param string $header Value of the Accept header
  41. * @return array
  42. */
  43. private function _parse($data)
  44. {
  45. $array = [];
  46. $items = explode(',', $data);
  47. foreach ($items as $item) {
  48. $elems = explode(';', $item);
  49. $acceptElement = [];
  50. $mime = current($elems);
  51. list($type, $subtype) = explode('/', $mime);
  52. $acceptElement['type'] = trim($type);
  53. $acceptElement['subtype'] = trim($subtype);
  54. $acceptElement['raw'] = $mime;
  55. $acceptElement['params'] = [];
  56. while (next($elems)) {
  57. list($name, $value) = explode('=', current($elems));
  58. $acceptElement['params'][trim($name)] = trim($value);
  59. }
  60. $array[] = $acceptElement;
  61. }
  62. return $array;
  63. }
  64. /**
  65. * Compare two Accepted types with their parameters to know
  66. * if one media type should be used instead of an other
  67. *
  68. * @param array $a The first media type and its parameters
  69. * @param array $b The second media type and its parameters
  70. * @return int
  71. */
  72. private function _compare($a, $b)
  73. {
  74. $a_q = isset($a['params']['q']) ? floatval($a['params']['q']) : 1.0;
  75. $b_q = isset($b['params']['q']) ? floatval($b['params']['q']) : 1.0;
  76. if ($a_q === $b_q) {
  77. $a_count = count($a['params']);
  78. $b_count = count($b['params']);
  79. if ($a_count === $b_count) {
  80. if ($r = $this->_compareSubType($a['subtype'], $b['subtype'])) {
  81. return $r;
  82. } else {
  83. return $this->_compareSubType($a['type'], $b['type']);
  84. }
  85. } else {
  86. return $a_count < $b_count;
  87. }
  88. } else {
  89. return $a_q < $b_q;
  90. }
  91. }
  92. /**
  93. * Compare two subtypes
  94. *
  95. * @param string $a First subtype to compare
  96. * @param string $b Second subtype to compare
  97. * @return int
  98. */
  99. private function _compareSubType($a, $b)
  100. {
  101. if ($a === '*' && $b !== '*') {
  102. return 1;
  103. } elseif ($b === '*' && $a !== '*') {
  104. return -1;
  105. } else {
  106. return 0;
  107. }
  108. }
  109. }