Altered MediaType parse routine.

This commit is contained in:
Pachira 2023-02-01 20:24:42 +00:00
parent 1a8344c1c3
commit bce5ba77a2
2 changed files with 12 additions and 13 deletions

View file

@ -1 +1 @@
0.2301.252226
0.2302.12024

View file

@ -1,7 +1,7 @@
<?php
// MediaType.php
// Created: 2022-02-10
// Updated: 2022-02-27
// Updated: 2023-02-01
namespace Index;
@ -133,25 +133,24 @@ class MediaType implements Stringable, IComparable, IEquatable {
}
public static function parse(string $mediaTypeStr): MediaType {
if(preg_match('#^([A-Za-z0-9\!\#\$%&\'\*\+\.\-_\{\}\|]+)/([A-Za-z0-9\!\#\$%&\'\*\+\.\-_\{\}\|]+)(?: ?; ?([A-Za-z0-9\!\#\$%&\'\*\+\.\-_\{\}\|\=; ]+))?$#', $mediaTypeStr, $matches) !== 1)
$parts = explode(';', $mediaTypeStr);
if(empty($parts))
throw new InvalidArgumentException('Invalid media type supplied.');
$category = $matches[1];
$mediaTypeStr = array_shift($parts);
$mediaTypeParts = explode('/', $mediaTypeStr, 2);
$kindSplit = explode('+', $matches[2], 2);
$category = $mediaTypeParts[0];
$kindSplit = explode('+', $mediaTypeParts[1] ?? '', 2);
$kind = $kindSplit[0];
$suffix = $kindSplit[1] ?? '';
$params = [];
if(isset($matches[3])) {
$rawParams = explode(';', $matches[3]);
foreach($rawParams as $param) {
$parts = explode('=', trim($param), 2);
if(!isset($parts[1]))
continue;
$params[$parts[0]] = $parts[1];
}
foreach($parts as $part) {
$paramSplit = explode('=', trim($part), 2);
$params[trim($paramSplit[0])] = trim($paramSplit[1] ?? '', " \n\r\t\v\0\"");
}
return new MediaType($category, $kind, $suffix, $params);