Skip to content

Commit fe665c6

Browse files
committed
refactored fpdi merger and config model a bit to use typehints
Signed-off-by: Christoph Massmann <[email protected]>
1 parent 70e4d60 commit fe665c6

File tree

4 files changed

+32
-94
lines changed

4 files changed

+32
-94
lines changed

src/Model/Config.php

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

44
/**
5-
* Vianetz Pdf Options Model
6-
*
75
* @section LICENSE
86
* This file is created by vianetz <[email protected]>.
97
* The code is distributed under the GPL license.
@@ -32,7 +30,7 @@ final class Config
3230
private string $pdfAuthor = '';
3331
private string $pdfTitle = '';
3432
private bool $isDebugMode = false;
35-
private string $tempDir;
33+
private string $tempDir = '';
3634
private string $chrootDir = '/';
3735

3836
public function __construct()

src/Model/Merger/AbstractMerger.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,13 @@ abstract class AbstractMerger implements MergerInterface
2525
{
2626
/**
2727
* Add the background pdf (if enabled and file exists).
28-
*
29-
* @param string $pdfBackgroundFile
30-
*
31-
* @return \Vianetz\Pdf\Model\Merger\AbstractMerger
3228
*/
33-
public function importBackgroundTemplateFile($pdfBackgroundFile)
29+
public function importBackgroundTemplateFile(string $pdfBackgroundFile): void
3430
{
3531
if (empty($pdfBackgroundFile) || ! file_exists($pdfBackgroundFile)) {
36-
return $this;
32+
return;
3733
}
3834

3935
$this->importPageFromFile($pdfBackgroundFile, 1); // We assume the background pdf has only one page.
40-
41-
return $this;
4236
}
4337
}

src/Model/Merger/Fpdi.php

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,21 @@
2828
final class Fpdi extends AbstractMerger
2929
{
3030
/** @var string */
31-
const OUTPUT_MODE_STRING = 'S';
31+
private const OUTPUT_MODE_STRING = 'S';
3232

3333
/** @var string */
34-
const OUTPUT_FORMAT_LANDSCAPE = 'L';
34+
private const OUTPUT_FORMAT_LANDSCAPE = 'L';
3535

3636
/** @var string */
37-
const OUTPUT_FORMAT_PORTRAIT = 'P';
37+
private const OUTPUT_FORMAT_PORTRAIT = 'P';
3838

39-
/** @var FpdiModel */
40-
private $fpdiModel;
39+
private FpdiModel $fpdiModel;
40+
private string $orientation = self::OUTPUT_FORMAT_PORTRAIT;
41+
private string $paper = 'a4';
4142

42-
/** @var string */
43-
private $orientation = self::OUTPUT_FORMAT_PORTRAIT;
44-
45-
/** @var string */
46-
private $paper = 'a4';
47-
48-
public function __construct(\Vianetz\Pdf\Model\Config $config = null)
43+
public function __construct(?\Vianetz\Pdf\Model\Config $config = null)
4944
{
50-
if (empty($config)) {
51-
$config = new \Vianetz\Pdf\Model\Config();
52-
}
45+
$config ??= new \Vianetz\Pdf\Model\Config();
5346

5447
if ($config->getPdfOrientation() === Config::PAPER_ORIENTATION_PORTRAIT) {
5548
$this->orientation = self::OUTPUT_FORMAT_PORTRAIT;
@@ -70,63 +63,41 @@ public function __construct(\Vianetz\Pdf\Model\Config $config = null)
7063
/**
7164
* Import the specified page number from the given file into the current pdf model.
7265
*
73-
* @param string|StreamReader $pdfFile
74-
* @param int $pageNumber
66+
* @param string|resource|StreamReader $file
7567
*
76-
* @return void
68+
* @throws \setasign\Fpdi\PdfParser\PdfParserException
69+
* @throws \setasign\Fpdi\PdfReader\PdfReaderException
7770
*/
78-
public function importPageFromFile($pdfFile, $pageNumber)
71+
public function importPageFromFile($file, int $pageNumber): void
7972
{
80-
$this->fpdiModel->setSourceFile($pdfFile);
73+
$this->fpdiModel->setSourceFile($file);
8174
$pageId = $this->fpdiModel->importPage($pageNumber);
8275
$this->fpdiModel->useTemplate($pageId);
8376
}
8477

85-
/**
86-
* @param string $pdfString
87-
* @param int $pageNumber
88-
*
89-
* @return void
90-
*/
91-
public function importPageFromPdfString($pdfString, $pageNumber)
78+
public function importPageFromPdfString(string $pdfString, int $pageNumber): void
9279
{
9380
$this->importPageFromFile($this->createPdfStream($pdfString), $pageNumber);
9481
}
9582

96-
/**
97-
* @return string
98-
*/
99-
public function getPdfContents()
83+
public function getPdfContents(): string
10084
{
10185
return $this->fpdiModel->Output('', self::OUTPUT_MODE_STRING);
10286
}
10387

104-
/**
105-
* @param string $pdfString
106-
*
107-
* @return integer
108-
*/
109-
public function countPages($pdfString)
88+
public function countPages(string $pdfString): int
11089
{
11190
return $this->fpdiModel->setSourceFile($this->createPdfStream($pdfString));
11291
}
11392

114-
/**
115-
* @return \Vianetz\Pdf\Model\Merger\Fpdi
116-
*/
117-
public function addPage()
93+
public function addPage(): self
11894
{
11995
$this->fpdiModel->addPage($this->orientation, $this->paper);
12096

12197
return $this;
12298
}
12399

124-
/**
125-
* @param string $pdfString
126-
*
127-
* @return \setasign\Fpdi\PdfParser\StreamReader
128-
*/
129-
private function createPdfStream($pdfString)
100+
private function createPdfStream(string $pdfString): \setasign\Fpdi\PdfParser\StreamReader
130101
{
131102
return StreamReader::createByString($pdfString);
132103
}

src/Model/MergerInterface.php

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
35
* Pdf merger interface class
46
*
@@ -19,47 +21,20 @@
1921

2022
namespace Vianetz\Pdf\Model;
2123

24+
use setasign\Fpdi\PdfParser\StreamReader;
25+
2226
interface MergerInterface
2327
{
24-
/**
25-
* Return the merged PDF contents as string.
26-
*
27-
* @return string
28-
*/
29-
public function getPdfContents();
28+
public function getPdfContents(): string;
3029

31-
/**
32-
* @param string $fileName
33-
* @param integer $pageNumber
34-
*
35-
* @return void
36-
*/
37-
public function importPageFromFile($fileName, $pageNumber);
30+
/** @param string|resource|StreamReader $file */
31+
public function importPageFromFile($file, int $pageNumber): void;
3832

39-
/**
40-
* @param string $pdfString
41-
* @param integer $pageNumber
42-
*
43-
* @return void
44-
*/
45-
public function importPageFromPdfString($pdfString, $pageNumber);
33+
public function importPageFromPdfString(string $pdfString, int $pageNumber): void;
4634

47-
/**
48-
* @param string $fileName
49-
*
50-
* @return integer
51-
*/
52-
public function countPages($fileName);
35+
public function countPages(string $fileName): int;
5336

54-
/**
55-
* @return \Vianetz\Pdf\Model\MergerInterface
56-
*/
57-
public function addPage();
37+
public function addPage(): self;
5838

59-
/**
60-
* @param string $pdfBackgroundFile
61-
*
62-
* @return void
63-
*/
64-
public function importBackgroundTemplateFile($pdfBackgroundFile);
39+
public function importBackgroundTemplateFile(string $pdfBackgroundFile): void;
6540
}

0 commit comments

Comments
 (0)