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
@@ -24,30 +26,30 @@ class Command extends BaseCommand
24
26
* @var array 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
32
* @var array list of command options, either strings or array with
31
33
* arguments to addArg()
32
34
*/
33
- protected $ _options = array ();
35
+ protected array $ _options = array ();
34
36
35
37
/**
36
38
* @var string the operation to perform
37
39
*/
38
- protected $ _operation ;
40
+ protected ? string $ _operation = null ;
39
41
40
42
/**
41
43
* @var string|array operation arguments, e.g. a list of page ranges or a
42
44
* filename or tmp file instance
43
45
*/
44
- protected $ _operationArgument = array ();
46
+ protected string | array | File $ _operationArgument = array ();
45
47
46
48
/**
47
49
* @var bool whether to force escaping of the operation argument e.g. for
48
50
* filenames
49
51
*/
50
- protected $ _escapeOperationArgument = false ;
52
+ protected bool $ _escapeOperationArgument = false ;
51
53
52
54
/**
53
55
* @param string $name the PDF file to add for processing
@@ -57,7 +59,7 @@ class Command extends BaseCommand
57
59
* @return Command the command instance for method chaining
58
60
* @throws \Exception
59
61
*/
60
- public function addFile ($ name , $ handle , $ password = null )
62
+ public function addFile ($ name , $ handle , $ password = null ): self
61
63
{
62
64
$ this ->checkExecutionStatus ();
63
65
$ file = array (
@@ -76,7 +78,7 @@ public function addFile($name, $handle, $password = null)
76
78
* use Command default setting.
77
79
* @return Command the command instance for method chaining
78
80
*/
79
- public function addOption ($ option , $ argument = null , $ escape = null )
81
+ public function addOption ($ option , $ argument = null , ? bool $ escape = null ): self
80
82
{
81
83
$ this ->_options [] = $ argument === null ? $ option : array ($ option , $ argument , $ escape );
82
84
return $ this ;
@@ -86,7 +88,7 @@ public function addOption($option, $argument = null, $escape = null)
86
88
* @param string $operation the operation to perform
87
89
* @return Command the command instance for method chaining
88
90
*/
89
- public function setOperation ($ operation )
91
+ public function setOperation ($ operation ): self
90
92
{
91
93
$ this ->checkExecutionStatus ();
92
94
$ this ->_operation = $ operation ;
@@ -102,11 +104,11 @@ public function getOperation()
102
104
}
103
105
104
106
/**
105
- * @param string $value the operation argument
107
+ * @param string|array $value the operation argument
106
108
* @param bool $escape whether to escape the operation argument
107
109
* @return Command the command instance for method chaining
108
110
*/
109
- public function setOperationArgument ($ value , $ escape = false )
111
+ public function setOperationArgument (string | array | File $ value , bool $ escape = false ): self
110
112
{
111
113
$ this ->checkExecutionStatus ();
112
114
$ this ->_operationArgument = $ value ;
@@ -118,7 +120,7 @@ public function setOperationArgument($value, $escape = false)
118
120
* @return string|array|null the current operation argument as string or
119
121
* array or null if none set
120
122
*/
121
- public function getOperationArgument ()
123
+ public function getOperationArgument (): string | array | null
122
124
{
123
125
// Typecast to string in case we have a File instance as argument
124
126
return is_array ($ this ->_operationArgument ) ? $ this ->_operationArgument : (string ) $ this ->_operationArgument ;
@@ -127,7 +129,7 @@ public function getOperationArgument()
127
129
/**
128
130
* @return int the number of files added to the command
129
131
*/
130
- public function getFileCount ()
132
+ public function getFileCount (): int
131
133
{
132
134
return count ($ this ->_files );
133
135
}
@@ -144,17 +146,20 @@ public function getFileCount()
144
146
* only a single file was added.
145
147
* @param string|null $qualifier the page number qualifier, either 'even'
146
148
* or 'odd' or null for none
147
- * @param string $rotation the rotation to apply to the pages.
149
+ * @param string|null $rotation the rotation to apply to the pages.
148
150
* @return Command the command instance for method chaining
149
151
*/
150
- public function addPageRange ($ start , $ end = null , $ handle = null , $ qualifier = null , $ rotation = null )
151
- {
152
+ public function addPageRange (
153
+ int |string |array $ start ,
154
+ int |string |null $ end = null ,
155
+ string |null $ handle = null ,
156
+ string |null $ qualifier = null ,
157
+ string |null $ rotation = null ,
158
+ ) {
152
159
$ this ->checkExecutionStatus ();
153
160
if (is_array ($ start )) {
154
161
if ($ handle !== null ) {
155
- $ start = array_map (function ($ p ) use ($ handle ) {
156
- return $ handle . $ p ;
157
- }, $ start );
162
+ $ start = array_map (fn ($ p ) => $ handle . $ p , $ start );
158
163
}
159
164
$ range = implode (' ' , $ start );
160
165
} else {
@@ -173,6 +178,7 @@ public function addPageRange($start, $end = null, $handle = null, $qualifier = n
173
178
* null if none
174
179
* @return bool whether the command was executed successfully
175
180
*/
181
+ #[Override]
176
182
public function execute ($ filename = null )
177
183
{
178
184
$ this ->checkExecutionStatus ();
@@ -185,7 +191,7 @@ public function execute($filename = null)
185
191
/**
186
192
* Process input PDF files and create respective command arguments
187
193
*/
188
- protected function processInputFiles ()
194
+ protected function processInputFiles (): void
189
195
{
190
196
$ passwords = array ();
191
197
foreach ($ this ->_files as $ handle => $ file ) {
@@ -204,10 +210,11 @@ protected function processInputFiles()
204
210
205
211
/**
206
212
* Process options and create respective command arguments
213
+ *
207
214
* @param string|null $filename if provided an 'output' option will be
208
215
* added
209
216
*/
210
- protected function processOptions ($ filename = null )
217
+ protected function processOptions ($ filename = null ): void
211
218
{
212
219
// output must be first option after operation
213
220
if ($ filename !== null ) {
@@ -225,23 +232,21 @@ protected function processOptions($filename = null)
225
232
/**
226
233
* Process opearation and create respective command arguments
227
234
*/
228
- protected function processOperation ()
235
+ protected function processOperation (): void
229
236
{
230
237
if ($ this ->_operation !== null ) {
231
238
$ value = $ this ->_operationArgument ? $ this ->_operationArgument : null ;
232
- if ($ value instanceof TmpFile) {
233
- $ value = (string ) $ value ;
234
- }
235
239
$ this ->addArg ($ this ->_operation , $ value , $ this ->_escapeOperationArgument );
236
240
}
237
241
}
238
242
239
243
/**
240
244
* Ensure that the command was not exectued yet. Throws exception
241
245
* otherwise.
246
+ *
242
247
* @throws \Exception
243
248
*/
244
- protected function checkExecutionStatus ()
249
+ protected function checkExecutionStatus (): void
245
250
{
246
251
if ($ this ->getExecuted ()) {
247
252
throw new \Exception ('Operation was already executed ' );
0 commit comments