Habari\RestResponse::get_best_mime PHP Метод

get_best_mime() публичный Метод

Determine the best mimetype to respond to the client with based on the accept header and the mimetypes available
public get_best_mime ( array $mime_types = null ) : null | string
$mime_types array An array of available mimetypes
Результат null | string The best mimetype available to return based on the client accept header
    public function get_best_mime($mime_types = null)
    {
        // Values will be stored in this array
        $accept_types = array();
        // Accept header is case insensitive, and whitespace isn’t important
        $accept = strtolower(str_replace(' ', '', $_SERVER['HTTP_ACCEPT']));
        // divide it into parts in the place of a ","
        $accept = explode(',', $accept);
        foreach ($accept as $a) {
            // the default quality is 1.
            $q = 1;
            // check if there is a different quality
            if (strpos($a, ';q=')) {
                // divide "mime/type;q=X" into two parts: "mime/type" and "X"
                list($a, $q) = explode(';q=', $a);
            }
            // mime-type $a is accepted with the quality $q
            // WARNING: $q == 0 means, that mime-type isn’t supported!
            $accept_types[$a] = $q;
        }
        arsort($accept_types);
        // if no parameter was passed, just return parsed data
        if (!$mime_types) {
            return $accept_types;
        }
        $mime_types = array_map('strtolower', (array) $mime_types);
        // let's check our supported types:
        foreach ($accept_types as $mime => $q) {
            if ($q && in_array($mime, $mime_types)) {
                return $mime;
            }
        }
        // no mime-type found
        return null;
    }