Skip to content

Commit 2e86cc3

Browse files
committed
🔧 fix StreamFactory::createStreamFromFile() according to https://github.com/http-interop/http-factory-tests
1 parent 0a8e213 commit 2e86cc3

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/Psr17/StreamFactory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
namespace chillerlan\HTTP\Psr17;
1414

1515
use chillerlan\HTTP\Psr7\Stream;
16+
use InvalidArgumentException;
1617
use Psr\Http\Message\{StreamFactoryInterface, StreamInterface};
18+
use RuntimeException;
1719

1820
final class StreamFactory implements StreamFactoryInterface{
1921

@@ -44,6 +46,15 @@ public function createStream(string $content = ''):StreamInterface{
4446
* @return \Psr\Http\Message\StreamInterface
4547
*/
4648
public function createStreamFromFile(string $filename, string $mode = 'r'):StreamInterface{
49+
50+
if(empty($filename) || !is_file($filename)){
51+
throw new RuntimeException('invalid file');
52+
}
53+
54+
if(!isset(Stream::MODES_WRITE[$mode]) && !isset(Stream::MODES_READ[$mode])){
55+
throw new InvalidArgumentException('invalid mode');
56+
}
57+
4758
return new Stream(fopen($filename, $mode));
4859
}
4960

0 commit comments

Comments
 (0)