Visitor::acceptance PHP Method

acceptance() public static method

Returns a number between 0 and 1 that defines how "accepted" a specified MIME type is for the visitor's browser using the HTTP Accept header
public static acceptance ( string $type ) : float
$type string MIME type like "text/html"
return float Number between 0 (not accepted) and 1 (very accepted)
    public static function acceptance($type)
    {
        $accept = a::get($_SERVER, 'HTTP_ACCEPT');
        // if there is no header, everything is accepted
        if (!$accept) {
            return 1;
        }
        // check each type in the Accept header
        foreach (str::split($accept, ',') as $item) {
            $item = str::split($item, ';');
            $mime = a::first($item);
            // $item now only contains params
            // check if the type matches
            if (!fnmatch($mime, $type, FNM_PATHNAME)) {
                continue;
            }
            // check for the q param ("quality" of the type)
            foreach ($item as $param) {
                $param = str::split($param, '=');
                if (a::get($param, 0) === 'q' && ($value = a::get($param, 1))) {
                    return (double) $value;
                }
            }
            // no quality param, default to a quality of 1
            return 1;
        }
        // no match at all, the type is not accepted
        return 0;
    }