finds best matching mime type based on accept header

@param   \stubbles\peer\http\AcceptHeader  $acceptedMimeTypes
@return  string|null
556109
public function findMatch(AcceptHeader $acceptedMimeTypes)
    {
        if (count($this->mimeTypes) === 0) {
            return 'text/html';
        }

        if (count($acceptedMimeTypes) === 0) {
            reset($this->mimeTypes);
            return current($this->mimeTypes);
        }

        return $acceptedMimeTypes->findMatchWithGreatestPriority($this->mimeTypes);
    }
20491
public static function for_mimetypes($mimetypes)
    {
        foreach ($mimetypes as $mimetype) {
            if ($formatter = self::for_mimetype($mimetype)) {
                return $formatter;
            }
        }

        return false;
    }
1102967
private function retrieveGenericMatches($acceptLanguages, &$matches)
    {
        $genericMatches = [];

        foreach ($acceptLanguages as $option) {
            $option  = array_map('trim', explode(';', $option));
            $locale  = $option[0];
            $quality = $this->getQualityFactor($locale, $option);

            // Unweighted values, get high weight by their position in the list
            $quality          = isset($quality) ? $quality : 1000 - count($matches);
            $matches[$locale] = $quality;

            // If for some reason the Accept-Language header only sends language with country we should make
            // the language without country an accepted option, with a value less than it's parent.
            $localeOptions = explode('-', $locale);
            array_pop($localeOptions);

            while ( ! empty($localeOptions)) {
                //The new generic option needs to be slightly less important than it's base
                $quality -= 0.001;
                $opt      = implode('-', $localeOptions);

                if (empty($genericMatches[$opt]) || $genericMatches[$opt] > $quality) {
                    $genericMatches[$opt] = $quality;
                }

                array_pop($localeOptions);
            }
        }

        return $genericMatches;
    }
573392
public static function get_type_from_mime_type($mimeType) {
        $types = [
            'image/jpeg' => 'jpg',
            'image/png' => 'png',
            'image/gif' => 'gif',
        ];
        if (isset($types[$mimeType])) return $types[$mimeType];
        throw new ImageException(sprintf('Unsupported MIME type: %s', $mimeType), ImageException::UNSUPPORTED_MIME_TYPE);
    }
318117
public function type($mimeTypes) {
        $types = array();

        foreach ((array) $mimeTypes as $mimeType) {
            switch ($mimeType) {
                case 'application': $types += MimeType::getApplicationList(); break;
                case 'audio':       $types += MimeType::getAudioList(); break;
                case 'image':       $types += MimeType::getImageList(); break;
                case 'text':        $types += MimeType::getTextList(); break;
                case 'video':       $types += MimeType::getVideoList(); break;
                default:            $types += MimeType::getSubTypeList($mimeType); break;
            }
        }

        return in_array($this->getFile()->type(), $types);
    }
772429
protected function getWhitelistedMimeTypes()
    {
        $allow = config('cms-upload-module.upload.restrict.mimetypes.allow', null);

        if ( ! is_array($allow) || ! count($allow)) {
            return false;
        }

        return array_map('trim', array_map('strtolower', $allow));
    }
784340
public function set_mime_type($mime)
	{
		if (array_key_exists($mime, static::$supported_formats))
		{
			$mime = static::$supported_formats[$mime];
		}

		$this->set_header('Accept', $mime);
		return $this;
	}
1215804
public function getMimeType()
    {
        if ($this->exists()) {
            $finfo = new \finfo(FILEINFO_MIME_TYPE | FILEINFO_PRESERVE_ATIME);
            $mime = $finfo->file($this->getRealPath());
            return $mime;
        }
        return null;
    }
576743
public function getByMimetype($mimetype)
    {
        if (isset($this->mimetypeMap[$mimetype])) {
            return $this->get($this->mimetypeMap[$mimetype]);
        }

        return null;
    }
