1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpCfdi\SatCatalogosPopulate\Converters; |
6
|
|
|
|
7
|
|
|
use PhpCfdi\SatCatalogosPopulate\Utils\ShellExec; |
8
|
|
|
use PhpCfdi\SatCatalogosPopulate\Utils\WhichTrait; |
9
|
|
|
use RuntimeException; |
10
|
|
|
|
11
|
|
|
class XlsxToCsvFolderConverter |
12
|
|
|
{ |
13
|
|
|
use WhichTrait; |
14
|
|
|
|
15
|
|
|
/** @var string Location of xlsx2csvPath executable */ |
16
|
|
|
private readonly string $xlsx2csvPath; |
17
|
|
|
|
18
|
4 |
|
public function __construct(string $xlsx2csvPath = '') |
19
|
|
|
{ |
20
|
4 |
|
if ('' === $xlsx2csvPath) { |
21
|
4 |
|
$xlsx2csvPath = $this->which('xlsx2csv'); |
22
|
|
|
} |
23
|
4 |
|
$this->xlsx2csvPath = $xlsx2csvPath; |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
4 |
|
public function xlsx2csvPath(): string |
27
|
|
|
{ |
28
|
4 |
|
return $this->xlsx2csvPath; |
29
|
|
|
} |
30
|
|
|
|
31
|
4 |
|
public function convert(string $source, string $destination): void |
32
|
|
|
{ |
33
|
4 |
|
if ('' === $destination) { |
34
|
|
|
throw new RuntimeException('Destination is empty'); |
35
|
|
|
} |
36
|
4 |
|
if (! is_dir($destination) || ! is_writable($destination)) { |
37
|
|
|
throw new RuntimeException("Destination directory $destination is not a directory or is not writable"); |
38
|
|
|
} |
39
|
|
|
|
40
|
4 |
|
$command = implode(' ', array_map('escapeshellarg', [ |
41
|
4 |
|
$this->xlsx2csvPath(), |
42
|
4 |
|
'--ignoreempty', |
43
|
4 |
|
'--escape', |
44
|
4 |
|
'--all', |
45
|
4 |
|
'--dateformat', |
46
|
4 |
|
'%Y-%m-%d', |
47
|
4 |
|
$source, |
48
|
4 |
|
$destination, |
49
|
4 |
|
])); |
50
|
|
|
|
51
|
4 |
|
$execution = ShellExec::run($command); |
52
|
4 |
|
if (0 !== $execution->exitStatus()) { |
53
|
|
|
throw new RuntimeException( |
54
|
|
|
"Execution of xlsx2csv conversion return a non zero status code [{$execution->exitStatus()}]" |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// remove spaces on exported file name |
59
|
4 |
|
$csvFiles = glob($destination . '/*.csv') ?: []; |
60
|
4 |
|
foreach ($csvFiles as $csvFile) { |
61
|
4 |
|
$this->removeTrailCommasOnFile($csvFile); |
62
|
|
|
|
63
|
4 |
|
$renamed = $destination . '/' . preg_replace('/\s*(.*?)\s*\.csv+/', '$1.csv', basename($csvFile)); |
64
|
4 |
|
if (! file_exists($renamed) && $csvFile !== $renamed) { |
65
|
1 |
|
rename($csvFile, $renamed); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
4 |
|
private function removeTrailCommasOnFile(string $csvFile): void |
71
|
|
|
{ |
72
|
4 |
|
$command = implode(' ', array_map('escapeshellarg', [ |
73
|
4 |
|
'sed', |
74
|
4 |
|
'--in-place', |
75
|
4 |
|
'--regexp-extended', 's/,+$//g', |
76
|
4 |
|
$csvFile, |
77
|
4 |
|
])); |
78
|
|
|
|
79
|
4 |
|
$execution = ShellExec::run($command); |
80
|
4 |
|
if (0 !== $execution->exitStatus()) { |
81
|
|
|
throw new RuntimeException( |
82
|
|
|
"Remove trailing commas failed with code [{$execution->exitStatus()}] for file $csvFile" |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|