You are here

public function Mimeparse::best_match in Services 7.3

Same name and namespace in other branches
  1. 6.3 servers/rest_server/lib/mimeparse.php \Mimeparse::best_match()

Takes a list of supported mime-types and finds the best match for all the media-ranges listed in header. The value of header must be a string that conforms to the format of the HTTP Accept: header. The value of supported is an Enumerable of mime-types

Mimeparser::best_match(array("application/xbel+xml", "text/xml"), "text/*;q=0.5,*\/*; q=0.1") => "text/xml"

Parameters

array $supported:

string $header:

Return value

mixed $mime_type or NULL

File

servers/rest_server/lib/mimeparse.php, line 160

Class

Mimeparse

Code

public function best_match($supported, $header) {
  $parsed_header = explode(',', $header);
  foreach ($parsed_header as $i => $r) {
    $parsed_header[$i] = $this
      ->parse_media_range($r);
  }
  $weighted_matches = array();
  foreach ($supported as $mime_type) {
    $weighted_matches[] = array(
      $this
        ->fitness_and_quality_parsed($mime_type, $parsed_header),
      $mime_type,
    );
  }
  array_multisort($weighted_matches);
  $a = $weighted_matches[count($weighted_matches) - 1];
  return empty($a[0][1]) ? null : $a[1];
}