3
3
namespace mikehaertl \pdftk ;
4
4
5
5
use mikehaertl \shellcommand \Command as BaseCommand ;
6
+ use mikehaertl \tmp \File ;
7
+ use Override ;
6
8
7
9
/**
8
10
* Command
16
18
class Command extends BaseCommand
17
19
{
18
20
/**
19
- * @var string the pdftk binary
21
+ * The pdftk binary
20
22
*/
21
23
protected $ _command = 'pdftk ' ;
22
24
23
25
/**
24
- * @var array list of input files to process as array('name' => $filename,
26
+ * List of input files to process as array('name' => $filename,
25
27
* 'password' => $pw) indexed by handle
26
28
*/
27
- protected $ _files = array ();
29
+ protected array $ _files = array ();
28
30
29
31
/**
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()
32
33
*/
33
- protected $ _options = array ();
34
+ protected array $ _options = array ();
34
35
35
36
/**
36
- * @var string the operation to perform
37
+ * The operation to perform
37
38
*/
38
- protected $ _operation ;
39
+ protected ? string $ _operation = null ;
39
40
40
41
/**
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
43
43
*/
44
- protected $ _operationArgument = array ();
44
+ protected string | array | File $ _operationArgument = array ();
45
45
46
46
/**
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
49
48
*/
50
- protected $ _escapeOperationArgument = false ;
49
+ protected bool $ _escapeOperationArgument = false ;
51
50
52
51
/**
53
52
* @param string $name the PDF file to add for processing
@@ -57,7 +56,7 @@ class Command extends BaseCommand
57
56
* @return Command the command instance for method chaining
58
57
* @throws \Exception
59
58
*/
60
- public function addFile ($ name , $ handle , $ password = null )
59
+ public function addFile ($ name , $ handle , $ password = null ): self
61
60
{
62
61
$ this ->checkExecutionStatus ();
63
62
$ file = array (
@@ -76,7 +75,7 @@ public function addFile($name, $handle, $password = null)
76
75
* use Command default setting.
77
76
* @return Command the command instance for method chaining
78
77
*/
79
- public function addOption ($ option , $ argument = null , $ escape = null )
78
+ public function addOption ($ option , $ argument = null , ? bool $ escape = null ): self
80
79
{
81
80
$ this ->_options [] = $ argument === null ? $ option : array ($ option , $ argument , $ escape );
82
81
return $ this ;
@@ -86,7 +85,7 @@ public function addOption($option, $argument = null, $escape = null)
86
85
* @param string $operation the operation to perform
87
86
* @return Command the command instance for method chaining
88
87
*/
89
- public function setOperation ($ operation )
88
+ public function setOperation ($ operation ): self
90
89
{
91
90
$ this ->checkExecutionStatus ();
92
91
$ this ->_operation = $ operation ;
@@ -102,11 +101,11 @@ public function getOperation()
102
101
}
103
102
104
103
/**
105
- * @param string $value the operation argument
104
+ * @param string|array $value the operation argument
106
105
* @param bool $escape whether to escape the operation argument
107
106
* @return Command the command instance for method chaining
108
107
*/
109
- public function setOperationArgument ($ value , $ escape = false )
108
+ public function setOperationArgument (string | array | File $ value , bool $ escape = false ): self
110
109
{
111
110
$ this ->checkExecutionStatus ();
112
111
$ this ->_operationArgument = $ value ;
@@ -118,7 +117,7 @@ public function setOperationArgument($value, $escape = false)
118
117
* @return string|array|null the current operation argument as string or
119
118
* array or null if none set
120
119
*/
121
- public function getOperationArgument ()
120
+ public function getOperationArgument (): string | array | null
122
121
{
123
122
// Typecast to string in case we have a File instance as argument
124
123
return is_array ($ this ->_operationArgument ) ? $ this ->_operationArgument : (string ) $ this ->_operationArgument ;
@@ -127,7 +126,7 @@ public function getOperationArgument()
127
126
/**
128
127
* @return int the number of files added to the command
129
128
*/
130
- public function getFileCount ()
129
+ public function getFileCount (): int
131
130
{
132
131
return count ($ this ->_files );
133
132
}
@@ -144,17 +143,20 @@ public function getFileCount()
144
143
* only a single file was added.
145
144
* @param string|null $qualifier the page number qualifier, either 'even'
146
145
* 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.
148
147
* @return Command the command instance for method chaining
149
148
*/
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
+ ) {
152
156
$ this ->checkExecutionStatus ();
153
157
if (is_array ($ start )) {
154
158
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 );
158
160
}
159
161
$ range = implode (' ' , $ start );
160
162
} else {
@@ -164,6 +166,13 @@ public function addPageRange($start, $end = null, $handle = null, $qualifier = n
164
166
}
165
167
$ range .= $ qualifier . $ rotation ;
166
168
}
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
+ }
167
176
$ this ->_operationArgument [] = $ range ;
168
177
return $ this ;
169
178
}
@@ -173,6 +182,7 @@ public function addPageRange($start, $end = null, $handle = null, $qualifier = n
173
182
* null if none
174
183
* @return bool whether the command was executed successfully
175
184
*/
185
+ #[Override]
176
186
public function execute ($ filename = null )
177
187
{
178
188
$ this ->checkExecutionStatus ();
@@ -185,7 +195,7 @@ public function execute($filename = null)
185
195
/**
186
196
* Process input PDF files and create respective command arguments
187
197
*/
188
- protected function processInputFiles ()
198
+ protected function processInputFiles (): void
189
199
{
190
200
$ passwords = array ();
191
201
foreach ($ this ->_files as $ handle => $ file ) {
@@ -204,10 +214,11 @@ protected function processInputFiles()
204
214
205
215
/**
206
216
* Process options and create respective command arguments
217
+ *
207
218
* @param string|null $filename if provided an 'output' option will be
208
219
* added
209
220
*/
210
- protected function processOptions ($ filename = null )
221
+ protected function processOptions ($ filename = null ): void
211
222
{
212
223
// output must be first option after operation
213
224
if ($ filename !== null ) {
@@ -225,23 +236,21 @@ protected function processOptions($filename = null)
225
236
/**
226
237
* Process opearation and create respective command arguments
227
238
*/
228
- protected function processOperation ()
239
+ protected function processOperation (): void
229
240
{
230
241
if ($ this ->_operation !== null ) {
231
242
$ value = $ this ->_operationArgument ? $ this ->_operationArgument : null ;
232
- if ($ value instanceof TmpFile) {
233
- $ value = (string ) $ value ;
234
- }
235
243
$ this ->addArg ($ this ->_operation , $ value , $ this ->_escapeOperationArgument );
236
244
}
237
245
}
238
246
239
247
/**
240
248
* Ensure that the command was not exectued yet. Throws exception
241
249
* otherwise.
250
+ *
242
251
* @throws \Exception
243
252
*/
244
- protected function checkExecutionStatus ()
253
+ protected function checkExecutionStatus (): void
245
254
{
246
255
if ($ this ->getExecuted ()) {
247
256
throw new \Exception ('Operation was already executed ' );
0 commit comments