From 8c951b79dcb166cc4681c610b15bdb35653ff506 Mon Sep 17 00:00:00 2001 From: Chukwuemeka Nwobodo Date: Tue, 3 Sep 2019 01:33:23 +0100 Subject: [PATCH] PDF-Unite Implementation --- src/PopplerPhp/Config.php | 4 +- src/PopplerPhp/Constants.php | 8 +- .../Exceptions/PopplerPhpException.php | 10 +- src/PopplerPhp/Helpers.php | 16 +++ src/PopplerPhp/PdfInfo.php | 132 ++++++++++++++--- src/PopplerPhp/PdfSeparate.php | 10 +- src/PopplerPhp/PdfToCairo.php | 9 +- src/PopplerPhp/PdfToHtml.php | 51 ++++++- src/PopplerPhp/PdfToText.php | 41 +++++- src/PopplerPhp/PdfUnite.php | 27 +++- .../PopplerOptions/CairoOptions.php | 105 +++++++++++++- .../PopplerOptions/ConsoleFlags.php | 12 +- .../PopplerOptions/CredentialOptions.php | 23 ++- src/PopplerPhp/PopplerOptions/DateFlags.php | 18 ++- .../PopplerOptions/EncodingOptions.php | 22 ++- src/PopplerPhp/PopplerOptions/HelpFlags.php | 12 +- src/PopplerPhp/PopplerOptions/HtmlOptions.php | 63 ++++++++- src/PopplerPhp/PopplerOptions/InfoFlags.php | 12 +- .../PopplerOptions/PageRangeOptions.php | 29 +++- src/PopplerPhp/PopplerOptions/TextFlags.php | 15 +- .../PopplerOptions/VersionFlags.php | 12 +- src/PopplerPhp/PopplerUtil.php | 133 ++++++++++++------ tests/PdfInfoTest.php | 16 ++- tests/PdfToCairoTest.php | 12 +- tests/PdfToHtmlTest.php | 12 +- tests/PdfUniteTest.php | 18 ++- tests/PopplerUtilTest.php | 74 ++++++---- 27 files changed, 757 insertions(+), 139 deletions(-) diff --git a/src/PopplerPhp/Config.php b/src/PopplerPhp/Config.php index a5f0b6a..1137fd3 100644 --- a/src/PopplerPhp/Config.php +++ b/src/PopplerPhp/Config.php @@ -78,7 +78,9 @@ public static function setBinDirectory($dir) */ public static function getBinDirectory() { - return self::get(C::BIN_DIR, H::parseDirName(realpath(__DIR__.'/../../vendor/bin/poppler'))); + $win = self::get(C::BIN_DIR, H::parseDirName(realpath(__DIR__.'/../../vendor/bin/poppler'))); + + return PHP_OS === 'WINNT' ? $win : ""; } /** diff --git a/src/PopplerPhp/Constants.php b/src/PopplerPhp/Constants.php index eb719c2..7168ccb 100644 --- a/src/PopplerPhp/Constants.php +++ b/src/PopplerPhp/Constants.php @@ -9,6 +9,10 @@ namespace NcJoes\PopplerPhp; +/** + * Class Constants + * @package NcJoes\PopplerPhp + */ abstract class Constants { //Poppler Utils Binaries @@ -98,8 +102,8 @@ abstract class Constants const BIN_DIR = 'ncjoes.poppler-php.bin_dir'; const OUTPUT_DIR = 'ncjoes.poppler-php.output_dir'; const OUTPUT_NAME = 'ncjoes.poppler-php.output_name'; - - const DEFAULT_OUTPUT_DIR = __DIR__.'/../../tests/results'; + + const DEFAULT_OUTPUT_DIR = 'tests/results'; //Config Helpers const DFT = '_d_'; diff --git a/src/PopplerPhp/Exceptions/PopplerPhpException.php b/src/PopplerPhp/Exceptions/PopplerPhpException.php index ac3682d..38e5ee6 100644 --- a/src/PopplerPhp/Exceptions/PopplerPhpException.php +++ b/src/PopplerPhp/Exceptions/PopplerPhpException.php @@ -9,7 +9,13 @@ namespace NcJoes\PopplerPhp\Exceptions; -class PopplerPhpException extends \Exception +use Exception; + +/** + * Class PopplerPhpException + * @package NcJoes\PopplerPhp\Exceptions + */ +class PopplerPhpException extends Exception { -} \ No newline at end of file +} diff --git a/src/PopplerPhp/Helpers.php b/src/PopplerPhp/Helpers.php index dfe40a7..1b2dd60 100644 --- a/src/PopplerPhp/Helpers.php +++ b/src/PopplerPhp/Helpers.php @@ -11,8 +11,16 @@ use NcJoes\PopplerPhp\Constants as C; +/** + * Class Helpers + * @package NcJoes\PopplerPhp + */ abstract class Helpers { + /** + * @param $dir + * @return mixed + */ public static function parseDirName($dir) { $dir = str_replace('/', C::DS, $dir); @@ -20,6 +28,10 @@ public static function parseDirName($dir) return $dir; } + /** + * @param $name + * @return string|string[]|null + */ public static function parseFileName($name) { $name = preg_replace("/[^A-Za-z0-9-_. ]/", '', $name); @@ -27,6 +39,10 @@ public static function parseFileName($name) return $name; } + /** + * @param $dir + * @return mixed + */ public static function parseFileRealPath($dir) { $dir = str_replace('\\', C::DS, $dir); diff --git a/src/PopplerPhp/PdfInfo.php b/src/PopplerPhp/PdfInfo.php index b0f0788..ea0216e 100644 --- a/src/PopplerPhp/PdfInfo.php +++ b/src/PopplerPhp/PdfInfo.php @@ -17,6 +17,10 @@ use NcJoes\PopplerPhp\PopplerOptions\InfoFlags; use NcJoes\PopplerPhp\PopplerOptions\PageRangeOptions; +/** + * Class PdfInfo + * @package NcJoes\PopplerPhp + */ class PdfInfo extends PopplerUtil { use CredentialOptions; @@ -26,26 +30,41 @@ class PdfInfo extends PopplerUtil use ConsoleFlags; use InfoFlags; - private $pdf_info; - + /** + * @var + */ + private $pdfInfo; + + /** + * PdfInfo constructor. + * @param string $pdfFile + * @param array $options + * @throws Exceptions\PopplerPhpException + */ public function __construct($pdfFile = '', array $options = []) { $this->setRequireOutputDir(false); - $this->bin_file = C::PDF_INFO; + $this->binFile = C::PDF_INFO; return parent::__construct($pdfFile, $options); } + /** + * @return mixed + */ public function getInfo() { $this->checkInfo(); - return $this->pdf_info; + return $this->pdfInfo; } + /** + * @return $this + */ protected function checkInfo() { - if (is_null($this->pdf_info)) { + if (is_null($this->pdfInfo)) { $content = $this->shellExec(); $lines = explode("\n", $content); $info = []; @@ -55,85 +74,118 @@ protected function checkInfo() $info[ str_replace([" "], ["_"], strtolower($key)) ] = trim(implode(':', (array)$value)); } } - $this->pdf_info = $info; + $this->pdfInfo = $info; } return $this; } + /** + * @return string|null + */ public function getTitle() { $this->checkInfo(); - return isset($this->pdf_info['title']) ? $this->pdf_info['title'] : null; + return isset($this->pdfInfo['title']) ? $this->pdfInfo['title'] : null; } + /** + * @return array + */ public function getAuthors() { $this->checkInfo(); - $authors = isset($this->pdf_info['author']) ? explode(',', $this->pdf_info['author']) : []; + $authors = isset($this->pdfInfo['author']) ? explode(',', $this->pdfInfo['author']) : []; return array_map(function ($item) { return ltrim(rtrim($item, ' '), ' '); }, $authors); } + /** + * @return string|null + */ public function getCreator() { $this->checkInfo(); - return isset($this->pdf_info['creator']) ? $this->pdf_info['creator'] : null; + return isset($this->pdfInfo['creator']) ? $this->pdfInfo['creator'] : null; } + /** + * @return string|null + */ public function getProducer() { $this->checkInfo(); - return isset($this->pdf_info['producer']) ? $this->pdf_info['producer'] : null; + return isset($this->pdfInfo['producer']) ? $this->pdfInfo['producer'] : null; } + /** + * @return string|null + */ public function getCreationDate() { $this->checkInfo(); - return isset($this->pdf_info['creationdate']) ? $this->pdf_info['creationdate'] : null; + return isset($this->pdfInfo['creationdate']) ? $this->pdfInfo['creationdate'] : null; } + /** + * @return string|null + */ public function getModificationDate() { $this->checkInfo(); - return isset($this->pdf_info['moddate']) ? $this->pdf_info['moddate'] : null; + return isset($this->pdfInfo['moddate']) ? $this->pdfInfo['moddate'] : null; } + /** + * @return bool + */ public function isTagged() { $this->checkInfo(); - return isset($this->pdf_info['tagged']) ? ($this->pdf_info['tagged'] != 'no') : false; + return isset($this->pdfInfo['tagged']) ? ($this->pdfInfo['tagged'] != 'no') : false; } + /** + * @return bool + */ public function hasJavaScript() { $this->checkInfo(); - return isset($this->pdf_info['javascript']) ? ($this->pdf_info['javascript'] != 'no') : false; + return isset($this->pdfInfo['javascript']) ? ($this->pdfInfo['javascript'] != 'no') : false; } + /** + * @return string|null + */ public function getNumOfPages() { $this->checkInfo(); - return isset($this->pdf_info['pages']) ? $this->pdf_info['pages'] : null; + return isset($this->pdfInfo['pages']) ? $this->pdfInfo['pages'] : null; } + /** + * @return bool + */ public function isEncrypted() { $this->checkInfo(); - return isset($this->pdf_info['encrypted']) ? ($this->pdf_info['encrypted'] != 'no') : false; + return isset($this->pdfInfo['encrypted']) ? ($this->pdfInfo['encrypted'] != 'no') : false; } + /** + * @return string|null + */ public function getSizeUnit() { $dimensions = explode(' ', $this->getPageSize()); @@ -141,13 +193,19 @@ public function getSizeUnit() return isset($dimensions[3]) ? $dimensions[3] : null; } + /** + * @return string|null + */ public function getPageSize() { $this->checkInfo(); - return isset($this->pdf_info['page_size']) ? $this->pdf_info['page_size'] : null; + return isset($this->pdfInfo['page_size']) ? $this->pdfInfo['page_size'] : null; } + /** + * @return float|null + */ public function getPageWidth() { $dimensions = explode('x', $this->getPageSize()); @@ -155,6 +213,9 @@ public function getPageWidth() return isset($dimensions[0]) ? doubleval($dimensions[0]) : null; } + /** + * @return float|null + */ public function getPageHeight() { $dimensions = explode('x', $this->getPageSize()); @@ -162,38 +223,53 @@ public function getPageHeight() return isset($dimensions[1]) ? doubleval($dimensions[1]) : null; } + /** + * @return string|null + */ public function getPageRot() { $this->checkInfo(); - return isset($this->pdf_info['page_rot']) ? $this->pdf_info['page_rot'] : null; + return isset($this->pdfInfo['page_rot']) ? $this->pdfInfo['page_rot'] : null; } + /** + * @return int|null + */ public function getFileSize() { $this->checkInfo(); - if (isset($this->pdf_info['file_size'])) { - return intval($this->pdf_info['file_size']); + if (isset($this->pdfInfo['file_size'])) { + return intval($this->pdfInfo['file_size']); } return null; } + /** + * @return bool + */ public function isOptimized() { $this->checkInfo(); - return isset($this->pdf_info['optimized']) ? ($this->pdf_info['optimized'] != 'no') : false; + return isset($this->pdfInfo['optimized']) ? ($this->pdfInfo['optimized'] != 'no') : false; } + /** + * @return string|null + */ public function getPdfVersion() { $this->checkInfo(); - return isset($this->pdf_info['pdf_version']) ? $this->pdf_info['pdf_version'] : null; + return isset($this->pdfInfo['pdf_version']) ? $this->pdfInfo['pdf_version'] : null; } + /** + * @return array|mixed + */ public function utilOptions() { return array_merge( @@ -203,6 +279,9 @@ public function utilOptions() ); } + /** + * @return array|mixed + */ public function utilOptionRules() { return [ @@ -210,6 +289,9 @@ public function utilOptionRules() ]; } + /** + * @return array|mixed + */ public function utilFlags() { return array_merge( @@ -220,6 +302,9 @@ public function utilFlags() ); } + /** + * @return array|mixed + */ public function utilFlagRules() { return [ @@ -227,6 +312,9 @@ public function utilFlagRules() ]; } + /** + * @return mixed|null + */ public function outputExtension() { return null; diff --git a/src/PopplerPhp/PdfSeparate.php b/src/PopplerPhp/PdfSeparate.php index a3ec922..1556c9f 100644 --- a/src/PopplerPhp/PdfSeparate.php +++ b/src/PopplerPhp/PdfSeparate.php @@ -10,10 +10,6 @@ namespace NcJoes\PopplerPhp; use NcJoes\PopplerPhp\Constants as C; -use NcJoes\PopplerPhp\PopplerOptions\CairoOptions; -use NcJoes\PopplerPhp\PopplerOptions\HelpFlags; -use NcJoes\PopplerPhp\PopplerOptions\PageRangeOptions; - class PdfSeparate extends PopplerUtil { @@ -25,7 +21,7 @@ class PdfSeparate extends PopplerUtil */ public function __construct($pdfFile = '', array $options = []) { - $this->bin_file = C::PDF_SEPARATE; + $this->binFile = C::PDF_SEPARATE; return parent::__construct($pdfFile, $options); } @@ -61,10 +57,10 @@ public function outputExtension() */ public function generate() { - $this->output_file_extension = $this->outputExtension(); + $this->outputFileExtension = $this->outputExtension(); return $this->shellExec(); } -} \ No newline at end of file +} diff --git a/src/PopplerPhp/PdfToCairo.php b/src/PopplerPhp/PdfToCairo.php index 001fcd0..602a3ba 100644 --- a/src/PopplerPhp/PdfToCairo.php +++ b/src/PopplerPhp/PdfToCairo.php @@ -35,10 +35,11 @@ class PdfToCairo extends PopplerUtil * * @param string $pdfFile * @param array $options + * @throws Exceptions\PopplerPhpException */ public function __construct($pdfFile = '', array $options = []) { - $this->bin_file = C::PDF_TO_CAIRO; + $this->binFile = C::PDF_TO_CAIRO; return parent::__construct($pdfFile, $options); } @@ -122,7 +123,7 @@ public function generateTIFF() public function generatePS() { $this->setOutputFormat(C::_PS); - $this->output_file_extension = $this->outputExtension(); + $this->outputFileExtension = $this->outputExtension(); return $this->generate(); } @@ -133,7 +134,7 @@ public function generatePS() public function generateEPS() { $this->setOutputFormat(C::_EPS); - $this->output_file_extension = $this->outputExtension(); + $this->outputFileExtension = $this->outputExtension(); return $this->generate(); } @@ -154,7 +155,7 @@ public function generatePDF() public function generateSVG() { $this->setOutputFormat(C::_SVG); - $this->output_file_extension = $this->outputExtension(); + $this->outputFileExtension = $this->outputExtension(); return $this->generate(); } diff --git a/src/PopplerPhp/PdfToHtml.php b/src/PopplerPhp/PdfToHtml.php index ea59c90..83baf21 100644 --- a/src/PopplerPhp/PdfToHtml.php +++ b/src/PopplerPhp/PdfToHtml.php @@ -16,6 +16,10 @@ use NcJoes\PopplerPhp\PopplerOptions\HtmlOptions; use NcJoes\PopplerPhp\PopplerOptions\PageRangeOptions; +/** + * Class PdfToHtml + * @package NcJoes\PopplerPhp + */ class PdfToHtml extends PopplerUtil { use PageRangeOptions; @@ -24,16 +28,28 @@ class PdfToHtml extends PopplerUtil use EncodingOptions; use CredentialOptions; + /** + * @var + */ private $products; + /** + * PdfToHtml constructor. + * @param string $pdfFile + * @param array $options + * @throws Exceptions\PopplerPhpException + */ public function __construct($pdfFile = '', array $options = []) { - $this->bin_file = C::PDF_TO_HTML; + $this->binFile = C::PDF_TO_HTML; $this->setFlag(C::_Q); return parent::__construct($pdfFile, $options); } + /** + * @return array|mixed + */ public function utilOptions() { return array_merge( @@ -44,6 +60,9 @@ public function utilOptions() ); } + /** + * @return array|mixed + */ public function utilFlags() { return array_merge( @@ -52,6 +71,9 @@ public function utilFlags() ); } + /** + * @return array|mixed + */ public function utilOptionRules() { return [ @@ -59,6 +81,9 @@ public function utilOptionRules() ]; } + /** + * @return array|mixed + */ public function utilFlagRules() { return [ @@ -66,31 +91,50 @@ public function utilFlagRules() ]; } + /** + * @return $this + */ public function defaultEncoding() { return $this; } + /** + * @return $this + */ public function listEncodings() { return $this; } + /** + * @return $this + */ public function oddPagesOnly() { return $this; } + /** + * @return $this + */ public function evenPagesOnly() { return $this; } + /** + * @return $this + */ public function firstPageOnly() { return $this; } + /** + * @param bool $regenerate + * @return string + */ public function generate($regenerate = false) { if (is_null($this->products) or $regenerate == true) { @@ -102,8 +146,11 @@ public function generate($regenerate = false) return $this->products; } + /** + * @return mixed|string + */ public function outputExtension() { return '.html'; } -} \ No newline at end of file +} diff --git a/src/PopplerPhp/PdfToText.php b/src/PopplerPhp/PdfToText.php index 7ed8a55..355452d 100644 --- a/src/PopplerPhp/PdfToText.php +++ b/src/PopplerPhp/PdfToText.php @@ -17,6 +17,10 @@ use NcJoes\PopplerPhp\PopplerOptions\PageRangeOptions; use NcJoes\PopplerPhp\PopplerOptions\TextFlags; +/** + * Class PdfToText + * @package NcJoes\PopplerPhp + */ class PdfToText extends PopplerUtil { use PageRangeOptions; @@ -27,19 +31,23 @@ class PdfToText extends PopplerUtil use TextFlags; - /** + /** * PdfToCairo constructor. * * @param string $pdfFile * @param array $options + * @throws Exceptions\PopplerPhpException */ public function __construct($pdfFile = '', array $options = []) { - $this->bin_file = C::PDF_TO_TEXT; + $this->binFile = C::PDF_TO_TEXT; return parent::__construct($pdfFile, $options); } + /** + * @return array|mixed + */ public function utilOptions() { return array_merge( @@ -50,6 +58,9 @@ public function utilOptions() ); } + /** + * @return array|mixed + */ public function utilOptionRules() { return [ @@ -57,11 +68,17 @@ public function utilOptionRules() ]; } + /** + * @return array|mixed + */ public function utilFlags() { return $this->textFlags(); } + /** + * @return array|mixed + */ public function utilFlagRules() { return [ @@ -69,6 +86,9 @@ public function utilFlagRules() ]; } + /** + * @return mixed|string + */ public function outputExtension() { return '.txt'; @@ -79,21 +99,34 @@ public function outputExtension() */ public function generate() { - $this->output_file_extension = $this->outputExtension(); + $this->outputFileExtension = $this->outputExtension(); return $this->shellExec(); } + /** + * @param $page + * @return PdfToText + * @throws Exceptions\PopplerPhpException + */ public function startFromPage($page) { return $this->setOption(C::_F, $page); } + /** + * @param $page + * @return PdfToText + * @throws Exceptions\PopplerPhpException + */ public function stopAtPage($page) { return $this->setOption(C::_L, $page); } + /** + * @return array + */ protected function pageRangeOptions() { return [ @@ -101,4 +134,4 @@ protected function pageRangeOptions() C::_L => C::T_INTEGER, ]; } -} \ No newline at end of file +} diff --git a/src/PopplerPhp/PdfUnite.php b/src/PopplerPhp/PdfUnite.php index 1f158c6..8788d0f 100644 --- a/src/PopplerPhp/PdfUnite.php +++ b/src/PopplerPhp/PdfUnite.php @@ -12,11 +12,16 @@ use NcJoes\PopplerPhp\Constants as C; use NcJoes\PopplerPhp\Exceptions\PopplerPhpException; +/** + * Class PdfUnite + * @package NcJoes\PopplerPhp + */ class PdfUnite extends PopplerUtil { /** * @param array $srcPdfFiles + * @throws PopplerPhpException */ public function __construct($srcPdfFiles) { @@ -26,36 +31,54 @@ public function __construct($srcPdfFiles) throw new PopplerPhpException("At least two pdf files are required"); } - $this->bin_file = C::PDF_UNITE; - $this->output_file_extension = $this->outputExtension(); + $this->binFile = C::PDF_UNITE; + $this->outputFileExtension = $this->outputExtension(); $this->setSourcePdfs($srcPdfFiles); } + /** + * @return mixed|void + */ public function utilOptions() { // TODO: Implement utilOptions() method. } + /** + * @return mixed|void + */ public function utilOptionRules() { // TODO: Implement utilOptionRules() method. } + /** + * @return mixed|void + */ public function utilFlags() { // TODO: Implement utilFlags() method. } + /** + * @return mixed|void + */ public function utilFlagRules() { // TODO: Implement utilFlagRules() method. } + /** + * @return mixed|string + */ public function outputExtension() { return '.pdf'; } + /** + * @return string + */ public function generate() { return $this->shellExec(); diff --git a/src/PopplerPhp/PopplerOptions/CairoOptions.php b/src/PopplerPhp/PopplerOptions/CairoOptions.php index 2aee1b0..79154b4 100644 --- a/src/PopplerPhp/PopplerOptions/CairoOptions.php +++ b/src/PopplerPhp/PopplerOptions/CairoOptions.php @@ -11,13 +11,24 @@ use NcJoes\PopplerPhp\Constants as C; +/** + * Trait CairoOptions + * @package NcJoes\PopplerPhp\PopplerOptions + */ trait CairoOptions { + /** + * @return mixed + */ public function getOutputFormat() { return $this->format; } + /** + * @param $format + * @return $this + */ public function setOutputFormat($format) { if (in_array($format, $this->cairoFormatFlags())) { @@ -29,11 +40,20 @@ public function setOutputFormat($format) return $this; } + /** + * @param $options + * @return mixed + */ public function setTiffCompression($options) { return $this->setOption(C::_TIFF_COMP, $options); } + /** + * @param $rx + * @param null $ry + * @return mixed + */ public function setResolution($rx, $ry = null) { if (is_null($ry)) { @@ -46,6 +66,11 @@ public function setResolution($rx, $ry = null) } } + /** + * @param $px + * @param null $py + * @return mixed + */ public function scalePagesTo($px, $py = null) { if (is_null($py)) { @@ -58,6 +83,11 @@ public function scalePagesTo($px, $py = null) } } + /** + * @param $x + * @param $y + * @return mixed + */ public function cropAreaOrigin($x, $y) { $this->setOption(C::_CROP_X, $x); @@ -65,6 +95,11 @@ public function cropAreaOrigin($x, $y) return $this->setOption(C::_CROP_Y, $y); } + /** + * @param $width + * @param null $height + * @return mixed + */ public function cropAreaSize($width, $height = null) { if (is_null($height)) { @@ -77,41 +112,68 @@ public function cropAreaSize($width, $height = null) } } + /** + * @return mixed + */ public function useCropBox() { return $this->setFlag(C::_CROP_BOX); } + /** + * @return mixed + */ public function noCrop() { return $this->setFlag(C::_NO_CROP); } + /** + * @return mixed + */ public function monochrome() { return $this->setFlag(C::_MONO); } + /** + * @return mixed + */ public function grayscale() { return $this->setFlag(C::_GRAY); } + /** + * @return mixed + */ public function transparentBg() { return $this->setFlag(C::_TRANSP); } + /** + * @param $options + * @return mixed + */ public function setAntiAlias($options) { return $this->setOption(C::_ANTI_ALIAS, $options); } + /** + * @param $profile + * @return mixed + */ public function setIccProfile($profile) { return $this->setOption(C::_ICC, $profile); } + /** + * @param $level + * @return $this + */ public function setPostScriptLevel($level) { if (in_array($level, $this->postScriptLevelFlags())) @@ -120,6 +182,9 @@ public function setPostScriptLevel($level) return $this; } + /** + * @return array + */ protected function cairoFlags() { return array_merge( @@ -131,31 +196,49 @@ protected function cairoFlags() ); } + /** + * @return array + */ protected function cairoFormatFlags() { return [C::_PNG, C::_JPEG, C::_TIFF, C::_PS, C::_EPS, C::_PDF, C::_SVG]; } + /** + * @return array + */ protected function cropFlags() { return [C::_CROP_BOX]; } + /** + * @return array + */ protected function colorFlags() { return [C::_MONO, C::_GRAY]; } + /** + * @return array + */ protected function bgFlags() { return [C::_TRANSP]; } + /** + * @return array + */ protected function postScriptLevelFlags() { return [C::_LEVEL2, C::_LEVEL3]; } + /** + * @return array + */ protected function cairoOptions() { return array_merge( @@ -168,6 +251,9 @@ protected function cairoOptions() ); } + /** + * @return array + */ protected function tiffOptions() { return [ @@ -175,15 +261,21 @@ protected function tiffOptions() ]; } + /** + * @return array + */ protected function resolutionOptions() { return [ C::_R => C::T_DOUBLE, C::_RX => C::T_DOUBLE, - C::_RX => C::T_DOUBLE, + C::_RY => C::T_DOUBLE, ]; } + /** + * @return array + */ protected function scaleOptions() { return [ @@ -193,6 +285,9 @@ protected function scaleOptions() ]; } + /** + * @return array + */ protected function cropOptions() { return [ @@ -204,6 +299,9 @@ protected function cropOptions() ]; } + /** + * @return array + */ protected function antiAliasOptions() { return [ @@ -211,10 +309,13 @@ protected function antiAliasOptions() ]; } + /** + * @return array + */ protected function iccProfileOptions() { return [ C::_ICC => C::T_STRING, ]; } -} \ No newline at end of file +} diff --git a/src/PopplerPhp/PopplerOptions/ConsoleFlags.php b/src/PopplerPhp/PopplerOptions/ConsoleFlags.php index a31cd50..70ccca7 100644 --- a/src/PopplerPhp/PopplerOptions/ConsoleFlags.php +++ b/src/PopplerPhp/PopplerOptions/ConsoleFlags.php @@ -11,11 +11,18 @@ use NcJoes\PopplerPhp\Constants as C; +/** + * Trait ConsoleFlags + * @package NcJoes\PopplerPhp\PopplerOptions + */ trait ConsoleFlags { use HelpFlags; use VersionFlags; + /** + * @return array + */ protected function allConsoleFlags() { return array_merge( @@ -25,8 +32,11 @@ protected function allConsoleFlags() ); } + /** + * @return array + */ protected function consoleFlags() { return [C::_Q, C::_STDOUT]; } -} \ No newline at end of file +} diff --git a/src/PopplerPhp/PopplerOptions/CredentialOptions.php b/src/PopplerPhp/PopplerOptions/CredentialOptions.php index 2d77fc2..7b38851 100644 --- a/src/PopplerPhp/PopplerOptions/CredentialOptions.php +++ b/src/PopplerPhp/PopplerOptions/CredentialOptions.php @@ -11,28 +11,49 @@ use NcJoes\PopplerPhp\Constants as C; +/** + * Trait CredentialOptions + * @package NcJoes\PopplerPhp\PopplerOptions + */ trait CredentialOptions { + /** + * @param $pswd + * @return mixed + */ public function setOwnerPassword($pswd) { return $this->setOption(C::_OPW, $pswd); } + /** + * @return mixed + */ public function unsetOwnerPassword() { return $this->unsetOption(C::_OPW); } + /** + * @param $pswd + * @return mixed + */ public function setUserPassword($pswd) { return $this->setOption(C::_UPW, $pswd); } + /** + * @return mixed + */ public function unsetUserPassword() { return $this->unsetOption(C::_UPW); } + /** + * @return array + */ protected function credentialOptions() { return [ @@ -41,4 +62,4 @@ protected function credentialOptions() ]; } -} \ No newline at end of file +} diff --git a/src/PopplerPhp/PopplerOptions/DateFlags.php b/src/PopplerPhp/PopplerOptions/DateFlags.php index a72c196..63e34df 100644 --- a/src/PopplerPhp/PopplerOptions/DateFlags.php +++ b/src/PopplerPhp/PopplerOptions/DateFlags.php @@ -11,18 +11,31 @@ use NcJoes\PopplerPhp\Constants as C; +/** + * Trait DateFlags + * @package NcJoes\PopplerPhp\PopplerOptions + */ trait DateFlags { + /** + * @return mixed + */ public function isoDates() { return $this->setFlag(C::_ISODATES); } + /** + * @return mixed + */ public function rawDates() { return $this->setFlag(C::_RAWDATE); } + /** + * @return mixed + */ public function defaultDates() { $this->unsetFlag(C::_RAWDATE); @@ -30,8 +43,11 @@ public function defaultDates() return $this->unsetFlag(C::_ISODATES); } + /** + * @return array + */ protected function dateFlags() { return [C::_ISODATES, C::_RAWDATE]; } -} \ No newline at end of file +} diff --git a/src/PopplerPhp/PopplerOptions/EncodingOptions.php b/src/PopplerPhp/PopplerOptions/EncodingOptions.php index 28cc3ce..56be86a 100644 --- a/src/PopplerPhp/PopplerOptions/EncodingOptions.php +++ b/src/PopplerPhp/PopplerOptions/EncodingOptions.php @@ -11,23 +11,40 @@ use NcJoes\PopplerPhp\Constants as C; +/** + * Trait EncodingOptions + * @package NcJoes\PopplerPhp\PopplerOptions + */ trait EncodingOptions { + /** + * @param $string + * @return mixed + */ public function setEncoding($string) { return $this->setOption(C::_ENC, $string); } + /** + * @return mixed + */ public function defaultEncoding() { return $this->unsetOption(C::_ENC); } + /** + * @return mixed + */ public function listEncodings() { return $this->setFlag(C::_LISTENC); } + /** + * @return array + */ protected function encodingOptions() { return [ @@ -35,8 +52,11 @@ protected function encodingOptions() ]; } + /** + * @return array + */ protected function encodingFlags() { return [C::_LISTENC]; } -} \ No newline at end of file +} diff --git a/src/PopplerPhp/PopplerOptions/HelpFlags.php b/src/PopplerPhp/PopplerOptions/HelpFlags.php index bd0ccd7..a6f1cae 100644 --- a/src/PopplerPhp/PopplerOptions/HelpFlags.php +++ b/src/PopplerPhp/PopplerOptions/HelpFlags.php @@ -11,15 +11,25 @@ use NcJoes\PopplerPhp\Constants as C; +/** + * Trait HelpFlags + * @package NcJoes\PopplerPhp\PopplerOptions + */ trait HelpFlags { + /** + * @return mixed + */ public function printHelpInfo() { return $this->setFlag(C::_H); } + /** + * @return array + */ protected function helpFlags() { return [C::_H, C::_HELP, C::_HELP_, C::_HELP_Q]; } -} \ No newline at end of file +} diff --git a/src/PopplerPhp/PopplerOptions/HtmlOptions.php b/src/PopplerPhp/PopplerOptions/HtmlOptions.php index ff13351..532e61a 100644 --- a/src/PopplerPhp/PopplerOptions/HtmlOptions.php +++ b/src/PopplerPhp/PopplerOptions/HtmlOptions.php @@ -11,28 +11,49 @@ use NcJoes\PopplerPhp\Constants as C; +/** + * Trait HtmlOptions + * @package NcJoes\PopplerPhp\PopplerOptions + */ trait HtmlOptions { + /** + * @return mixed + */ public function suppressConsoleOutput() { return $this->setFlag(C::_Q); } + /** + * @return mixed + */ public function outputToConsole() { return $this->setFlag(C::_STDOUT); } + /** + * @param $ratio + * @return mixed + */ public function setZoomRatio($ratio) { return $this->setOption(C::_ZOOM, $ratio); } + /** + * @return mixed + */ public function defaultZoom() { return $this->unsetOption(C::_ZOOM); } + /** + * @param $string + * @return $this + */ public function splashImageFormat($string) { if (in_array($string, ['png', 'jpg'])) @@ -41,61 +62,98 @@ public function splashImageFormat($string) return $this; } + /** + * @param $float + * @return mixed + */ public function setWordBreakThreshold($float) { return $this->setOption(C::_WBT, $float); } + /** + * @return mixed + */ public function exchangePdfLinks() { return $this->setFlag(C::_P); } + /** + * @return mixed + */ public function generateComplexDocument() { return $this->setFlag(C::_C); } + /** + * @return mixed + */ public function generateSingleDocument() { return $this->setFlag(C::_S); } + /** + * @return mixed + */ public function ignoreImages() { return $this->setFlag(C::_I); } + /** + * @return mixed + */ public function noFrames() { return $this->setFlag(C::_NOFRAMES); } + /** + * @return mixed + */ public function xmlOutput() { return $this->setFlag(C::_XML); } + /** + * @return mixed + */ public function revealHiddenText() { return $this->setFlag(C::_HIDDEN); } + /** + * @return mixed + */ public function unmergedParagraphs() { return $this->setFlag(C::_NOMERGE); } + /** + * @return mixed + */ public function noDrm() { return $this->setFlag(C::_NODRM); } + /** + * @return mixed + */ public function fullFontNames() { return $this->setFlag(C::_FONT_FN); } + /** + * @return array + */ protected function htmlFlags() { return [ @@ -112,6 +170,9 @@ protected function htmlFlags() ]; } + /** + * @return array + */ protected function htmlOptions() { return [ @@ -120,4 +181,4 @@ protected function htmlOptions() C::_WBT => C::T_DOUBLE, ]; } -} \ No newline at end of file +} diff --git a/src/PopplerPhp/PopplerOptions/InfoFlags.php b/src/PopplerPhp/PopplerOptions/InfoFlags.php index 14879c1..52450e1 100644 --- a/src/PopplerPhp/PopplerOptions/InfoFlags.php +++ b/src/PopplerPhp/PopplerOptions/InfoFlags.php @@ -4,15 +4,25 @@ use NcJoes\PopplerPhp\Constants as C; +/** + * Trait InfoFlags + * @package NcJoes\PopplerPhp\PopplerOptions + */ trait InfoFlags { + /** + * @return mixed + */ public function setBox() { return $this->setFlag(C::_BOX); } + /** + * @return array + */ protected function infoFlags() { return [C::_BOX]; } -} \ No newline at end of file +} diff --git a/src/PopplerPhp/PopplerOptions/PageRangeOptions.php b/src/PopplerPhp/PopplerOptions/PageRangeOptions.php index c957ed0..ac222e3 100644 --- a/src/PopplerPhp/PopplerOptions/PageRangeOptions.php +++ b/src/PopplerPhp/PopplerOptions/PageRangeOptions.php @@ -11,33 +11,57 @@ use NcJoes\PopplerPhp\Constants as C; +/** + * Trait PageRangeOptions + * @package NcJoes\PopplerPhp\PopplerOptions + */ trait PageRangeOptions { + /** + * @param $page + * @return mixed + */ public function startFromPage($page) { return $this->setOption(C::_F, $page); } + /** + * @param $page + * @return mixed + */ public function stopAtPage($page) { return $this->setOption(C::_L, $page); } + /** + * @return mixed + */ public function oddPagesOnly() { return $this->setFlag(C::_ODD_ONLY); } + /** + * @return mixed + */ public function evenPagesOnly() { return $this->setFlag(C::_EVEN_ONLY); } + /** + * @return mixed + */ public function firstPageOnly() { return $this->setFlag(C::_SINGLE_FILE); } + /** + * @return array + */ protected function pageRangeOptions() { return [ @@ -46,8 +70,11 @@ protected function pageRangeOptions() ]; } + /** + * @return array + */ protected function pageRangeFlags() { return [C::_ODD_ONLY, C::_EVEN_ONLY, C::_SINGLE_FILE]; } -} \ No newline at end of file +} diff --git a/src/PopplerPhp/PopplerOptions/TextFlags.php b/src/PopplerPhp/PopplerOptions/TextFlags.php index c95b2be..0de4716 100644 --- a/src/PopplerPhp/PopplerOptions/TextFlags.php +++ b/src/PopplerPhp/PopplerOptions/TextFlags.php @@ -4,20 +4,33 @@ use NcJoes\PopplerPhp\Constants as C; +/** + * Trait TextFlags + * @package NcJoes\PopplerPhp\PopplerOptions + */ trait TextFlags { + /** + * @return mixed + */ public function setBboxLayout() { return $this->setFlag(C::_BBOX_LAYOUT); } + /** + * @return mixed + */ public function setLayout() { return $this->setFlag(C::_LAYOUT); } + /** + * @return array + */ protected function textFlags() { return [C::_BBOX_LAYOUT, C::_LAYOUT]; } -} \ No newline at end of file +} diff --git a/src/PopplerPhp/PopplerOptions/VersionFlags.php b/src/PopplerPhp/PopplerOptions/VersionFlags.php index 334515e..de97b61 100644 --- a/src/PopplerPhp/PopplerOptions/VersionFlags.php +++ b/src/PopplerPhp/PopplerOptions/VersionFlags.php @@ -11,15 +11,25 @@ use NcJoes\PopplerPhp\Constants as C; +/** + * Trait VersionFlags + * @package NcJoes\PopplerPhp\PopplerOptions + */ trait VersionFlags { + /** + * @return mixed + */ public function printVersionInfo() { return $this->setFlag(C::_V); } + /** + * @return array + */ protected function versionFlags() { return [C::_V]; } -} \ No newline at end of file +} diff --git a/src/PopplerPhp/PopplerUtil.php b/src/PopplerPhp/PopplerUtil.php index eb3653a..f5e96ff 100644 --- a/src/PopplerPhp/PopplerUtil.php +++ b/src/PopplerPhp/PopplerUtil.php @@ -9,9 +9,14 @@ namespace NcJoes\PopplerPhp; +use FilesystemIterator; use NcJoes\PopplerPhp\Constants as C; use NcJoes\PopplerPhp\Exceptions\PopplerPhpException; use NcJoes\PopplerPhp\Helpers as H; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; +use function is_array; +use function is_string; /** * Class PopplerUtil @@ -20,17 +25,50 @@ */ abstract class PopplerUtil { - protected $bin_file; - protected $output_file_extension; - protected $require_output_dir = true; - protected $require_sub_dir = false; - protected $output_file_suffix = ''; - protected $source_pdfs = []; - private $binary_dir; - private $flags = []; - private $options = []; - private $output_sub_dir; - private $output_file_name_prefix; + /** + * @var string + */ + protected $binFile; + /** + * @var + */ + protected $outputFileExtension; + /** + * @var bool + */ + protected $requireOutputDir = true; + /** + * @var bool + */ + protected $requireSubDir = false; + /** + * @var string + */ + protected $outputFileSuffix = ''; + /** + * @var array + */ + protected $sourcePdfs = []; + /** + * @var string + */ + private $binaryDir; + /** + * @var array + */ + private $flags = []; + /** + * @var array + */ + private $options = []; + /** + * @var string + */ + private $outputSubDir; + /** + * @var string + */ + private $outputFileNamePrefix; /** @@ -88,7 +126,7 @@ public function setOutputSubDir($dir_name) $dir_name = H::parseDirName($dir_name); if (!empty($dir_name)) { - $this->output_sub_dir = $dir_name; + $this->outputSubDir = $dir_name; return $this; } @@ -100,11 +138,11 @@ public function setOutputSubDir($dir_name) */ public function getOutputSubDir() { - if ($this->isSubDirRequired() && empty($this->output_sub_dir)) { - $this->output_sub_dir = uniqid('test-'.date('m-d-Y_H-i')); + if ($this->isSubDirRequired() && empty($this->outputSubDir)) { + $this->outputSubDir = uniqid('test-'.date('m-d-Y_H-i')); } - return $this->output_sub_dir; + return $this->outputSubDir; } /** @@ -132,8 +170,8 @@ public function setOption($key, $value) { $util_options = $this->utilOptions(); - if (array_key_exists($key, $util_options) and $util_options[ $key ] == gettype($value)) { - $this->options[ $key ] = $value; + if (array_key_exists($key, $util_options) and $util_options[$key] == gettype($value)) { + $this->options[$key] = $value; return $this; } @@ -174,7 +212,7 @@ public function setFlag($key) $util_flags = $this->utilFlags(); if (in_array($key, $util_flags)) { - $this->flags[ $key ] = $key; + $this->flags[$key] = $key; return $this; } @@ -211,7 +249,7 @@ public function hasFlag($key) */ public function getOption($key) { - return $this->hasOption($key) ? $this->options[ $key ] : null; + return $this->hasOption($key) ? $this->options[$key] : null; } /** @@ -221,7 +259,7 @@ public function getOption($key) */ public function getFlag($key) { - return $this->hasFlag($key) ? $this->flags[ $key ] : null; + return $this->hasFlag($key) ? $this->flags[$key] : null; } /** @@ -242,6 +280,7 @@ public function getFlags() /** * @return string + * @throws PopplerPhpException */ public function previewShellCommand() { @@ -263,10 +302,10 @@ public function clearUtilOutputs() { $directory = $this->getOutputPath(); - $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS)); + $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS)); foreach ($files as $file) { - $path = (string)$file; + $path = (string) $file; $basename = basename($path); if ($basename != '..' && $basename != ".gitignore") { if (is_file($path) && file_exists($path)) @@ -298,18 +337,23 @@ protected function shellExec() return shell_exec($command); } - protected function getShellQuoteStr() { + /** + * @return string + */ + protected function getShellQuoteStr() + { return PHP_OS === 'WINNT' ? "\"" : "'"; } /** * @return string + * @throws PopplerPhpException */ private function makeShellCommand() { $q = $this->getShellQuoteStr(); - $bin = H::parseDirName($q.$this->binDir().C::DS.$this->bin_file.$q); + $bin = PHP_OS === 'WINNT' ? H::parseDirName($q.$this->binDir().C::DS.$this->binFile.$q) : $q.$this->binFile.$q; $opts_and_args = array_merge( $this->makeShellOptions(), @@ -323,15 +367,16 @@ private function makeShellCommand() * @param string $dir * * @return $this + * @throws PopplerPhpException */ public function binDir($dir = '') { if (!empty($dir)) { - $this->binary_dir = Config::setBinDirectory($dir); + $this->binaryDir = Config::setBinDirectory($dir); return $this; } elseif ($dir == C::DFT) { - $this->binary_dir = Config::setBinDirectory(Config::getBinDirectory()); + $this->binaryDir = Config::setBinDirectory(Config::getBinDirectory()); } return Config::getBinDirectory(); @@ -374,7 +419,8 @@ protected function makeShellArgs() protected function makeShellArgSrc() { $q = $this->getShellQuoteStr(); - return array_map(function($src_pdf) use($q) { + + return array_map(function ($src_pdf) use ($q) { return "{$q}{$src_pdf}{$q}"; }, $this->getSourcePdfs()); } @@ -391,7 +437,7 @@ protected function makeShellArgDest() if ($this->isOutputDirRequired()) { $directory = $this->getOutputPath(); $output_path = H::parseDirName($directory.C::DS.$this->getOutputFilenamePrefix()); - $generated[] = $q.$output_path.$this->output_file_suffix.$this->output_file_extension.$q; + $generated[] = $q.$output_path.$this->outputFileSuffix.$this->outputFileExtension.$q; } return $generated; @@ -402,7 +448,7 @@ protected function makeShellArgDest() */ public function getSourcePdfs() { - return $this->source_pdfs; + return $this->sourcePdfs; } /** @@ -411,10 +457,10 @@ public function getSourcePdfs() */ public function setSourcePdfs($src) { - if (\is_string($src)) { - $this->source_pdfs = [$src]; - } elseif (\is_array($src)) { - $this->source_pdfs = $src; + if (is_string($src)) { + $this->sourcePdfs = [$src]; + } elseif (is_array($src)) { + $this->sourcePdfs = $src; } else { throw new PopplerPhpException("src must be string or array"); } @@ -432,7 +478,7 @@ public function setOutputFilenamePrefix($name) { $name = H::parseFileName($name); if (!empty($name)) { - $this->output_file_name_prefix = $name; + $this->outputFileNamePrefix = $name; return $this; } @@ -447,7 +493,8 @@ public function setOutputFilenamePrefix($name) */ public function setOutputFilenameSuffix($suffix = '') { - $this->output_file_suffix = $suffix; + $this->outputFileSuffix = $suffix; + return $this; } @@ -456,13 +503,13 @@ public function setOutputFilenameSuffix($suffix = '') */ public function getOutputFilenamePrefix() { - if (!empty($this->output_file_name_prefix)) { - return $this->output_file_name_prefix; + if (!empty($this->outputFileNamePrefix)) { + return $this->outputFileNamePrefix; } $base = basename($this->getSourcePdfs()[0]); $default_name = str_replace('.pdf', '', $base); - $this->output_file_name_prefix = $default_name; + $this->outputFileNamePrefix = $default_name; return $default_name; } @@ -472,7 +519,7 @@ public function getOutputFilenamePrefix() */ public function getOutputFilenameSuffix() { - return $this->output_file_suffix; + return $this->outputFileSuffix; } /** @@ -481,7 +528,7 @@ public function getOutputFilenameSuffix() */ public function setRequireOutputDir($bool) { - $this->require_output_dir = $bool; + $this->requireOutputDir = $bool; return $this; } @@ -491,7 +538,7 @@ public function setRequireOutputDir($bool) */ public function isOutputDirRequired() { - return $this->require_output_dir; + return $this->requireOutputDir; } /** @@ -499,7 +546,7 @@ public function isOutputDirRequired() */ public function isSubDirRequired() { - return $this->require_sub_dir; + return $this->requireSubDir; } /** @@ -508,7 +555,7 @@ public function isSubDirRequired() */ public function setSubDirRequired($bool) { - $this->require_sub_dir = $bool; + $this->requireSubDir = $bool; return $this; } diff --git a/tests/PdfInfoTest.php b/tests/PdfInfoTest.php index dd1e949..e1ea210 100644 --- a/tests/PdfInfoTest.php +++ b/tests/PdfInfoTest.php @@ -9,27 +9,39 @@ use NcJoes\PopplerPhp\Config; use NcJoes\PopplerPhp\Constants as C; +use NcJoes\PopplerPhp\Exceptions\PopplerPhpException; use NcJoes\PopplerPhp\PdfInfo; +/** + * Class PdfInfoTest + */ class PdfInfoTest extends PHPUnit_Framework_TestCase { + /** + * @throws PopplerPhpException + */ public function setUp() { parent::setUp(); Config::setOutputDirectory(C::DFT); } + /** + * @throws PopplerPhpException + */ public function testGetInfo() { $DS = DIRECTORY_SEPARATOR; $file = __DIR__.$DS."sources{$DS}test1.pdf"; $pdf_info = new PdfInfo($file); - print_r($pdf_info->getInfo()); - $this->assertArrayHasKey('pages', $pdf_info->getInfo()); + static::assertArrayHasKey('pages', $pdf_info->getInfo()); $this->addToAssertionCount(sizeof($pdf_info->getInfo())); } + /** + * @throws PopplerPhpException + */ public function testGetters() { $DS = DIRECTORY_SEPARATOR; diff --git a/tests/PdfToCairoTest.php b/tests/PdfToCairoTest.php index 13be4c0..820e721 100644 --- a/tests/PdfToCairoTest.php +++ b/tests/PdfToCairoTest.php @@ -9,15 +9,25 @@ use NcJoes\PopplerPhp\Config; use NcJoes\PopplerPhp\Constants as C; +use NcJoes\PopplerPhp\Exceptions\PopplerPhpException; use NcJoes\PopplerPhp\PdfToCairo; +/** + * Class PdfToCairoTest + */ class PdfToCairoTest extends PHPUnit_Framework_TestCase { + /** + * + */ public function setUp() { parent::setUp(); } + /** + * @throws PopplerPhpException + */ public function testGeneratorMethods() { Config::setOutputDirectory(Config::getOutputDirectory()); @@ -49,4 +59,4 @@ public function testGeneratorMethods() $cairo6->generateSVG(); } -} \ No newline at end of file +} diff --git a/tests/PdfToHtmlTest.php b/tests/PdfToHtmlTest.php index 63ac397..5845797 100644 --- a/tests/PdfToHtmlTest.php +++ b/tests/PdfToHtmlTest.php @@ -8,15 +8,25 @@ **/ use NcJoes\PopplerPhp\Config; +use NcJoes\PopplerPhp\Exceptions\PopplerPhpException; use NcJoes\PopplerPhp\PdfToHtml; +/** + * Class PdfToHtmlTest + */ class PdfToHtmlTest extends PHPUnit_Framework_TestCase { + /** + * + */ public function setUp() { parent::setUp(); } + /** + * @throws PopplerPhpException + */ public function testGenerateMethod() { Config::setOutputDirectory(Config::getOutputDirectory(true), true); @@ -37,4 +47,4 @@ public function testGenerateMethod() print_r($pdfToHtml->generate()); } -} \ No newline at end of file +} diff --git a/tests/PdfUniteTest.php b/tests/PdfUniteTest.php index 8bef50b..47b36b6 100644 --- a/tests/PdfUniteTest.php +++ b/tests/PdfUniteTest.php @@ -1,14 +1,19 @@ outputExtension(); $expected_output_file = "{$output_dir}{$DS}{$output_file_prefix}{$output_file_ext}"; - $this->assertFileExists($expected_output_file); + static::assertFileExists($expected_output_file); $pdf_info = new PdfInfo($expected_output_file); - $this->assertEquals(16 * 3, intval($pdf_info->getNumOfPages())); + static::assertEquals(16 * 3, intval($pdf_info->getNumOfPages())); } } diff --git a/tests/PopplerUtilTest.php b/tests/PopplerUtilTest.php index 14348df..f84d108 100644 --- a/tests/PopplerUtilTest.php +++ b/tests/PopplerUtilTest.php @@ -7,20 +7,30 @@ * Date: 10/13/2016 * Time: 2:31 PM **/ + use NcJoes\PopplerPhp\Config; use NcJoes\PopplerPhp\Constants as C; +use NcJoes\PopplerPhp\Exceptions\PopplerPhpException; use NcJoes\PopplerPhp\PdfInfo; use NcJoes\PopplerPhp\PdfToCairo; use NcJoes\PopplerPhp\PdfUnite; +/** + * Class PopplerUtilTest + */ class PopplerUtilTest extends PHPUnit_Framework_TestCase { - + /** + * + */ public function setUp() { parent::setUp(); } + /** + * @throws PopplerPhpException + */ public function testSetOutputFileName() { $DS = DIRECTORY_SEPARATOR; @@ -28,9 +38,12 @@ public function testSetOutputFileName() $pdf = new PdfToCairo($file); $pdf->setOutputFilenamePrefix('different-name'); - $this->assertEquals('different-name', $pdf->getOutputFilenamePrefix()); + static::assertEquals('different-name', $pdf->getOutputFilenamePrefix()); } + /** + * @throws PopplerPhpException + */ public function testMakeOptionsMethod() { $DS = DIRECTORY_SEPARATOR; @@ -38,32 +51,38 @@ public function testMakeOptionsMethod() $pdf = new PdfInfo($file); $pdf->startFromPage(10); - $this->assertArrayHasKey('-f', $pdf->getOptions()); + static::assertArrayHasKey('-f', $pdf->getOptions()); $pdf->stopAtPage(20); - $this->assertArrayHasKey('-l', $pdf->getOptions()); + static::assertArrayHasKey('-l', $pdf->getOptions()); $pdf->listEncodings(); - $this->assertArrayHasKey('-listenc', $pdf->getFlags()); + static::assertArrayHasKey('-listenc', $pdf->getFlags()); $pdf->isoDates(); - $this->assertArrayHasKey('-isodates', $pdf->getFlags()); + static::assertArrayHasKey('-isodates', $pdf->getFlags()); - $this->assertContains('-f', $pdf->previewShellOptions()); + static::assertContains('-f', $pdf->previewShellOptions()); } + /** + * @throws PopplerPhpException + */ public function testOutputDirMethodSetterAndGetter() { Config::setOutputDirectory(Config::getOutputDirectory(C::DFT)); - $this->assertTrue(Config::isKeySet(C::OUTPUT_DIR)); + static::assertTrue(Config::isKeySet(C::OUTPUT_DIR)); $DS = DIRECTORY_SEPARATOR; $file = __DIR__.$DS."sources{$DS}test1.pdf"; $cairo = new PdfToCairo($file); - $this->assertTrue($cairo->getOutputPath() != dirname($file)); + static::assertTrue($cairo->getOutputPath() != dirname($file)); } + /** + * @throws PopplerPhpException + */ public function testMakeCommandMethod() { $q = PHP_OS === 'WINNT' ? "\"" : "'"; @@ -78,15 +97,15 @@ public function testMakeCommandMethod() $bin_file = C::PDF_TO_CAIRO; $output_dir = Config::getOutputDirectory(); - $expected_bin = "{$q}{$bin_dir}{$DS}{$bin_file}{$q}"; + $expected_bin = PHP_OS === 'WINNT' ? "{$q}".implode($DS, [$bin_dir, $bin_file])."{$q}" : $q.$bin_file.$q; $expected_src = "{$q}{$file}{$q}"; $expected_dest = "{$q}{$output_dir}{$DS}{$output_file_prefix}{$q}"; - $this->assertRegExp( - "%^{$expected_bin} {$expected_src} {$expected_dest}$%", - $pdf->previewShellCommand() - ); + static::assertRegExp("%^{$expected_bin} {$expected_src} {$expected_dest}$%", $pdf->previewShellCommand()); } + /** + * @throws PopplerPhpException + */ public function testMakeCommandMethodWithSubDirEnabled() { $q = PHP_OS === 'WINNT' ? "\"" : "'"; @@ -103,15 +122,15 @@ public function testMakeCommandMethodWithSubDirEnabled() $output_dir = Config::getOutputDirectory(); $output_sub_dir = $pdf->getOutputSubDir(); - $expected_bin = "{$q}{$bin_dir}{$DS}{$bin_file}{$q}"; + $expected_bin = PHP_OS === 'WINNT' ? "{$q}".implode($DS, [$bin_dir, $bin_file])."{$q}" : $q.$bin_file.$q; $expected_src = "{$q}{$file}{$q}"; $expected_dest = "{$q}{$output_dir}{$DS}{$output_sub_dir}{$DS}{$output_file_prefix}{$q}"; - $this->assertRegExp( - "%^{$expected_bin} {$expected_src} {$expected_dest}$%", - $pdf->previewShellCommand() - ); + static::assertRegExp("%^{$expected_bin} {$expected_src} {$expected_dest}$%", $pdf->previewShellCommand()); } + /** + * @throws PopplerPhpException + */ public function testMakeCommandMethodWithoutOutputFile() { $q = PHP_OS === 'WINNT' ? "\"" : "'"; @@ -127,16 +146,16 @@ public function testMakeCommandMethodWithoutOutputFile() $bin_dir = Config::getBinDirectory(); $bin_file = C::PDF_INFO; - $expected_bin = "{$q}{$bin_dir}{$DS}{$bin_file}{$q}"; + $expected_bin = PHP_OS === 'WINNT' ? "{$q}".implode($DS, [$bin_dir, $bin_file])."{$q}" : $q.$bin_file.$q; $expected_option_str = "-f {$expected_page_from} -l {$expected_page_to}"; $expected_src = "{$q}{$file}{$q}"; - $this->assertRegExp( - "%^{$expected_bin} {$expected_option_str} {$expected_src}$%", - $pdf->previewShellCommand() - ); + static::assertRegExp("%^{$expected_bin} {$expected_option_str} {$expected_src}$%", $pdf->previewShellCommand()); } + /** + * @throws PopplerPhpException + */ public function testMakeCommandMethodWithMultipleSourcePDFs() { $q = PHP_OS === 'WINNT' ? "\"" : "'"; @@ -157,13 +176,10 @@ public function testMakeCommandMethodWithMultipleSourcePDFs() $output_sub_dir = $pdf->getOutputSubDir(); $output_file_ext = $pdf->outputExtension(); - $expected_bin = "{$q}{$bin_dir}{$DS}{$bin_file}{$q}"; + $expected_bin = PHP_OS === 'WINNT' ? "{$q}".implode($DS, [$bin_dir, $bin_file])."{$q}" : $q.$bin_file.$q; $expected_src = "{$q}{$file1}{$q} {$q}{$file2}{$q} {$q}{$file3}{$q}"; $expected_dest = "{$q}{$output_dir}{$DS}{$output_sub_dir}{$DS}{$output_file_prefix}{$output_file_ext}{$q}"; - $this->assertRegExp( - "%^{$expected_bin} {$expected_src} {$expected_dest}$%", - $pdf->previewShellCommand() - ); + static::assertRegExp("%^{$expected_bin} {$expected_src} {$expected_dest}$%", $pdf->previewShellCommand()); } }