From 15a933103f6f848fe11eeab62a2109cd156ef8d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Wed, 2 Oct 2019 09:30:54 +0200 Subject: [PATCH] PdfResponse: Add strict type support. --- src/PdfResponse.php | 71 +++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/src/PdfResponse.php b/src/PdfResponse.php index 69160a6..bf59f9e 100644 --- a/src/PdfResponse.php +++ b/src/PdfResponse.php @@ -1,5 +1,7 @@ = 5.2) - const LAYOUT_TWORIGHT = "tworight"; // Display the pages in two columns, with the first page displayed on the right side (mPDF >= 5.2) - const LAYOUT_DEFAULT = "default"; // User’s default setting in Adobe Reader + public const LAYOUT_SINGLE = "single"; // Display one page at a time + public const LAYOUT_CONTINUOUS = "continuous"; // Display the pages in one column + public const LAYOUT_TWO = "two"; // Display the pages in two columns (first page determined by document direction (e.g. RTL)) + public const LAYOUT_TWOLEFT = "twoleft"; // Display the pages in two columns, with the first page displayed on the left side (mPDF >= 5.2) + public const LAYOUT_TWORIGHT = "tworight"; // Display the pages in two columns, with the first page displayed on the right side (mPDF >= 5.2) + public const LAYOUT_DEFAULT = "default"; // User’s default setting in Adobe Reader /** @var array onBeforeComplete event */ public $onBeforeComplete = array(); @@ -115,10 +117,10 @@ class PdfResponse implements Nette\Application\IResponse private $pageMargins = "16,15,16,15,9,9"; /** @var Mpdf|null */ - private $mPDF = null; + private $mPDF; /** @var Mpdf|null */ - private $generatedFile = null; + private $generatedFile; /************************************ properties **************************************/ @@ -136,7 +138,7 @@ public function getDocumentAuthor(): string */ public function setDocumentAuthor(string $documentAuthor): void { - $this->documentAuthor = (string)$documentAuthor; + $this->documentAuthor = $documentAuthor; } @@ -154,7 +156,7 @@ public function getDocumentTitle(): string */ public function setDocumentTitle(string $documentTitle): void { - $this->documentTitle = (string)$documentTitle; + $this->documentTitle = $documentTitle; } @@ -172,12 +174,12 @@ public function getDisplayZoom() */ public function setDisplayZoom($displayZoom): void { - if (!in_array($displayZoom, array( + if ($displayZoom <= 0 && !in_array($displayZoom, array( self::ZOOM_DEFAULT, self::ZOOM_FULLPAGE, self::ZOOM_FULLWIDTH, self::ZOOM_REAL - )) && $displayZoom <= 0) { + ), true)) { throw new InvalidArgumentException("Invalid zoom '$displayZoom', use PdfResponse::ZOOM_* constants or o positive integer."); } $this->displayZoom = $displayZoom; @@ -187,9 +189,9 @@ public function setDisplayZoom($displayZoom): void /** * @return string */ - public function getDisplayLayout() + public function getDisplayLayout(): string { - return $this->displayLayout; + return (string) $this->displayLayout; } @@ -199,7 +201,7 @@ public function getDisplayLayout() */ public function setDisplayLayout(string $displayLayout): void { - if (!in_array( + if ($displayLayout <= 0 && !in_array( $displayLayout, array( self::LAYOUT_DEFAULT, @@ -208,12 +210,13 @@ public function setDisplayLayout(string $displayLayout): void self::LAYOUT_TWO, self::LAYOUT_TWOLEFT, self::LAYOUT_TWORIGHT - ) - ) && $displayLayout <= 0 + ), + true + ) ) { throw new InvalidArgumentException("Invalid layout '$displayLayout', use PdfResponse::LAYOUT* constants."); } - $this->displayLayout = (string)$displayLayout; + $this->displayLayout = $displayLayout; } @@ -231,7 +234,7 @@ public function isMultiLanguage(): bool */ public function setMultiLanguage(bool $multiLanguage): void { - $this->multiLanguage = (bool)$multiLanguage; + $this->multiLanguage = $multiLanguage; } @@ -249,7 +252,7 @@ public function isIgnoreStylesInHTMLDocument(): bool */ public function setIgnoreStylesInHTMLDocument(bool $ignoreStylesInHTMLDocument): void { - $this->ignoreStylesInHTMLDocument = (bool)$ignoreStylesInHTMLDocument; + $this->ignoreStylesInHTMLDocument = $ignoreStylesInHTMLDocument; } @@ -271,7 +274,7 @@ public function getSaveMode(): string */ public function setSaveMode(string $saveMode): void { - if (!in_array($saveMode, array(self::DOWNLOAD, self::INLINE))) { + if (!in_array($saveMode, array(self::DOWNLOAD, self::INLINE), true)) { throw new InvalidArgumentException("Invalid mode '$saveMode', use PdfResponse::INLINE or PdfResponse::DOWNLOAD instead."); } $this->saveMode = $saveMode; @@ -297,7 +300,7 @@ public function setPageOrientation(string $pageOrientation): void if ($this->mPDF) { throw new InvalidStateException('mPDF instance already created. Set page orientation before calling getMPDF'); } - if (!in_array($pageOrientation, array(self::ORIENTATION_PORTRAIT, self::ORIENTATION_LANDSCAPE))) { + if (!in_array($pageOrientation, array(self::ORIENTATION_PORTRAIT, self::ORIENTATION_LANDSCAPE), true)) { throw new InvalidArgumentException('Unknown page orientation'); } $this->pageOrientation = $pageOrientation; @@ -412,7 +415,7 @@ public function setBackgroundTemplate(string $pathToBackgroundTemplate): void /** - * @return array + * @return mixed[] */ protected function getMPDFConfig(): array { @@ -614,10 +617,10 @@ public function save(string $dir, ?string $filename = null): string * @return string * @throws MpdfException */ - public function toString() + public function toString(): string { $pdf = $this->build(); - return $pdf->Output("", Destination::STRING_RETURN); + return (string) $pdf->Output("", Destination::STRING_RETURN); } @@ -626,7 +629,7 @@ public function toString() * * @return string */ - public function __toString() + public function __toString(): string { $string = ""; try {