1269002
private function isAcceptableContentType(ServerRequestInterface $request, string $contentType): bool
    {
        $acceptHeader = $request->getHeaderLine('Accept');

        if (strpos($acceptHeader, $contentType) !== false) {
            return true;
        }
        // special case for 'xxx+json' and 'xxx+xml' example : 'application/xhtml+xml'
        if (preg_match('/\+(json|xml)/', $acceptHeader, $matches)) {
            $mediaType = 'application/' . $matches[1];
            if ($mediaType === $contentType) {
                return true;
            }
        }
        // special case when accepting all subtypes of the mime 'mime/*'
        if (strpos($acceptHeader, strtok($contentType, '/') . '/*') !== false) {
            return true;
        }
        // special case when accepting all mimes '*/*'
        if (strpos($acceptHeader, '*/*') !== false) {
            return true;
        }

        return false;
    }
3744
protected function _getMimetypes()
    {
        if(!isset($this->_mimetypes))
        {
            $file = $this->getConfig()->file;

            if (is_readable($file)) {
                $this->_mimetypes = json_decode(file_get_contents($file), true);
            }
        }

        return $this->_mimetypes;
    }
188858
protected function determineResponseType($availableTypes)
    {
        $bestType = $this->contentNegotiation->negotiateMedia($availableTypes);
        if ($bestType instanceof Accept\Media\MediaValue) {
            $contentType = $bestType->getValue();
            $subType = $bestType->getSubtype();
            $this->contentType = $contentType;
            return $contentType;
        }
    }
1140247
def _response_mimetype_based_on_accept_header(self):
        
        response_mimetype = None

        if not request.accept_mimetypes:
            response_mimetype = self.default_mimetype
        else:
            all_media_types_wildcard = '*/*'

            for mimetype, q in request.accept_mimetypes:
                if mimetype == all_media_types_wildcard:
                    response_mimetype = self.default_mimetype
                    break
                if mimetype in self.response_formatters:
                    response_mimetype = mimetype
                    break

        return response_mimetype
780701
private function detectFromHeader(ServerRequestInterface $request)
    {
        $mimes = \array_column($this->formats, 'mime-type');
        $headers = \array_merge(...$mimes);

        $accept = $request->getHeaderLine('Accept');
        $mime = $this->getBest($accept, $headers);

        if ($mime !== null) {
            foreach ($this->formats as $format => $data) {
                if (\in_array($mime, $data['mime-type'], true)) {
                    return $format;
                }
            }
        }

        return null;
    }
1217058
protected function getMediaType(Request $request)
    {
        foreach ($request->getAcceptableContentTypes() as $mimeType) {
            if (preg_match('(^([a-z0-9-/.]+)\+.*$)', strtolower($mimeType), $matches)) {
                return $matches[1];
            }
        }

        return 'unknown/unknown';
    }
497134
private MediaType acceptabePlainTextMediaType() {
        final List<MediaType> acceptable = headers.getAcceptableMediaTypes();
        if (acceptable == null || acceptable.size() == 0) {
            return TEXT_PLAIN_TYPE;
        }
        for (final MediaType type : acceptable) {
            if (type.isWildcardType() || (type.isCompatible(TEXT_PLAIN_TYPE) && type.isWildcardSubtype())) {
                return TEXT_PLAIN_TYPE;
            } else if (type.isCompatible(TEXT_PLAIN_TYPE)) {
                return type;
            }
        }
        return null;
    }
299049
protected static function _parseAccept($accept, array $defaults = null)
    {
        if (! empty($accept)) {
            // Get all of the types
            $types = explode(',', $accept);

            foreach ($types as $type) {
                // Split the type into parts
                $parts = explode(';', $type);

                // Make the type only the MIME
                $type = trim(array_shift($parts));

                // Default quality is 1.0
                $options = array('quality' => 1.0);

                foreach ($parts as $part) {
                    // Prevent undefined $value notice below
                    if (strpos($part, '=') === false) {
                        continue;
                    }

                    // Separate the key and value
                    list($key, $value) = explode('=', trim($part));

                    switch ($key) {
                        case 'q':
                            $options['quality'] = (float) trim($value);
                            break;
                        case 'version':
                            $options['version'] = (float) trim($value);
                            break;
                    }
                }

                // Add the accept type and quality
                $defaults[$type] = $options;
            }
        }

        // Make sure that accepts is an array
        $accepts = (array) $defaults;

        // Order by quality
        arsort($accepts);

        return $accepts;
    }