Skip to content

Commit 856ffb2

Browse files
committed
:octocat: +MessageUtil::setContentTypeHeader()
1 parent 61a885d commit 856ffb2

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/MessageUtil.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Psr\Http\Message\{MessageInterface, RequestInterface, ResponseInterface};
1414
use RuntimeException, Throwable;
1515
use function call_user_func, extension_loaded, function_exists, gzdecode, gzinflate, gzuncompress, implode,
16-
in_array, json_decode, json_encode, simplexml_load_string, sprintf, strtolower;
16+
in_array, json_decode, json_encode, simplexml_load_string, sprintf, strtolower, trim;
1717
use const JSON_THROW_ON_ERROR;
1818

1919
/**
@@ -146,4 +146,28 @@ public static function setContentLengthHeader(MessageInterface $message):Message
146146
return $message;
147147
}
148148

149+
/**
150+
* Tries to determine the content type from the given values and sets the Content-Type header accordingly,
151+
* throws if no mime type could be guessed.
152+
*
153+
* @throws \RuntimeException
154+
*/
155+
public static function setContentTypeHeader(
156+
MessageInterface $message,
157+
string $filename = null,
158+
string $extension = null
159+
):MessageInterface{
160+
$mime = (
161+
MimeTypeUtil::getFromExtension(trim(($extension ?? ''), ".\t\n\r\0\x0B"))
162+
?? MimeTypeUtil::getFromFilename(($filename ?? ''))
163+
?? MimeTypeUtil::getFromContent(self::getContents($message))
164+
);
165+
166+
if($mime === null){
167+
throw new RuntimeException('could not determine content type');
168+
}
169+
170+
return $message->withHeader('Content-Type', $mime);
171+
}
172+
149173
}

tests/MessageUtilTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPUnit\Framework\TestCase;
1616
use RuntimeException;
1717
use function extension_loaded;
18+
use function file_get_contents;
1819
use function function_exists;
1920
use function sprintf;
2021
use function str_repeat;
@@ -162,4 +163,29 @@ public function testSetContentLengthHeader():void{
162163
);
163164
}
164165

166+
public static function contentTypeProvider():array{
167+
return [
168+
'text/plain' => ['foo', null, null, 'text/plain'],
169+
'application/json' => ['{}', null, null, 'application/json'],
170+
'text/javascript (name)' => ['{}', 'test.js', null, 'text/javascript'],
171+
'text/javascript (ext)' => ['{}', null, 'js', 'text/javascript'],
172+
'text/x-php' => [file_get_contents(__FILE__), null, null, 'text/x-php'],
173+
'text/markdown' => [file_get_contents(__DIR__.'/../README.md'), null, 'md', 'text/markdown'],
174+
];
175+
}
176+
177+
#[DataProvider('contentTypeProvider')]
178+
public function testSetContentTypeHeader(string $content, ?string $filename, ?string $extension, string $expectedMIME):void{
179+
180+
$message = $this->requestFactory
181+
->createRequest('GET', 'https://example.com')
182+
->withBody($this->streamFactory->createStream($content))
183+
;
184+
185+
$message = MessageUtil::setContentTypeHeader($message, $filename, $extension);
186+
187+
$this::assertTrue($message->hasHeader('content-type'));
188+
$this::assertSame($expectedMIME, $message->getHeaderLine('content-type'));
189+
}
190+
165191
}

tests/MimeTypeUtilTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function testGetMimetypeFromExtension():void{
2525
}
2626

2727
public function testGetMimeTypeFromFileName():void{
28+
$this::assertNull(MimeTypeUtil::getFromFilename(''));
2829
$this::assertSame('application/json', MimeTypeUtil::getFromFilename('/path/to/some/file.json'));
2930
}
3031

0 commit comments

Comments
 (0)