Skip to content

Commit aa8a6c0

Browse files
committed
improved code style and used more typehints
Signed-off-by: Christoph Massmann <[email protected]>
1 parent 7f055a1 commit aa8a6c0

13 files changed

+87
-147
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ Internally it uses the DomPDF library for PDF generation and TCPDF for merging.
55

66
## Usage
77

8-
### Create PDF document from HTML contents
8+
### Create PDF document from HTML contents
9+
910
```php
1011
// Create a new pdf instance.
1112
$pdf = Vianetz\Pdf\Model\PdfFactory::general()->create();
1213

1314
// Create the document. You can return any kind of HTML content here.
14-
$document = new \Vianetz\Pdf\Model\Document();
15+
$document = new \Vianetz\Pdf\Model\HtmlDocument();
1516
$document->setHtmlContents('<strong>Hello</strong> World!');
1617

1718
// Add our document to the pdf. You can add as many documents as you like

src/Model/EventManagerInterface.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
3-
* Event manager interface class
4-
*
55
* @section LICENSE
66
* This file is created by vianetz <[email protected]>.
77
* The code is distributed under the GPL license.
@@ -27,9 +27,7 @@ interface EventManagerInterface
2727
* Calls all observer callbacks registered for this event
2828
* and multiple observers matching event name pattern
2929
*
30-
* @param string $eventName
3130
* @param array<mixed,mixed> $data
32-
* @return void
3331
*/
34-
public function dispatch($eventName, array $data = []);
32+
public function dispatch(string $eventName, array $data = []): void;
3533
}

src/Model/Generator/Dompdf.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
35
* @section LICENSE
46
* This file is created by vianetz <[email protected]>.
@@ -17,7 +19,7 @@
1719

1820
namespace Vianetz\Pdf\Model\Generator;
1921

20-
use Vianetz\Pdf\Model\DocumentInterface;
22+
use Vianetz\Pdf\Model\HtmlDocumentInterface;
2123

2224
/**
2325
* Known limitations of Dompdf:
@@ -34,7 +36,7 @@ final class Dompdf extends AbstractGenerator
3436
private \Dompdf\Dompdf $domPdf;
3537

3638
/** @throws \Exception */
37-
public function renderPdfDocument(DocumentInterface $documentModel): ?string
39+
public function renderPdfDocument(HtmlDocumentInterface $documentModel): string
3840
{
3941
$this->initPdf();
4042

@@ -43,7 +45,7 @@ public function renderPdfDocument(DocumentInterface $documentModel): ?string
4345

4446
$this->injectPageCount();
4547

46-
return $this->domPdf->output();
48+
return $this->domPdf->output() ?? '';
4749
}
4850

4951
protected function initPdf(): self
@@ -63,7 +65,7 @@ protected function initPdf(): self
6365
*
6466
* @throws \Exception
6567
*/
66-
protected function getHtmlContentsForDocument(DocumentInterface $documentModel): string
68+
protected function getHtmlContentsForDocument(HtmlDocumentInterface $documentModel): string
6769
{
6870
$htmlContents = $documentModel->getHtmlContents();
6971

src/Model/GeneratorInterface.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
3-
* Pdf generator interface class
4-
*
55
* @section LICENSE
66
* This file is created by vianetz <[email protected]>.
77
* The code is distributed under the GPL license.
@@ -19,17 +19,7 @@
1919

2020
namespace Vianetz\Pdf\Model;
2121

22-
/**
23-
* Interface Vianetz_Pdf_Model_Generator_Interface
24-
*/
2522
interface GeneratorInterface
2623
{
27-
/**
28-
* Render the pdf document.
29-
*
30-
* @param DocumentInterface $documentModel
31-
*
32-
* @return string
33-
*/
34-
public function renderPdfDocument(DocumentInterface $documentModel);
24+
public function renderPdfDocument(HtmlDocumentInterface $documentModel): string;
3525
}

src/Model/Document.php renamed to src/Model/HtmlDocument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace Vianetz\Pdf\Model;
2121

22-
class Document implements DocumentInterface
22+
class HtmlDocument implements HtmlDocumentInterface
2323
{
2424
private string $htmlContents;
2525
private string $pdfBackgroundFile = '';

src/Model/DocumentInterface.php renamed to src/Model/HtmlDocumentInterface.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919

2020
namespace Vianetz\Pdf\Model;
2121

22-
/** @todo rename to HtmlDocumentInterface */
23-
interface DocumentInterface
22+
interface HtmlDocumentInterface
2423
{
2524
public function getHtmlContents(): string;
2625

src/Model/NullEventManager.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
35
* @section LICENSE
46
* This file is created by vianetz <[email protected]>.
@@ -19,10 +21,8 @@
1921

2022
final class NullEventManager implements EventManagerInterface
2123
{
22-
/**
23-
* {@inheritDoc}
24-
*/
25-
public function dispatch($eventName, array $data = [])
24+
/** {@inheritDoc} */
25+
public function dispatch(string $eventName, array $data = []): void
2626
{
2727
// do nothing
2828
}

src/Model/Pdf.php

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
35
* Vianetz Public Pdf Model
46
*
@@ -29,35 +31,19 @@
2931

3032
class Pdf implements PdfInterface
3133
{
32-
/**
33-
* The generator instance.
34-
*
35-
* @var GeneratorInterface
36-
*/
37-
private $generator;
38-
39-
/** @var \Vianetz\Pdf\Model\PdfMerge */
40-
private $pdfMerge;
41-
42-
/**
43-
* The (cached) pdf contents.
44-
*
45-
* @var string|null
46-
*/
47-
private $contents;
34+
private GeneratorInterface $generator;
35+
private PdfMerge $pdfMerge;
36+
private ?string $contents = null;
4837

4938
/**
5039
* Initialize empty array for PDF documents to print.
5140
*
52-
* @var array<\Vianetz\Pdf\Model\DocumentInterface|\Vianetz\Pdf\Model\PdfDocumentInterface>
41+
* @var array<\Vianetz\Pdf\Model\HtmlDocumentInterface|\Vianetz\Pdf\Model\PdfDocumentInterface>
5342
*/
54-
private $documents = [];
43+
private array $documents = [];
5544

56-
/** @var Config */
57-
protected $config;
58-
59-
/** @var \Vianetz\Pdf\Model\EventManagerInterface */
60-
protected $eventManager;
45+
protected Config $config;
46+
protected EventManagerInterface $eventManager;
6147

6248
/**
6349
* Default constructor initializes pdf generator.
@@ -76,10 +62,8 @@ final public function __construct(
7662
$this->eventManager = $eventManager;
7763
}
7864

79-
/**
80-
* {@inheritDoc}
81-
*/
82-
final public function getContents()
65+
/** {@inheritDoc} */
66+
final public function getContents(): string
8367
{
8468
if ($this->contents === null) {
8569
$this->renderPdfContentsForAllDocuments();
@@ -89,10 +73,8 @@ final public function getContents()
8973
return $this->contents;
9074
}
9175

92-
/**
93-
* {@inheritDoc}
94-
*/
95-
final public function saveToFile($fileName)
76+
/** {@inheritDoc} */
77+
final public function saveToFile(string $fileName): bool
9678
{
9779
$pdfContents = $this->getContents();
9880

@@ -102,7 +84,7 @@ final public function saveToFile($fileName)
10284
/**
10385
* Add a new document to generate.
10486
*
105-
* @param \Vianetz\Pdf\Model\DocumentInterface|\Vianetz\Pdf\Model\PdfDocumentInterface $documentModel
87+
* @param \Vianetz\Pdf\Model\HtmlDocumentInterface|\Vianetz\Pdf\Model\PdfDocumentInterface $documentModel
10688
*
10789
* @api
10890
*/
@@ -117,10 +99,8 @@ final public function addDocument($documentModel): self
11799

118100
/**
119101
* Returns the number of documents added to the generator.
120-
*
121-
* @return int
122102
*/
123-
final public function countDocuments()
103+
final public function countDocuments(): int
124104
{
125105
return count($this->documents);
126106
}
@@ -131,35 +111,29 @@ final public function countDocuments()
131111
* Note:
132112
* This method only exists for compatibility reasons to provide the same interface as the original Zend_Pdf
133113
* components.
134-
*
135-
* @return string
136114
*/
137-
final public function render()
115+
final public function render(): string
138116
{
139117
return $this->getContents();
140118
}
141119

142-
/**
143-
* @return \Vianetz\Pdf\Model\Config
144-
*/
145-
public function getConfig()
120+
public function getConfig(): Config
146121
{
147122
return $this->config;
148123
}
149124

150125
/**
151126
* Return merged pdf contents of all documents and save it to single temporary files.
152127
*
153-
* @return void
154128
* @throws \Vianetz\Pdf\NoDataException
155129
*/
156-
private function renderPdfContentsForAllDocuments()
130+
private function renderPdfContentsForAllDocuments(): void
157131
{
158132
$hasData = false;
159133
foreach ($this->documents as $documentInstance) {
160134
$this->eventManager->dispatch('vianetz_pdf_document_render_before', ['document' => $documentInstance]);
161135

162-
if ($documentInstance instanceof DocumentInterface) {
136+
if ($documentInstance instanceof HtmlDocumentInterface) {
163137
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_render_before', ['document' => $documentInstance]);
164138

165139
$pdfContents = $this->generator->renderPdfDocument($documentInstance);

src/Model/PdfDocument.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* @section LICENSE
6+
* This file is created by vianetz <[email protected]>.
7+
* The code is distributed under the GPL license.
8+
*
9+
* If you did not receive a copy of the license and are unable to
10+
* obtain it through the world-wide-web, please send an email
11+
* to [email protected] so we can send you a copy immediately.
12+
*
13+
* @package Vianetz\Pdf
14+
* @author Christoph Massmann, <[email protected]>
15+
* @link https://www.vianetz.com
16+
* @copyright Copyright (c) since 2006 vianetz - Dipl.-Ing. C. Massmann (https://www.vianetz.com)
17+
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GENERAL PUBLIC LICENSE
18+
*/
219

320
namespace Vianetz\Pdf\Model;
421

522
final class PdfDocument implements PdfDocumentInterface
623
{
7-
/** @var string */
8-
private $pdfFile;
24+
private string $pdfFile;
925

10-
public function getPdfFile()
26+
public function getPdfFile(): string
1127
{
1228
return $this->pdfFile;
1329
}
1430

15-
public function setPdfFile($pdfFile)
31+
public function setPdfFile(string $pdfFile): void
1632
{
1733
$this->pdfFile = $pdfFile;
1834
}

src/Model/PdfDocumentInterface.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
35
* @section LICENSE
46
* This file is created by vianetz <[email protected]>.
@@ -19,14 +21,7 @@
1921

2022
interface PdfDocumentInterface
2123
{
22-
/**
23-
* @return string
24-
*/
25-
public function getPdfFile();
24+
public function getPdfFile(): string;
2625

27-
/**
28-
* @param string $pdfFile
29-
* @return void
30-
*/
31-
public function setPdfFile($pdfFile);
26+
public function setPdfFile(string $pdfFile): void;
3227
}

0 commit comments

Comments
 (0)