Aggiornato Composer

This commit is contained in:
Paolo A
2024-05-17 12:24:19 +00:00
parent 4ac62108b5
commit ec201d75b2
2238 changed files with 38684 additions and 59785 deletions

View File

@@ -17,6 +17,9 @@ final class MultipartStream implements StreamInterface
/** @var string */
private $boundary;
/** @var StreamInterface */
private $stream;
/**
* @param array $elements Array of associative arrays, each containing a
* required "name" key mapping to the form field,
@@ -31,7 +34,7 @@ final class MultipartStream implements StreamInterface
*/
public function __construct(array $elements = [], string $boundary = null)
{
$this->boundary = $boundary ?: sha1(uniqid('', true));
$this->boundary = $boundary ?: bin2hex(random_bytes(20));
$this->stream = $this->createStream($elements);
}
@@ -48,7 +51,7 @@ final class MultipartStream implements StreamInterface
/**
* Get the headers needed before transferring the content of a POST file
*
* @param array<string, string> $headers
* @param string[] $headers
*/
private function getHeaders(array $headers): string
{
@@ -57,7 +60,7 @@ final class MultipartStream implements StreamInterface
$str .= "{$key}: {$value}\r\n";
}
return "--{$this->boundary}\r\n" . trim($str) . "\r\n\r\n";
return "--{$this->boundary}\r\n".trim($str)."\r\n\r\n";
}
/**
@@ -68,6 +71,9 @@ final class MultipartStream implements StreamInterface
$stream = new AppendStream();
foreach ($elements as $element) {
if (!is_array($element)) {
throw new \UnexpectedValueException('An array is expected');
}
$this->addElement($stream, $element);
}
@@ -106,10 +112,15 @@ final class MultipartStream implements StreamInterface
$stream->addStream(Utils::streamFor("\r\n"));
}
/**
* @param string[] $headers
*
* @return array{0: StreamInterface, 1: string[]}
*/
private function createElement(string $name, StreamInterface $stream, ?string $filename, array $headers): array
{
// Set a default content-disposition header if one was no provided
$disposition = $this->getHeader($headers, 'content-disposition');
$disposition = self::getHeader($headers, 'content-disposition');
if (!$disposition) {
$headers['Content-Disposition'] = ($filename === '0' || $filename)
? sprintf(
@@ -121,7 +132,7 @@ final class MultipartStream implements StreamInterface
}
// Set a default content-length header if one was no provided
$length = $this->getHeader($headers, 'content-length');
$length = self::getHeader($headers, 'content-length');
if (!$length) {
if ($length = $stream->getSize()) {
$headers['Content-Length'] = (string) $length;
@@ -129,21 +140,22 @@ final class MultipartStream implements StreamInterface
}
// Set a default Content-Type if one was not supplied
$type = $this->getHeader($headers, 'content-type');
$type = self::getHeader($headers, 'content-type');
if (!$type && ($filename === '0' || $filename)) {
if ($type = MimeType::fromFilename($filename)) {
$headers['Content-Type'] = $type;
}
$headers['Content-Type'] = MimeType::fromFilename($filename) ?? 'application/octet-stream';
}
return [$stream, $headers];
}
private function getHeader(array $headers, string $key)
/**
* @param string[] $headers
*/
private static function getHeader(array $headers, string $key): ?string
{
$lowercaseHeader = strtolower($key);
foreach ($headers as $k => $v) {
if (strtolower($k) === $lowercaseHeader) {
if (strtolower((string) $k) === $lowercaseHeader) {
return $v;
}
}