You are here

public function Mimeparse::fitness_and_quality_parsed in Services 7.3

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

Find the best match for a given mime-type against a list of media_ranges that have already been parsed by Mimeparser::parse_media_range()

Returns the fitness and the "q" quality parameter of the best match, or an array [-1, 0] if no match was found. Just as for Mimeparser::quality(), "parsed_ranges" must be an Enumerable of parsed media ranges.

Parameters

string $mime_type:

array $parsed_ranges:

Return value

array ($best_fitness, $best_fit_q)

2 calls to Mimeparse::fitness_and_quality_parsed()
Mimeparse::best_match in servers/rest_server/lib/mimeparse.php
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
Mimeparse::quality_parsed in servers/rest_server/lib/mimeparse.php
Find the best match for a given mime-type against a list of media_ranges that have already been parsed by Mimeparser::parse_media_range()

File

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

Class

Mimeparse

Code

public function fitness_and_quality_parsed($mime_type, $parsed_ranges) {
  $best_fitness = -1;
  $best_fit_q = 0;
  list($target_type, $target_subtype, $target_params) = $this
    ->parse_media_range($mime_type);
  foreach ($parsed_ranges as $item) {
    list($type, $subtype, $params) = $item;
    if (($type == $target_type or $type == "*" or $target_type == "*") && ($subtype == $target_subtype or $subtype == "*" or $target_subtype == "*")) {
      $param_matches = 0;
      foreach ($target_params as $k => $v) {
        if ($k != 'q' && isset($params[$k]) && $v == $params[$k]) {
          $param_matches++;
        }
      }
      $fitness = $type == $target_type ? 100 : 0;
      $fitness += $subtype == $target_subtype ? 10 : 0;
      $fitness += $param_matches;
      if ($fitness > $best_fitness) {
        $best_fitness = $fitness;
        $best_fit_q = $params['q'];
      }
    }
  }
  return array(
    $best_fitness,
    (double) $best_fit_q,
  );
}