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 bool */ |
15
|
|
|
private $doCleanInput; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
private $inputFile; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private $outputFile; |
22
|
|
|
|
23
|
|
|
/** @var bool */ |
24
|
|
|
private $askForHelp; |
25
|
|
|
|
26
|
|
|
/** @var bool */ |
27
|
|
|
private $askForVersion; |
28
|
|
|
|
29
|
9 |
|
public function __construct( |
30
|
|
|
string $resolverLocation, |
31
|
|
|
bool $doCleanInput, |
32
|
|
|
string $inputFile, |
33
|
|
|
string $outputFile, |
34
|
|
|
bool $askForHelp, |
35
|
|
|
bool $askForVersion |
36
|
|
|
) { |
37
|
9 |
|
if ('' === $outputFile && '' !== $inputFile) { |
38
|
1 |
|
$outputFile = (string) preg_replace('/\.xml$/', '', $inputFile) . '.pdf'; |
39
|
|
|
} |
40
|
|
|
|
41
|
9 |
|
$this->resolverLocation = $resolverLocation; |
42
|
9 |
|
$this->doCleanInput = $doCleanInput; |
43
|
9 |
|
$this->inputFile = $inputFile; |
44
|
9 |
|
$this->outputFile = $outputFile; |
45
|
9 |
|
$this->askForHelp = $askForHelp; |
46
|
9 |
|
$this->askForVersion = $askForVersion; |
47
|
9 |
|
} |
48
|
|
|
|
49
|
3 |
|
public function askForHelp(): bool |
50
|
|
|
{ |
51
|
3 |
|
return $this->askForHelp; |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
public function askForVersion(): bool |
55
|
|
|
{ |
56
|
3 |
|
return $this->askForVersion; |
57
|
|
|
} |
58
|
|
|
|
59
|
5 |
|
public function inputFile(): string |
60
|
|
|
{ |
61
|
5 |
|
return $this->inputFile; |
62
|
|
|
} |
63
|
|
|
|
64
|
5 |
|
public function outputFile(): string |
65
|
|
|
{ |
66
|
5 |
|
return $this->outputFile; |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
public function doCleanInput(): bool |
70
|
|
|
{ |
71
|
4 |
|
return $this->doCleanInput; |
72
|
|
|
} |
73
|
|
|
|
74
|
4 |
|
public function resolverLocation(): string |
75
|
|
|
{ |
76
|
4 |
|
return $this->resolverLocation; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $arguments |
81
|
|
|
* |
82
|
|
|
* @return self |
83
|
|
|
*/ |
84
|
10 |
|
public static function createFromArguments(array $arguments): self |
85
|
|
|
{ |
86
|
10 |
|
$askForHelp = false; |
87
|
10 |
|
$askForVersion = false; |
88
|
10 |
|
$resolverLocation = ''; |
89
|
10 |
|
$cleanInput = true; |
90
|
10 |
|
$inputFile = ''; |
91
|
10 |
|
$outputFile = ''; |
92
|
|
|
|
93
|
10 |
|
$count = count($arguments); |
94
|
10 |
|
for ($i = 0; $i < $count; $i = $i + 1) { |
95
|
9 |
|
$argument = $arguments[$i]; |
96
|
9 |
|
if (in_array($argument, ['-h', '--help'], true)) { |
97
|
1 |
|
$askForHelp = true; |
98
|
1 |
|
break; |
99
|
|
|
} |
100
|
8 |
|
if (in_array($argument, ['-v', '--version'], true)) { |
101
|
1 |
|
$askForVersion = true; |
102
|
1 |
|
break; |
103
|
|
|
} |
104
|
7 |
|
if (in_array($argument, ['-d', '--dirty'], true)) { |
105
|
1 |
|
$cleanInput = false; |
106
|
1 |
|
continue; |
107
|
|
|
} |
108
|
6 |
|
if (in_array($argument, ['-l', '--resource-location'], true)) { |
109
|
4 |
|
$i = $i + 1; |
110
|
4 |
|
if (! ($i < $count)) { |
111
|
1 |
|
throw new RuntimeException('The resource location parameter does not contains an argument'); |
112
|
|
|
} |
113
|
3 |
|
$resolverLocation = $arguments[$i]; |
114
|
3 |
|
continue; |
115
|
|
|
} |
116
|
4 |
|
if ('' === $inputFile) { |
117
|
4 |
|
$inputFile = $argument; |
118
|
4 |
|
continue; |
119
|
|
|
} |
120
|
3 |
|
if ('' === $outputFile) { |
121
|
3 |
|
$outputFile = $argument; |
122
|
3 |
|
continue; |
123
|
|
|
} |
124
|
1 |
|
throw new RuntimeException("Unexpected parameter '$argument'"); |
125
|
|
|
} |
126
|
|
|
|
127
|
8 |
|
return new self($resolverLocation, $cleanInput, $inputFile, $outputFile, $askForHelp, $askForVersion); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|