feat: refactoring of xml parsers
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\BusProNet\XmlParser;
|
||||
|
||||
use App\BusProNet\Model\File;
|
||||
use PhpZip\Exception\ZipException;
|
||||
use PhpZip\ZipFile;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
|
||||
class DocumentsParser
|
||||
{
|
||||
public function parseConfirmation(Crawler $result): ?File
|
||||
{
|
||||
$pdfData = $this->decode($result->filterXPath('//bestaetigungpdf'));
|
||||
|
||||
return new File('bestaetigung.pdf', $pdfData, 'application/pdf');
|
||||
}
|
||||
|
||||
public function parseDocuments(Crawler $result): ?File
|
||||
{
|
||||
$documents = $result->filterXPath('//reisedokumente/reisedokument');
|
||||
|
||||
if (1 === $documents->count()) {
|
||||
$node = $documents->first();
|
||||
[$filename, $pdfData] = $this->getFileInfo($node);
|
||||
|
||||
return new File($filename.'.pdf', $pdfData, 'application/pdf');
|
||||
}
|
||||
|
||||
// In case of multiple documents create ZIP file
|
||||
try {
|
||||
$zip = new ZipFile();
|
||||
|
||||
$documents->each(function (Crawler $node) use (&$zip) {
|
||||
[$filename, $pdfData] = $this->getFileInfo($node);
|
||||
$zip->addFromString($filename.'.pdf', $pdfData);
|
||||
});
|
||||
|
||||
return new File('reisedokumente.zip', $zip->outputAsString(), 'application/zip');
|
||||
}
|
||||
catch (ZipException $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function decode(Crawler $node): string
|
||||
{
|
||||
[, $pdfData] = explode(',', $node->text());
|
||||
|
||||
return base64_decode($pdfData);
|
||||
}
|
||||
|
||||
private function getFileInfo(Crawler $node): array
|
||||
{
|
||||
$pdfData = $this->decode($node->filterXPath('//pdf'));
|
||||
$filename = $node->filterXPath('//datei')->text();
|
||||
|
||||
return [$filename, $pdfData];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user