1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Marcin. |
4
|
|
|
* Date: 03.03.2019 |
5
|
|
|
* Time: 14:22 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Mrcnpdlk\Api\Unoconv; |
9
|
|
|
|
10
|
|
|
use mikehaertl\shellcommand\Command; |
11
|
|
|
use Mrcnpdlk\Api\Unoconv\Enum\FormatType; |
12
|
|
|
use Mrcnpdlk\Api\Unoconv\Exception\InvalidFileArgumentException; |
13
|
|
|
use Mrcnpdlk\Api\Unoconv\Exception\UnoconvException; |
14
|
|
|
use SplFileObject; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Api |
18
|
|
|
*/ |
19
|
|
|
class Api |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $params = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Api constructor. |
28
|
|
|
* |
29
|
|
|
* @param \Mrcnpdlk\Api\Unoconv\Config $oConfig |
30
|
|
|
* |
31
|
|
|
* @throws \Mrcnpdlk\Api\Unoconv\Exception |
32
|
|
|
*/ |
33
|
3 |
|
public function __construct(Config $oConfig = null) |
34
|
|
|
{ |
35
|
3 |
|
$oConfig = $oConfig ?? new Config(); |
36
|
3 |
|
$this->params['connection'] = $oConfig->getConnectionString(); |
37
|
3 |
|
$this->params['timeout'] = $oConfig->getTimeout(); |
38
|
3 |
|
$this->params['docType'] = $oConfig->getDocType(); |
39
|
3 |
|
$this->params['format'] = $oConfig->getFormat(); |
40
|
3 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $from Path to input file |
44
|
|
|
* @param FormatType|null $format Default PDF |
45
|
|
|
* @param string|null $to Path to output file or directory |
46
|
|
|
* |
47
|
|
|
* @throws \Mrcnpdlk\Api\Unoconv\Exception |
48
|
|
|
* @throws \Mrcnpdlk\Api\Unoconv\Exception\InvalidFileArgumentException |
49
|
|
|
* @throws \Mrcnpdlk\Api\Unoconv\Exception\UnoconvException |
50
|
|
|
* |
51
|
|
|
* @return SplFileObject |
52
|
|
|
*/ |
53
|
2 |
|
public function transcode(string $from, FormatType $format = null, string $to = null): SplFileObject |
54
|
|
|
{ |
55
|
2 |
|
$from = realpath($from); |
56
|
|
|
|
57
|
2 |
|
if (!is_file($from)) { |
58
|
1 |
|
throw new InvalidFileArgumentException(sprintf('Input file "%s" not exists', $from)); |
59
|
|
|
} |
60
|
1 |
|
if (!is_readable($from)) { |
61
|
|
|
throw new InvalidFileArgumentException(sprintf('Input file "%s" is not readable', $from)); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$format = $format ?? $this->params['format']; |
65
|
1 |
|
$fromPathInfo = pathinfo($from); |
66
|
|
|
|
67
|
1 |
|
if (null === $to) { |
68
|
1 |
|
$to = sprintf('%s%s%s.%s', |
69
|
1 |
|
$fromPathInfo['dirname'], |
70
|
1 |
|
DIRECTORY_SEPARATOR, |
71
|
1 |
|
$fromPathInfo['filename'], |
72
|
1 |
|
$format->getExtension() |
73
|
|
|
); |
74
|
|
|
} elseif (is_dir($to)) { |
75
|
|
|
$to = sprintf('%s%s%s.%s', |
76
|
|
|
$to, |
77
|
|
|
DIRECTORY_SEPARATOR, |
78
|
|
|
$fromPathInfo['filename'], |
79
|
|
|
$format->getExtension() |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
$command = new Command($this->params['connection']); |
84
|
|
|
$command |
85
|
1 |
|
->addArg('--doctype', $this->params['docType'], false) |
86
|
1 |
|
->addArg('--format', $format, false) |
87
|
1 |
|
->addArg('--timeout', $this->params['timeout'], false) |
88
|
1 |
|
->addArg('--output', $to) |
89
|
1 |
|
->addArg($from) |
90
|
|
|
; |
91
|
|
|
|
92
|
1 |
|
if ($command->execute()) { |
93
|
|
|
return new SplFileObject($to); |
94
|
|
|
} |
95
|
1 |
|
throw new UnoconvException(sprintf('Unoconv error: %s', $command->getError()), $command->getExitCode()); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|