This repository was archived by the owner on Apr 14, 2024. It is now read-only.
File tree 7 files changed +61
-59
lines changed
7 files changed +61
-59
lines changed Original file line number Diff line number Diff line change @@ -65,7 +65,7 @@ namespace YourAwesomeNamespace;
65
65
66
66
use TheCodingMachine\Gotenberg\Client;
67
67
use TheCodingMachine\Gotenberg\ClientException;
68
- use TheCodingMachine\Gotenberg\Document ;
68
+ use TheCodingMachine\Gotenberg\DocumentFactory ;
69
69
70
70
use GuzzleHttp\Psr7\LazyOpenStream;
71
71
@@ -78,15 +78,13 @@ class YourAwesomeClass {
78
78
$client = new Client('gotenberg:3000');
79
79
80
80
# let's instantiate some documents you wish to convert.
81
- $yourOfficeDocument = new Document('file.docx');
82
- $yourOfficeDocument->feedFromPath('path/to/file');
83
-
84
- $yourHTMLDocument = new Document('file.html');
85
- $yourHTMLDocument->feedFromStream(new LazyOpenStream('path/to/file', 'r'));
81
+ $yourOfficeDocument = DocumentFactory::makeFromPath('file.docx', '/path/to/file');
82
+ $yourHTMLDocument = DocumentFactory::makeFromStream('file.html', new LazyOpenStream('path/to/file', 'r'));
86
83
87
84
# now let's send those documents!
88
85
try {
89
86
# store method allows you to... store the resulting PDF in a particular folder.
87
+ # this method also returns the resulting PDF path.
90
88
$filePath = $client->store([
91
89
$yourOfficeDocument,
92
90
$yourHTMLDocument
Original file line number Diff line number Diff line change 2
2
3
3
namespace TheCodingMachine \Gotenberg ;
4
4
5
- use GuzzleHttp \Psr7 \LazyOpenStream ;
6
5
use Psr \Http \Message \StreamInterface ;
7
6
8
7
class Document
@@ -16,25 +15,11 @@ class Document
16
15
/**
17
16
* Document constructor.
18
17
* @param string $fileName
19
- */
20
- public function __construct (string $ fileName )
21
- {
22
- $ this ->fileName = $ fileName ;
23
- }
24
-
25
- /**
26
- * @param string $filePath
27
- */
28
- public function feedFromPath (string $ filePath ): void
29
- {
30
- $ this ->fileStream = new LazyOpenStream ($ filePath , 'r ' );
31
- }
32
-
33
- /**
34
18
* @param StreamInterface $fileStream
35
19
*/
36
- public function feedFromStream ( StreamInterface $ fileStream ): void
20
+ public function __construct ( string $ fileName , StreamInterface $ fileStream )
37
21
{
22
+ $ this ->fileName = $ fileName ;
38
23
$ this ->fileStream = $ fileStream ;
39
24
}
40
25
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace TheCodingMachine \Gotenberg ;
4
+
5
+ use GuzzleHttp \Psr7 \LazyOpenStream ;
6
+ use Psr \Http \Message \StreamInterface ;
7
+
8
+ class DocumentFactory
9
+ {
10
+ /**
11
+ * @param string $fileName
12
+ * @param string $filePath
13
+ * @return Document
14
+ */
15
+ public static function makeFromPath (string $ fileName , string $ filePath ): Document
16
+ {
17
+ return new Document ($ fileName , new LazyOpenStream ($ filePath , 'r ' ));
18
+ }
19
+
20
+ /**
21
+ * @param string $fileName
22
+ * @param StreamInterface $fileStream
23
+ * @return Document
24
+ */
25
+ public static function makeFromStream (string $ fileName , StreamInterface $ fileStream ): Document
26
+ {
27
+ return new Document ($ fileName , $ fileStream );
28
+ }
29
+ }
Original file line number Diff line number Diff line change @@ -25,19 +25,11 @@ class ClientTest extends TestCase
25
25
26
26
public function setUp ()
27
27
{
28
- $ this ->officeDocument = $ this ->makeDocument ('file.docx ' );
29
- $ this ->htmlDocument = $ this ->makeDocument ('file.html ' );
30
- $ this ->markdownDocument = $ this ->makeDocument ('file.md ' );
31
- $ this ->pdfDocument = $ this ->makeDocument ('file.pdf ' );
32
- $ this ->noExtensionDocument = $ this ->makeDocument ('file ' );
33
- }
34
-
35
- private function makeDocument (string $ fileName ): Document
36
- {
37
- $ document = new Document ($ fileName );
38
- $ document ->feedFromPath (__DIR__ . '/assets/ ' . $ fileName );
39
-
40
- return $ document ;
28
+ $ this ->officeDocument = DocumentFactory::makeFromPath ('file.docx ' , __DIR__ . '/assets/file.docx ' );
29
+ $ this ->htmlDocument = DocumentFactory::makeFromPath ('file.html ' , __DIR__ . '/assets/file.html ' );
30
+ $ this ->markdownDocument = DocumentFactory::makeFromPath ('file.md ' , __DIR__ . '/assets/file.md ' );
31
+ $ this ->pdfDocument = DocumentFactory::makeFromPath ('file.pdf ' , __DIR__ . '/assets/file.pdf ' );
32
+ $ this ->noExtensionDocument = DocumentFactory::makeFromPath ('file ' , __DIR__ . '/assets/file.pdf ' );
41
33
}
42
34
43
35
function testForward ()
@@ -63,7 +55,7 @@ function testForward()
63
55
64
56
function testStore ()
65
57
{
66
- $ client = new Client (self ::API_URL , new \ Http \ Adapter \ Guzzle6 \ Client () );
58
+ $ client = new Client (self ::API_URL );
67
59
$ storingPath = __DIR__ . '/store ' ;
68
60
69
61
// case 1: sends a single document.
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace TheCodingMachine \Gotenberg ;
4
+
5
+ use GuzzleHttp \Psr7 \LazyOpenStream ;
6
+ use PHPUnit \Framework \TestCase ;
7
+
8
+ class DocumentFactoryTest extends TestCase
9
+ {
10
+ function testMake ()
11
+ {
12
+ // case 1: uses a file path.
13
+ $ document = DocumentFactory::makeFromPath ('file.pdf ' , __DIR__ . '/assets/file.pdf ' );
14
+ $ this ->assertNotEmpty ($ document ->getFileStream ());
15
+
16
+ // case 2: uses a stream.
17
+ $ document = DocumentFactory::makeFromStream ('file.pdf ' , new LazyOpenStream (__DIR__ . '/assets/file.pdf ' , 'r ' ));
18
+ $ this ->assertNotEmpty ($ document ->getFileStream ());
19
+ }
20
+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments