Skip to content

Commit

Permalink
added back support of PHP 7.1.x and improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
petrparolek committed Jan 27, 2021
1 parent b848027 commit f19b1c6
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.2', '7.3', '7.4', '8.0']
php: ['7.1','7.2', '7.3', '7.4', '8.0']

fail-fast: false

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
}
],
"require": {
"php": ">=7.2",
"php": ">=7.1",
"mpdf/mpdf": "^8.0"
},
"require-dev": {
"latte/latte": "~2.7",
"nette/application": "~3.0.0",
"nette/di": "^3.0",
"ninjify/nunjuck": "^0.3.0",
"ninjify/qa": "^0.12.1",
"ninjify/qa": "^0.12",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-deprecation-rules": "^0.12",
"phpstan/phpstan-nette": "^0.12",
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ parameters:
ignoreErrors:
- '#Variable property access on Mpdf\\Mpdf#'
treatPhpDocTypesAsCertain: false
checkMissingIterableValueType: false
74 changes: 14 additions & 60 deletions src/PdfResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ class PdfResponse implements Nette\Application\IResponse

public const LAYOUT_DEFAULT = 'default'; // User’s default setting in Adobe Reader

/** @var array */
/** @var array<mixed> */
public $mpdfConfig = [];

/** @var array onBeforeComplete event */
/** @var array<mixed> onBeforeComplete event */
public $onBeforeComplete = [];

/** @var string Additional stylesheet as a html string */
Expand Down Expand Up @@ -128,7 +128,7 @@ class PdfResponse implements Nette\Application\IResponse
/** @var string ORIENTATION_PORTRAIT or ORIENTATION_LANDSCAPE */
private $pageOrientation = self::ORIENTATION_PORTRAIT;

/** @var string|array see second parameter ($format) at https://mpdf.github.io/reference/mpdf-functions/mpdf.html */
/** @var string|array<mixed> see second parameter ($format) at https://mpdf.github.io/reference/mpdf-functions/mpdf.html */
private $pageFormat = 'A4';

/** @var string margins: top, right, bottom, left, header, footer */
Expand Down Expand Up @@ -163,33 +163,21 @@ public function setTemplate($source): self
return $this;
}

/**
* @return string
*/
public function getDocumentAuthor(): string
{
return $this->documentAuthor;
}

/**
* @param string $documentAuthor
*/
public function setDocumentAuthor(string $documentAuthor): void
{
$this->documentAuthor = $documentAuthor;
}

/**
* @return string
*/
public function getDocumentTitle(): string
{
return $this->documentTitle;
}

/**
* @param string $documentTitle
*/
public function setDocumentTitle(string $documentTitle): void
{
$this->documentTitle = $documentTitle;
Expand Down Expand Up @@ -220,9 +208,6 @@ public function setDisplayZoom($displayZoom): void
$this->displayZoom = $displayZoom;
}

/**
* @return string
*/
public function getDisplayLayout(): string
{
return $this->displayLayout;
Expand Down Expand Up @@ -252,41 +237,26 @@ public function setDisplayLayout(string $displayLayout): void
$this->displayLayout = $displayLayout;
}

/**
* @return bool
*/
public function isMultiLanguage(): bool
{
return $this->multiLanguage;
}

/**
* @param bool $multiLanguage
*/
public function setMultiLanguage(bool $multiLanguage): void
{
$this->multiLanguage = $multiLanguage;
}

/**
* @return bool
*/
public function isIgnoreStylesInHTMLDocument(): bool
{
return $this->ignoreStylesInHTMLDocument;
}

/**
* @param bool $ignoreStylesInHTMLDocument
*/
public function setIgnoreStylesInHTMLDocument(bool $ignoreStylesInHTMLDocument): void
{
$this->ignoreStylesInHTMLDocument = $ignoreStylesInHTMLDocument;
}

/**
* @return string
*/
public function getSaveMode(): string
{
return $this->saveMode;
Expand All @@ -296,7 +266,6 @@ public function getSaveMode(): string
* To force download, use PdfResponse::DOWNLOAD
* To show pdf in browser, use PdfResponse::INLINE
*
* @param string $saveMode
* @throws InvalidArgumentException
*/
public function setSaveMode(string $saveMode): void
Expand All @@ -308,16 +277,12 @@ public function setSaveMode(string $saveMode): void
$this->saveMode = $saveMode;
}

/**
* @return string
*/
public function getPageOrientation(): string
{
return $this->pageOrientation;
}

/**
* @param string $pageOrientation
* @throws InvalidStateException
* @throws InvalidArgumentException
*/
Expand All @@ -335,15 +300,15 @@ public function setPageOrientation(string $pageOrientation): void
}

/**
* @return string|array
* @return string|array<mixed>
*/
public function getPageFormat()
{
return $this->pageFormat;
}

/**
* @param string|array $pageFormat
* @param string|array<mixed> $pageFormat
* @throws InvalidStateException
*/
public function setPageFormat($pageFormat): void
Expand All @@ -355,9 +320,6 @@ public function setPageFormat($pageFormat): void
$this->pageFormat = $pageFormat;
}

/**
* @return string
*/
public function getPageMargins(): string
{
return $this->pageMargins;
Expand All @@ -366,7 +328,7 @@ public function getPageMargins(): string
/**
* Gets margins as array
*
* @return array
* @return array<mixed>
*/
public function getMargins(): array
{
Expand All @@ -383,7 +345,6 @@ public function getMargins(): array
}

/**
* @param string $pageMargins
* @throws InvalidStateException
* @throws InvalidArgumentException
*/
Expand Down Expand Up @@ -412,7 +373,6 @@ public function setPageMargins(string $pageMargins): void
* WARNING: internally creates mPDF instance, setting some properties after calling this method
* may cause an Exception
*
* @param string $pathToBackgroundTemplate
* @throws FileNotFoundException
* @throws PdfParserExceptionAlias
*/
Expand All @@ -431,9 +391,11 @@ public function setBackgroundTemplate(string $pathToBackgroundTemplate): void
$tplId = $mpdf->importPage($i);
$mpdf->useTemplate($tplId);

if ($i < $pagecount) {
$mpdf->AddPage();
if ($i >= $pagecount) {
continue;
}

$mpdf->AddPage();
}

$mpdf->page = 1;
Expand Down Expand Up @@ -462,7 +424,6 @@ protected function getMPDFConfig(): array
}

/**
* @return Mpdf
* @throws InvalidStateException
*/
public function getMPDF(): Mpdf
Expand Down Expand Up @@ -494,9 +455,11 @@ public function getMPDF(): Mpdf
*/
public function __construct($source = null)
{
if ($source !== null) {
$this->setTemplate($source);
if ($source === null) {
return;
}

$this->setTemplate($source);
}


Expand All @@ -507,7 +470,6 @@ public function __construct($source = null)
/**
* Builds final pdf
*
* @return Mpdf
* @throws InvalidStateException
* @throws MissingServiceException
* @throws MpdfException
Expand Down Expand Up @@ -620,9 +582,6 @@ private function build(): Mpdf
/**
* Sends response to output
*
* @param IRequest $httpRequest
* @param IResponse $httpResponse
* @return void
* @throws MpdfException
*/
public function send(IRequest $httpRequest, IResponse $httpResponse): void
Expand All @@ -636,8 +595,6 @@ public function send(IRequest $httpRequest, IResponse $httpResponse): void
* Note: $name overrides property $documentTitle
*
* @param string $dir path to directory
* @param string|null $filename
* @return string
* @throws MpdfException
*/
public function save(string $dir, ?string $filename = null): string
Expand All @@ -658,7 +615,6 @@ public function save(string $dir, ?string $filename = null): string
}

/**
* @return string
* @throws MpdfException
*/
public function toString(): string
Expand All @@ -669,8 +625,6 @@ public function toString(): string

/**
* Return generated PDF as a string
*
* @return string
*/
public function __toString(): string
{
Expand Down
3 changes: 2 additions & 1 deletion src/PdfResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
class PdfResponseFactory
{

/** @var array */
/** @var array<mixed> */
public $mpdfConfig;

/** @param array<mixed> $mpdfConfig */
public function __construct(array $mpdfConfig)
{
$this->mpdfConfig = $mpdfConfig;
Expand Down
2 changes: 1 addition & 1 deletion tests/PdfResponse/PdfResponse.full.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use Tester\Assert;
require __DIR__ . '/../bootstrap.php';

test(
function () {
function (): void {
$origData = file_get_contents(__DIR__ . '/templates/example1.htm');
$fileResponse = new PdfResponse($origData);
$fileResponse->setSaveMode(PdfResponse::INLINE);
Expand Down
14 changes: 7 additions & 7 deletions tests/PdfResponse/PdfResponse.page.format.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require __DIR__ . '/../bootstrap.php';
$origData = file_get_contents(__DIR__ . '/templates/example1.htm');

test(
function () use ($origData) {
function () use ($origData): void {
$fileResponse = new PdfResponse($origData);
$fileResponse->setSaveMode(PdfResponse::INLINE);
$fileResponse->pageOrientation = PdfResponse::ORIENTATION_LANDSCAPE;
Expand All @@ -33,12 +33,12 @@ test(
);

test(
function () use ($origData) {
function () use ($origData): void {
$fileResponse = new PdfResponse($origData);
$fileResponse->getMPDF();

Assert::exception(
function () use ($fileResponse) {
function () use ($fileResponse): void {
$fileResponse->pageOrientation = PdfResponse::ORIENTATION_LANDSCAPE;
},
InvalidStateException::class,
Expand All @@ -48,12 +48,12 @@ test(
);

test(
function () use ($origData) {
function () use ($origData): void {
$fileResponse = new PdfResponse($origData);
$fileResponse->getMPDF();

Assert::exception(
function () use ($fileResponse) {
function () use ($fileResponse): void {
$fileResponse->pageFormat = 'A4-L';
},
InvalidStateException::class,
Expand All @@ -63,12 +63,12 @@ test(
);

test(
function () use ($origData) {
function () use ($origData): void {
$fileResponse = new PdfResponse($origData);
$fileResponse->getMPDF();

Assert::exception(
function () use ($fileResponse) {
function () use ($fileResponse): void {
$fileResponse->pageMargins = $fileResponse->getPageMargins();
},
InvalidStateException::class,
Expand Down
2 changes: 1 addition & 1 deletion tests/PdfResponse/PdfResponse.save.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use Tester\Assert;
require __DIR__ . '/../bootstrap.php';

test(
function () {
function (): void {
$origData = file_get_contents(__DIR__ . '/templates/example1.htm');
$fileResponse = new PdfResponse($origData);
$fileResponse->setSaveMode(PdfResponse::DOWNLOAD);
Expand Down
Loading

0 comments on commit f19b1c6

Please sign in to comment.