Bluz\Proxy\Request::getAccept PHP Method

getAccept() public static method

Get Accept MIME Type
public static getAccept ( array $allowTypes = [] ) : string
$allowTypes array
return string
    public static function getAccept($allowTypes = [])
    {
        static $accept;
        if (!$accept) {
            // get header from request
            $header = self::getHeader('accept');
            // make array if types
            $header = explode(',', $header);
            $header = array_map('trim', $header);
            // result store
            $types = [];
            foreach ($header as $a) {
                // the default quality is 1.
                $q = 1;
                // check if there is a different quality
                if (strpos($a, ';q=') or strpos($a, '; q=')) {
                    // divide "mime/type;q=X" into two parts: "mime/type" i "X"
                    $res = preg_split('/;([ ]?)q=/', $a);
                    $a = $res[0];
                    $q = $res[1];
                }
                // remove other extension
                if (strpos($a, ';')) {
                    $a = substr($a, 0, strpos($a, ';'));
                }
                // mime-type $a is accepted with the quality $q
                // WARNING: $q == 0 means, that mime-type isn’t supported!
                $types[$a] = (double) $q;
            }
            arsort($types);
            $accept = $types;
        }
        // if no parameter was passed, just return parsed data
        if (empty($allowTypes)) {
            return $accept;
        }
        $mimeTypes = array_map('strtolower', $allowTypes);
        // let’s check our supported types:
        foreach ($accept as $mime => $q) {
            if ($q && in_array($mime, $mimeTypes)) {
                return $mime;
            }
        }
        // no mime-type found
        return null;
    }

Usage Example

Example #1
0
            break;
        case 501:
            $title = __("Not Implemented");
            $description = __("The server does not understand or does not support the HTTP method");
            break;
        case 503:
            $title = __("Service Unavailable");
            $description = __("The server is currently unable to handle the request due to a temporary overloading");
            Response::setHeader('Retry-After', '600');
            break;
        default:
            $title = __("Internal Server Error");
            $description = __("An unexpected error occurred with your request. Please try again later");
            break;
    }
    // check CLI or HTTP request
    if (Request::isHttp()) {
        // simple AJAX call, accept JSON
        if (Request::getAccept(['application/json'])) {
            $this->useJson();
            Messages::addError($description);
            return null;
        }
        // dialog AJAX call, accept HTML
        if (!Request::isXmlHttpRequest()) {
            $this->useLayout('small.phtml');
        }
    }
    Layout::title($title);
    return ['error' => $title, 'description' => $description];
};
All Usage Examples Of Bluz\Proxy\Request::getAccept