cleaned up composer
This commit is contained in:
+38
-35
@@ -33,17 +33,21 @@ class HeaderUtils
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* HeaderUtils::split("da, en-gb;q=0.8", ",;")
|
||||
* HeaderUtils::split('da, en-gb;q=0.8', ',;')
|
||||
* // => ['da'], ['en-gb', 'q=0.8']]
|
||||
*
|
||||
* @param string $separators List of characters to split on, ordered by
|
||||
* precedence, e.g. ",", ";=", or ",;="
|
||||
* precedence, e.g. ',', ';=', or ',;='
|
||||
*
|
||||
* @return array Nested array with as many levels as there are characters in
|
||||
* $separators
|
||||
*/
|
||||
public static function split(string $header, string $separators): array
|
||||
{
|
||||
if ('' === $separators) {
|
||||
throw new \InvalidArgumentException('At least one separator must be specified.');
|
||||
}
|
||||
|
||||
$quotedSeparators = preg_quote($separators, '/');
|
||||
|
||||
preg_match_all('
|
||||
@@ -77,8 +81,8 @@ class HeaderUtils
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* HeaderUtils::combine([["foo", "abc"], ["bar"]])
|
||||
* // => ["foo" => "abc", "bar" => true]
|
||||
* HeaderUtils::combine([['foo', 'abc'], ['bar']])
|
||||
* // => ['foo' => 'abc', 'bar' => true]
|
||||
*/
|
||||
public static function combine(array $parts): array
|
||||
{
|
||||
@@ -95,13 +99,13 @@ class HeaderUtils
|
||||
/**
|
||||
* Joins an associative array into a string for use in an HTTP header.
|
||||
*
|
||||
* The key and value of each entry are joined with "=", and all entries
|
||||
* The key and value of each entry are joined with '=', and all entries
|
||||
* are joined with the specified separator and an additional space (for
|
||||
* readability). Values are quoted if necessary.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* HeaderUtils::toString(["foo" => "abc", "bar" => true, "baz" => "a b c"], ",")
|
||||
* HeaderUtils::toString(['foo' => 'abc', 'bar' => true, 'baz' => 'a b c'], ',')
|
||||
* // => 'foo=abc, bar, baz="a b c"'
|
||||
*/
|
||||
public static function toString(array $assoc, string $separator): string
|
||||
@@ -138,7 +142,7 @@ class HeaderUtils
|
||||
* Decodes a quoted string.
|
||||
*
|
||||
* If passed an unquoted string that matches the "token" construct (as
|
||||
* defined in the HTTP specification), it is passed through verbatimly.
|
||||
* defined in the HTTP specification), it is passed through verbatim.
|
||||
*/
|
||||
public static function unquote(string $s): string
|
||||
{
|
||||
@@ -154,8 +158,6 @@ class HeaderUtils
|
||||
* is semantically equivalent to $filename. If the filename is already ASCII,
|
||||
* it can be omitted, or just copied from $filename
|
||||
*
|
||||
* @return string A string suitable for use as a Content-Disposition field-value
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @see RFC 6266
|
||||
@@ -176,12 +178,12 @@ class HeaderUtils
|
||||
}
|
||||
|
||||
// percent characters aren't safe in fallback.
|
||||
if (false !== strpos($filenameFallback, '%')) {
|
||||
if (str_contains($filenameFallback, '%')) {
|
||||
throw new \InvalidArgumentException('The filename fallback cannot contain the "%" character.');
|
||||
}
|
||||
|
||||
// path separators aren't allowed in either.
|
||||
if (false !== strpos($filename, '/') || false !== strpos($filename, '\\') || false !== strpos($filenameFallback, '/') || false !== strpos($filenameFallback, '\\')) {
|
||||
if (str_contains($filename, '/') || str_contains($filename, '\\') || str_contains($filenameFallback, '/') || str_contains($filenameFallback, '\\')) {
|
||||
throw new \InvalidArgumentException('The filename and the fallback cannot contain the "/" and "\\" characters.');
|
||||
}
|
||||
|
||||
@@ -254,39 +256,40 @@ class HeaderUtils
|
||||
private static function groupParts(array $matches, string $separators, bool $first = true): array
|
||||
{
|
||||
$separator = $separators[0];
|
||||
$partSeparators = substr($separators, 1);
|
||||
|
||||
$separators = substr($separators, 1) ?: '';
|
||||
$i = 0;
|
||||
|
||||
if ('' === $separators && !$first) {
|
||||
$parts = [''];
|
||||
|
||||
foreach ($matches as $match) {
|
||||
if (!$i && isset($match['separator'])) {
|
||||
$i = 1;
|
||||
$parts[1] = '';
|
||||
} else {
|
||||
$parts[$i] .= self::unquote($match[0]);
|
||||
}
|
||||
}
|
||||
|
||||
return $parts;
|
||||
}
|
||||
|
||||
$parts = [];
|
||||
$partMatches = [];
|
||||
$previousMatchWasSeparator = false;
|
||||
|
||||
foreach ($matches as $match) {
|
||||
if (!$first && $previousMatchWasSeparator && isset($match['separator']) && $match['separator'] === $separator) {
|
||||
$previousMatchWasSeparator = true;
|
||||
$partMatches[$i][] = $match;
|
||||
} elseif (isset($match['separator']) && $match['separator'] === $separator) {
|
||||
$previousMatchWasSeparator = true;
|
||||
if (($match['separator'] ?? null) === $separator) {
|
||||
++$i;
|
||||
} else {
|
||||
$previousMatchWasSeparator = false;
|
||||
$partMatches[$i][] = $match;
|
||||
}
|
||||
}
|
||||
|
||||
$parts = [];
|
||||
if ($partSeparators) {
|
||||
foreach ($partMatches as $matches) {
|
||||
$parts[] = self::groupParts($matches, $partSeparators, false);
|
||||
}
|
||||
} else {
|
||||
foreach ($partMatches as $matches) {
|
||||
$parts[] = self::unquote($matches[0][0]);
|
||||
}
|
||||
|
||||
if (!$first && 2 < \count($parts)) {
|
||||
$parts = [
|
||||
$parts[0],
|
||||
implode($separator, \array_slice($parts, 1)),
|
||||
];
|
||||
foreach ($partMatches as $matches) {
|
||||
if ('' === $separators && '' !== $unquoted = self::unquote($matches[0][0])) {
|
||||
$parts[] = $unquoted;
|
||||
} elseif ($groupedParts = self::groupParts($matches, $separators, false)) {
|
||||
$parts[] = $groupedParts;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user