Skip to content

Commit 7af2bf3

Browse files
committed
streamlined html and pdf document interfaces
Signed-off-by: Christoph Massmann <[email protected]>
1 parent d8641d8 commit 7af2bf3

13 files changed

+59
-89
lines changed

src/Model/DocumentInterface.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?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+
*/
19+
20+
namespace Vianetz\Pdf\Model;
21+
22+
interface DocumentInterface
23+
{
24+
public function getContents(): string;
25+
}

src/Model/Generator/AbstractGenerator.php

Lines changed: 10 additions & 45 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 abstract class
4-
*
55
* @section LICENSE
66
* This file is created by vianetz <[email protected]>.
77
* The code is distributed under the GPL license.
@@ -19,72 +19,37 @@
1919

2020
namespace Vianetz\Pdf\Model\Generator;
2121

22+
use Vianetz\Pdf\Model\Config;
2223
use Vianetz\Pdf\Model\GeneratorInterface;
2324

2425
/**
25-
* Class Vianetz_Pdf_Model_Abstract
26-
*
2726
* A note on PDF merging:
2827
* - each generator instance generates the PDF of one source
2928
* - the merge() method in this class takes care of the merging of the single files
3029
* This is necessary because the documents may contain source dependent information in header/footer like invoice id,
3130
* addresses, etc. that cannot be determined correctly if you put all in one file.
3231
*
3332
* A note on naming convention:
34-
* - "document" means the PDF representation of a source that serves as input for the generator
33+
* - "document" is the PDF contents of a source that serves as input for the generator (e.g. html string)
3534
*/
3635
abstract class AbstractGenerator implements GeneratorInterface
3736
{
38-
/** @var string */
39-
const DEBUG_FILE_NAME = 'vianetz_pdf_generator_debug.html';
37+
public const DEBUG_FILE_NAME = 'vianetz_pdf_generator_debug.html';
38+
protected Config $config;
4039

41-
/** @var \Vianetz\Pdf\Model\Config $config */
42-
protected $config;
43-
44-
/**
45-
* Constructor initializes configuration values.
46-
*
47-
* @param \Vianetz\Pdf\Model\Config|null $config
48-
*/
49-
public function __construct(\Vianetz\Pdf\Model\Config $config = null)
40+
public function __construct(?Config $config = null)
5041
{
51-
if (empty($config) === true) {
52-
$config = new \Vianetz\Pdf\Model\Config();
53-
}
54-
55-
$this->config = $config;
56-
57-
$this->initGenerator();
42+
$this->config = $config ?? new Config();
5843
}
5944

60-
/**
61-
* Initialize the generator instance.
62-
*
63-
* @return \Vianetz\Pdf\Model\Generator\AbstractGenerator
64-
*/
65-
protected function initGenerator()
45+
protected function replaceSpecialChars(string $htmlContents): string
6646
{
67-
// By default we do nothing..
68-
return $this;
69-
}
70-
71-
/**
72-
* Replace special characters for DomPDF library.
73-
*
74-
* @param string $htmlContents
75-
*
76-
* @return string
77-
*/
78-
protected function replaceSpecialChars($htmlContents)
79-
{
80-
// Nothing to do at the moment.
81-
8247
return $htmlContents;
8348
}
8449

8550
protected function writeDebugFile(string $fileContents): bool
8651
{
87-
if ($this->config->isDebugMode() === false) {
52+
if (! $this->config->isDebugMode()) {
8853
return false;
8954
}
9055

src/Model/Generator/Dompdf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected function initPdf(): self
6767
*/
6868
protected function getHtmlContentsForDocument(HtmlDocumentInterface $documentModel): string
6969
{
70-
$htmlContents = $documentModel->getHtmlContents();
70+
$htmlContents = $documentModel->getContents();
7171

7272
$htmlContents = $this->replaceSpecialChars($htmlContents);
7373
$this->writeDebugFile($htmlContents);

src/Model/HtmlDocument.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(string $htmlContents, ?string $pdfBackgroundFile = n
3232
$this->pdfBackgroundFileForFirstPage = $pdfBackgroundFileForFirstPage;
3333
}
3434

35-
public function getHtmlContents(): string
35+
public function getContents(): string
3636
{
3737
return $this->htmlContents;
3838
}
@@ -42,18 +42,8 @@ public function getPdfBackgroundFile(): ?string
4242
return $this->pdfBackgroundFile;
4343
}
4444

45-
public function setPdfBackgroundFile(string $pdfBackgroundFile): void
46-
{
47-
$this->pdfBackgroundFile = $pdfBackgroundFile;
48-
}
49-
5045
public function getPdfBackgroundFileForFirstPage(): ?string
5146
{
5247
return $this->pdfBackgroundFileForFirstPage;
5348
}
54-
55-
public function setPdfBackgroundFileForFirstPage(string $pdfBackgroundFileForFirstPage): void
56-
{
57-
$this->pdfBackgroundFileForFirstPage = $pdfBackgroundFileForFirstPage;
58-
}
5949
}

src/Model/HtmlDocumentInterface.php

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

2020
namespace Vianetz\Pdf\Model;
2121

22-
interface HtmlDocumentInterface
22+
interface HtmlDocumentInterface extends DocumentInterface
2323
{
24-
public function getHtmlContents(): string;
25-
2624
public function getPdfBackgroundFile(): ?string;
2725

2826
public function getPdfBackgroundFileForFirstPage(): ?string;

src/Model/Merger/AbstractMerger.php

Lines changed: 2 additions & 2 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 abstract class
4-
*
55
* @section LICENSE
66
* This file is created by vianetz <[email protected]>.
77
* The code is distributed under the GPL license.

src/Model/MergerInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
declare(strict_types=1);
33

44
/**
5-
* Pdf merger interface class
6-
*
75
* @section LICENSE
86
* This file is created by vianetz <[email protected]>.
97
* The code is distributed under the GPL license.

src/Model/NullEventManager.php renamed to src/Model/NoneEventManager.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-
final class NullEventManager implements EventManagerInterface
22+
final class NoneEventManager implements EventManagerInterface
2323
{
2424
/** {@inheritDoc} */
2525
public function dispatch(string $eventName, array $data = []): void

src/Model/Pdf.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ private function renderPdfContentsForAllDocuments(): void
131131
{
132132
$hasData = false;
133133
foreach ($this->documents as $documentInstance) {
134-
$this->eventManager->dispatch('vianetz_pdf_document_render_before', ['document' => $documentInstance]);
134+
$this->eventManager->dispatch('vianetz_pdf_document_render_before', [
135+
'document' => $documentInstance,
136+
'merger' => $this->pdfMerge,
137+
]);
135138

136139
if ($documentInstance instanceof HtmlDocumentInterface) {
137140
$pdfContents = $this->generator->renderPdfDocument($documentInstance);
@@ -142,16 +145,14 @@ private function renderPdfContentsForAllDocuments(): void
142145
$hasData = true;
143146

144147
$this->pdfMerge->mergePdfString($pdfContents, $documentInstance->getPdfBackgroundFile(), $documentInstance->getPdfBackgroundFileForFirstPage());
145-
146-
$this->eventManager->dispatch('vianetz_pdf_document_merge_after', [
147-
'merger' => $this->pdfMerge,
148-
'document' => $documentInstance,
149-
]);
150148
} elseif ($documentInstance instanceof PdfDocumentInterface) {
151-
$this->pdfMerge->mergePdfFile($documentInstance->getPdfFile());
149+
$this->pdfMerge->mergePdfFile($documentInstance->getContents());
152150
}
153151

154-
$this->eventManager->dispatch('vianetz_pdf_document_render_after', ['document' => $documentInstance]);
152+
$this->eventManager->dispatch('vianetz_pdf_document_render_after', [
153+
'document' => $documentInstance,
154+
'merger' => $this->pdfMerge,
155+
]);
155156
}
156157

157158
if (! $hasData) {

src/Model/PdfDocument.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919

2020
namespace Vianetz\Pdf\Model;
2121

22-
final class PdfDocument implements PdfDocumentInterface
22+
class PdfDocument implements PdfDocumentInterface
2323
{
24-
private string $pdfFile;
24+
protected string $pdfFile;
2525

26-
public function getPdfFile(): string
26+
public function __construct(string $pdfFile)
2727
{
28-
return $this->pdfFile;
28+
$this->pdfFile = $pdfFile;
2929
}
3030

31-
public function setPdfFile(string $pdfFile): void
31+
public function getContents(): string
3232
{
33-
$this->pdfFile = $pdfFile;
33+
return $this->pdfFile;
3434
}
3535
}

0 commit comments

Comments
 (0)