Skip to content
This repository was archived by the owner on Apr 14, 2024. It is now read-only.

Commit c556fe9

Browse files
author
Julien Neuhart
authored
Merge pull request #6 from tigitz/master
adding document creation from string
2 parents bc7e8d3 + 06d76c3 commit c556fe9

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/DocumentFactory.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use GuzzleHttp\Psr7\LazyOpenStream;
66
use Psr\Http\Message\StreamInterface;
7+
use Safe\Exceptions\FilesystemException;
8+
use function GuzzleHttp\Psr7\stream_for;
9+
use function Safe\fopen;
10+
use function Safe\fwrite;
711

812
final class DocumentFactory
913
{
@@ -26,4 +30,18 @@ public static function makeFromStream(string $fileName, StreamInterface $fileStr
2630
{
2731
return new Document($fileName, $fileStream);
2832
}
33+
34+
/**
35+
* @param string $fileName
36+
* @param string $string
37+
* @return Document
38+
* @throws FilesystemException
39+
*/
40+
public static function makeFromString(string $fileName, string $string): Document
41+
{
42+
$fileStream = fopen('php://memory', 'rb+');
43+
fwrite($fileStream, $string);
44+
45+
return new Document($fileName, stream_for($fileStream));
46+
}
2947
}

tests/DocumentFactoryTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@ function testMake()
1515
// case 2: uses a stream.
1616
$document = DocumentFactory::makeFromStream('file.pdf', new LazyOpenStream(__DIR__ . '/assets/file.pdf', 'r'));
1717
$this->assertNotEmpty($document->getFileStream());
18+
// case 3: uses a string
19+
$document = DocumentFactory::makeFromString('index.html', '<html>foo</html>');
20+
$this->assertNotEmpty($document->getFileStream());
1821
}
1922
}

0 commit comments

Comments
 (0)