1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kasifi\PdfParserBundle\Command; |
4
|
|
|
|
5
|
|
|
use Kasifi\PdfParserBundle\PdfParser; |
6
|
|
|
use Kasifi\PdfParserBundle\Util\ParseHelper; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
8
|
|
|
use Symfony\Component\Console\Helper\Table; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
14
|
|
|
use Symfony\Component\Finder\Finder; |
15
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
16
|
|
|
use Symfony\Component\Yaml\Dumper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ParserCommand. |
20
|
|
|
*/ |
21
|
|
|
class ParserCommand extends ContainerAwareCommand |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var OutputInterface |
25
|
|
|
*/ |
26
|
|
|
private $output; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Configure the command. |
30
|
|
|
*/ |
31
|
|
|
protected function configure() |
32
|
|
|
{ |
33
|
|
|
$this |
34
|
|
|
->setName('pdf-parser:parse') |
35
|
|
|
->setDescription('Parse document of many types.') |
36
|
|
|
->addArgument('processor', InputArgument::OPTIONAL, 'The id of the processor') |
37
|
|
|
->addArgument('file_path', InputArgument::OPTIONAL, 'The absolute path to the PDF file to parse.') |
38
|
|
|
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (console, json, yml)', 'console'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param InputInterface $input |
43
|
|
|
* @param OutputInterface $output |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
48
|
|
|
{ |
49
|
|
|
$this->output = $output; |
50
|
|
|
$container = $this->getContainer(); |
51
|
|
|
$pdfParser = $container->get('kasifi_pdfparser.pdf_parser'); |
52
|
|
|
$pdfDirectoryPath = $container->getParameter('kasifi_pdfparser.pdf_directory_path'); |
53
|
|
|
|
54
|
|
|
// Select processor |
55
|
|
|
$this->selectProcessor($input, $pdfParser); |
56
|
|
|
|
57
|
|
|
// Select file |
58
|
|
|
$filePath = $this->selectFile($input, $pdfDirectoryPath); |
59
|
|
|
|
60
|
|
|
// Parse |
61
|
|
|
$rows = $pdfParser->parse($filePath); |
62
|
|
|
$data = ParseHelper::inlineDates($rows->toArray()); |
63
|
|
|
|
64
|
|
|
// Write output |
65
|
|
|
$this->writeOutput($input->getOption('format'), $data); |
66
|
|
|
|
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $format |
72
|
|
|
* @param $data |
73
|
|
|
*/ |
74
|
|
|
protected function writeOutput($format, $data) |
75
|
|
|
{ |
76
|
|
|
switch ($format) { |
77
|
|
|
case 'json': |
78
|
|
|
$outputData = json_encode($data); |
79
|
|
|
$this->output->write($outputData); |
80
|
|
|
break; |
81
|
|
|
case 'yml': |
82
|
|
|
$dumper = new Dumper(); |
83
|
|
|
$outputData = $dumper->dump($data, 1); |
84
|
|
|
$this->output->write($outputData); |
85
|
|
|
break; |
86
|
|
|
case 'console': |
87
|
|
|
if (count($data)) { |
88
|
|
|
$table = new Table($this->output); |
89
|
|
|
$table |
90
|
|
|
->setHeaders([array_keys($data[0])]) |
91
|
|
|
->setRows($data); |
92
|
|
|
$table->render(); |
93
|
|
|
} |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param InputInterface $input |
100
|
|
|
* @param $pdfDirectoryPath |
101
|
|
|
* |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
|
|
protected function selectFile(InputInterface $input, $pdfDirectoryPath) |
105
|
|
|
{ |
106
|
|
|
$filePath = $input->getArgument('file_path'); |
107
|
|
|
if (!$filePath) { |
108
|
|
|
$helper = $this->getHelper('question'); |
109
|
|
|
$finder = new Finder(); |
110
|
|
|
$finder->files()->in($pdfDirectoryPath); |
111
|
|
|
$files = []; |
112
|
|
|
foreach ($finder as $key => $file) { |
113
|
|
|
/* @var SplFileInfo $file */ |
114
|
|
|
$files[] = $file->getRealPath(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$question = new ChoiceQuestion('Which file? Enter the key.', $files); |
118
|
|
|
$filePath = $helper->ask($input, $this->output, $question); |
119
|
|
|
|
120
|
|
|
return $filePath; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $filePath; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param InputInterface $input |
128
|
|
|
* @param $pdfParser |
129
|
|
|
*/ |
130
|
|
|
protected function selectProcessor(InputInterface $input, PdfParser $pdfParser) |
131
|
|
|
{ |
132
|
|
|
$processors = $pdfParser->getAvailableProcessors(); |
133
|
|
|
$processorId = $input->getArgument('processor'); |
134
|
|
|
if (!$processorId) { |
135
|
|
|
$helper = $this->getHelper('question'); |
136
|
|
|
$question = new ChoiceQuestion('Which processor to use?', $processors); |
137
|
|
|
$processorId = $helper->ask($input, $this->output, $question); |
138
|
|
|
} |
139
|
|
|
$pdfParser->setProcessor($processors[$processorId]); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|