AcceptHeader.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 $data 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. $mime = current($elems);
  50. $types = explode('/', $mime);
  51. if (!isset($types[1])) {
  52. continue;
  53. }
  54. $acceptElement = [
  55. 'raw' => $mime,
  56. 'type' => trim($types[0]),
  57. 'subtype' => trim($types[1]),
  58. ];
  59. $acceptElement['params'] = [];
  60. while (next($elems)) {
  61. list($name, $value) = explode('=', current($elems));
  62. $acceptElement['params'][trim($name)] = trim($value);
  63. }
  64. $array[] = $acceptElement;
  65. }
  66. return $array;
  67. }
  68. /**
  69. * Compare two Accepted types with their parameters to know
  70. * if one media type should be used instead of an other
  71. *
  72. * @param array $a The first media type and its parameters
  73. * @param array $b The second media type and its parameters
  74. * @return int
  75. */
  76. private function _compare($a, $b)
  77. {
  78. $a_q = isset($a['params']['q']) ? floatval($a['params']['q']) : 1.0;
  79. $b_q = isset($b['params']['q']) ? floatval($b['params']['q']) : 1.0;
  80. if ($a_q === $b_q) {
  81. $a_count = count($a['params']);
  82. $b_count = count($b['params']);
  83. if ($a_count === $b_count) {
  84. if ($r = $this->_compareSubType($a['subtype'], $b['subtype'])) {
  85. return $r;
  86. } else {
  87. return $this->_compareSubType($a['type'], $b['type']);
  88. }
  89. } else {
  90. return $a_count < $b_count;
  91. }
  92. } else {
  93. return $a_q < $b_q;
  94. }
  95. }
  96. /**
  97. * Compare two subtypes
  98. *
  99. * @param string $a First subtype to compare
  100. * @param string $b Second subtype to compare
  101. * @return int
  102. */
  103. private function _compareSubType($a, $b)
  104. {
  105. if ($a === '*' && $b !== '*') {
  106. return 1;
  107. } elseif ($b === '*' && $a !== '*') {
  108. return -1;
  109. } else {
  110. return 0;
  111. }
  112. }
  113. }