33namespace mikehaertl \pdftk ;
44
55use mikehaertl \shellcommand \Command as BaseCommand ;
6+ use mikehaertl \tmp \File ;
7+ use Override ;
68
79/**
810 * Command
1618class Command extends BaseCommand
1719{
1820 /**
19- * @var string the pdftk binary
21+ * The pdftk binary
2022 */
2123 protected $ _command = 'pdftk ' ;
2224
2325 /**
24- * @var array list of input files to process as array('name' => $filename,
26+ * List of input files to process as array('name' => $filename,
2527 * 'password' => $pw) indexed by handle
2628 */
27- protected $ _files = array ();
29+ protected array $ _files = array ();
2830
2931 /**
30- * @var array list of command options, either strings or array with
31- * arguments to addArg()
32+ * List of command options, either strings or array with arguments to addArg()
3233 */
33- protected $ _options = array ();
34+ protected array $ _options = array ();
3435
3536 /**
36- * @var string the operation to perform
37+ * The operation to perform
3738 */
38- protected $ _operation ;
39+ protected ? string $ _operation = null ;
3940
4041 /**
41- * @var string|array operation arguments, e.g. a list of page ranges or a
42- * filename or tmp file instance
42+ * Operation arguments, e.g. a list of page ranges or a filename or tmp file instance
4343 */
44- protected $ _operationArgument = array ();
44+ protected string | array | File $ _operationArgument = array ();
4545
4646 /**
47- * @var bool whether to force escaping of the operation argument e.g. for
48- * filenames
47+ * Whether to force escaping of the operation argument e.g. for filenames
4948 */
50- protected $ _escapeOperationArgument = false ;
49+ protected bool $ _escapeOperationArgument = false ;
5150
5251 /**
5352 * @param string $name the PDF file to add for processing
@@ -57,7 +56,7 @@ class Command extends BaseCommand
5756 * @return Command the command instance for method chaining
5857 * @throws \Exception
5958 */
60- public function addFile ($ name , $ handle , $ password = null )
59+ public function addFile ($ name , $ handle , $ password = null ): self
6160 {
6261 $ this ->checkExecutionStatus ();
6362 $ file = array (
@@ -76,7 +75,7 @@ public function addFile($name, $handle, $password = null)
7675 * use Command default setting.
7776 * @return Command the command instance for method chaining
7877 */
79- public function addOption ($ option , $ argument = null , $ escape = null )
78+ public function addOption ($ option , $ argument = null , ? bool $ escape = null ): self
8079 {
8180 $ this ->_options [] = $ argument === null ? $ option : array ($ option , $ argument , $ escape );
8281 return $ this ;
@@ -86,7 +85,7 @@ public function addOption($option, $argument = null, $escape = null)
8685 * @param string $operation the operation to perform
8786 * @return Command the command instance for method chaining
8887 */
89- public function setOperation ($ operation )
88+ public function setOperation ($ operation ): self
9089 {
9190 $ this ->checkExecutionStatus ();
9291 $ this ->_operation = $ operation ;
@@ -102,11 +101,11 @@ public function getOperation()
102101 }
103102
104103 /**
105- * @param string $value the operation argument
104+ * @param string|array $value the operation argument
106105 * @param bool $escape whether to escape the operation argument
107106 * @return Command the command instance for method chaining
108107 */
109- public function setOperationArgument ($ value , $ escape = false )
108+ public function setOperationArgument (string | array | File $ value , bool $ escape = false ): self
110109 {
111110 $ this ->checkExecutionStatus ();
112111 $ this ->_operationArgument = $ value ;
@@ -118,7 +117,7 @@ public function setOperationArgument($value, $escape = false)
118117 * @return string|array|null the current operation argument as string or
119118 * array or null if none set
120119 */
121- public function getOperationArgument ()
120+ public function getOperationArgument (): string | array | null
122121 {
123122 // Typecast to string in case we have a File instance as argument
124123 return is_array ($ this ->_operationArgument ) ? $ this ->_operationArgument : (string ) $ this ->_operationArgument ;
@@ -127,7 +126,7 @@ public function getOperationArgument()
127126 /**
128127 * @return int the number of files added to the command
129128 */
130- public function getFileCount ()
129+ public function getFileCount (): int
131130 {
132131 return count ($ this ->_files );
133132 }
@@ -144,17 +143,20 @@ public function getFileCount()
144143 * only a single file was added.
145144 * @param string|null $qualifier the page number qualifier, either 'even'
146145 * or 'odd' or null for none
147- * @param string $rotation the rotation to apply to the pages.
146+ * @param string|null $rotation the rotation to apply to the pages.
148147 * @return Command the command instance for method chaining
149148 */
150- public function addPageRange ($ start , $ end = null , $ handle = null , $ qualifier = null , $ rotation = null )
151- {
149+ public function addPageRange (
150+ int |string |array $ start ,
151+ int |string |null $ end = null ,
152+ string |null $ handle = null ,
153+ string |null $ qualifier = null ,
154+ string |null $ rotation = null ,
155+ ) {
152156 $ this ->checkExecutionStatus ();
153157 if (is_array ($ start )) {
154158 if ($ handle !== null ) {
155- $ start = array_map (function ($ p ) use ($ handle ) {
156- return $ handle . $ p ;
157- }, $ start );
159+ $ start = array_map (fn ($ p ) => $ handle . $ p , $ start );
158160 }
159161 $ range = implode (' ' , $ start );
160162 } else {
@@ -164,6 +166,13 @@ public function addPageRange($start, $end = null, $handle = null, $qualifier = n
164166 }
165167 $ range .= $ qualifier . $ rotation ;
166168 }
169+ if (!is_array ($ this ->_operationArgument )) {
170+ if (!empty ($ this ->_operationArgument )) {
171+ $ this ->_operationArgument = array ($ this ->_operationArgument );
172+ } else {
173+ $ this ->_operationArgument = array ();
174+ }
175+ }
167176 $ this ->_operationArgument [] = $ range ;
168177 return $ this ;
169178 }
@@ -173,6 +182,7 @@ public function addPageRange($start, $end = null, $handle = null, $qualifier = n
173182 * null if none
174183 * @return bool whether the command was executed successfully
175184 */
185+ #[Override]
176186 public function execute ($ filename = null )
177187 {
178188 $ this ->checkExecutionStatus ();
@@ -185,7 +195,7 @@ public function execute($filename = null)
185195 /**
186196 * Process input PDF files and create respective command arguments
187197 */
188- protected function processInputFiles ()
198+ protected function processInputFiles (): void
189199 {
190200 $ passwords = array ();
191201 foreach ($ this ->_files as $ handle => $ file ) {
@@ -204,10 +214,11 @@ protected function processInputFiles()
204214
205215 /**
206216 * Process options and create respective command arguments
217+ *
207218 * @param string|null $filename if provided an 'output' option will be
208219 * added
209220 */
210- protected function processOptions ($ filename = null )
221+ protected function processOptions ($ filename = null ): void
211222 {
212223 // output must be first option after operation
213224 if ($ filename !== null ) {
@@ -225,23 +236,21 @@ protected function processOptions($filename = null)
225236 /**
226237 * Process opearation and create respective command arguments
227238 */
228- protected function processOperation ()
239+ protected function processOperation (): void
229240 {
230241 if ($ this ->_operation !== null ) {
231242 $ value = $ this ->_operationArgument ? $ this ->_operationArgument : null ;
232- if ($ value instanceof TmpFile) {
233- $ value = (string ) $ value ;
234- }
235243 $ this ->addArg ($ this ->_operation , $ value , $ this ->_escapeOperationArgument );
236244 }
237245 }
238246
239247 /**
240248 * Ensure that the command was not exectued yet. Throws exception
241249 * otherwise.
250+ *
242251 * @throws \Exception
243252 */
244- protected function checkExecutionStatus ()
253+ protected function checkExecutionStatus (): void
245254 {
246255 if ($ this ->getExecuted ()) {
247256 throw new \Exception ('Operation was already executed ' );
0 commit comments