1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpCfdi\CfdiToPdf\Script; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
class ConvertOptions |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
private $resolverLocation; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
private $fontsDirectory; |
16
|
|
|
|
17
|
|
|
/** @var bool */ |
18
|
|
|
private $doCleanInput; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private $inputFile; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
private $outputFile; |
25
|
|
|
|
26
|
|
|
/** @var bool */ |
27
|
|
|
private $askForHelp; |
28
|
|
|
|
29
|
|
|
/** @var bool */ |
30
|
|
|
private $askForVersion; |
31
|
|
|
|
32
|
10 |
|
public function __construct( |
33
|
|
|
string $resolverLocation, |
34
|
|
|
string $fontsDirectory, |
35
|
|
|
bool $doCleanInput, |
36
|
|
|
string $inputFile, |
37
|
|
|
string $outputFile, |
38
|
|
|
bool $askForHelp, |
39
|
|
|
bool $askForVersion |
40
|
|
|
) { |
41
|
10 |
|
if ('' === $outputFile && '' !== $inputFile) { |
42
|
1 |
|
$outputFile = (string) preg_replace('/\.xml$/', '', $inputFile) . '.pdf'; |
43
|
|
|
} |
44
|
|
|
|
45
|
10 |
|
$this->resolverLocation = $resolverLocation; |
46
|
10 |
|
$this->fontsDirectory = $fontsDirectory; |
47
|
10 |
|
$this->doCleanInput = $doCleanInput; |
48
|
10 |
|
$this->inputFile = $inputFile; |
49
|
10 |
|
$this->outputFile = $outputFile; |
50
|
10 |
|
$this->askForHelp = $askForHelp; |
51
|
10 |
|
$this->askForVersion = $askForVersion; |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
public function askForHelp(): bool |
55
|
|
|
{ |
56
|
3 |
|
return $this->askForHelp; |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
public function askForVersion(): bool |
60
|
|
|
{ |
61
|
3 |
|
return $this->askForVersion; |
62
|
|
|
} |
63
|
|
|
|
64
|
5 |
|
public function inputFile(): string |
65
|
|
|
{ |
66
|
5 |
|
return $this->inputFile; |
67
|
|
|
} |
68
|
|
|
|
69
|
5 |
|
public function outputFile(): string |
70
|
|
|
{ |
71
|
5 |
|
return $this->outputFile; |
72
|
|
|
} |
73
|
|
|
|
74
|
4 |
|
public function doCleanInput(): bool |
75
|
|
|
{ |
76
|
4 |
|
return $this->doCleanInput; |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
public function resolverLocation(): string |
80
|
|
|
{ |
81
|
4 |
|
return $this->resolverLocation; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
public function fontsDirectory(): string |
85
|
|
|
{ |
86
|
3 |
|
return $this->fontsDirectory; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string[] $arguments |
91
|
|
|
* |
92
|
|
|
* @return self |
93
|
|
|
*/ |
94
|
12 |
|
public static function createFromArguments(array $arguments): self |
95
|
|
|
{ |
96
|
12 |
|
$askForHelp = false; |
97
|
12 |
|
$askForVersion = false; |
98
|
12 |
|
$resolverLocation = ''; |
99
|
12 |
|
$fontsDirectory = ''; |
100
|
12 |
|
$cleanInput = true; |
101
|
12 |
|
$inputFile = ''; |
102
|
12 |
|
$outputFile = ''; |
103
|
|
|
|
104
|
12 |
|
$count = count($arguments); |
105
|
12 |
|
for ($i = 0; $i < $count; $i = $i + 1) { |
106
|
11 |
|
$argument = $arguments[$i]; |
107
|
11 |
|
if (in_array($argument, ['-h', '--help'], true)) { |
108
|
1 |
|
$askForHelp = true; |
109
|
1 |
|
break; |
110
|
|
|
} |
111
|
10 |
|
if (in_array($argument, ['-V', '--version'], true)) { |
112
|
1 |
|
$askForVersion = true; |
113
|
1 |
|
break; |
114
|
|
|
} |
115
|
9 |
|
if (in_array($argument, ['-d', '--dirty'], true)) { |
116
|
1 |
|
$cleanInput = false; |
117
|
1 |
|
continue; |
118
|
|
|
} |
119
|
8 |
|
if (in_array($argument, ['-l', '--resource-location'], true)) { |
120
|
4 |
|
$i = $i + 1; |
121
|
4 |
|
if (! ($i < $count)) { |
122
|
1 |
|
throw new RuntimeException('The resource location parameter does not contains an argument'); |
123
|
|
|
} |
124
|
3 |
|
$resolverLocation = $arguments[$i]; |
125
|
3 |
|
continue; |
126
|
|
|
} |
127
|
6 |
|
if (in_array($argument, ['-f', '--fonts-dir'], true)) { |
128
|
2 |
|
$i = $i + 1; |
129
|
2 |
|
if (! ($i < $count)) { |
130
|
1 |
|
throw new RuntimeException('The fonts directory parameter does not contains an argument'); |
131
|
|
|
} |
132
|
1 |
|
$fontsDirectory = $arguments[$i]; |
133
|
1 |
|
continue; |
134
|
|
|
} |
135
|
4 |
|
if ('' === $inputFile) { |
136
|
4 |
|
$inputFile = $argument; |
137
|
4 |
|
continue; |
138
|
|
|
} |
139
|
3 |
|
if ('' === $outputFile) { |
140
|
3 |
|
$outputFile = $argument; |
141
|
3 |
|
continue; |
142
|
|
|
} |
143
|
1 |
|
throw new RuntimeException("Unexpected parameter '$argument'"); |
144
|
|
|
} |
145
|
|
|
|
146
|
9 |
|
return new self( |
147
|
9 |
|
$resolverLocation, |
148
|
9 |
|
$fontsDirectory, |
149
|
9 |
|
$cleanInput, |
150
|
9 |
|
$inputFile, |
151
|
9 |
|
$outputFile, |
152
|
9 |
|
$askForHelp, |
153
|
9 |
|
$askForVersion, |
154
|
9 |
|
); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|