From e3c1953e3d1d4dbbb7d244a60d3209cd21fac5ec Mon Sep 17 00:00:00 2001 From: HorstOeko Date: Thu, 12 Oct 2023 06:06:09 +0200 Subject: [PATCH] #25 Centralisation of profile recognition -> Using ZugferdProfileResolver in ZugferdDocumentPdfMerger and ZugferdDocumentReader --- src/ZugferdDocumentPdfMerger.php | 20 ++------------------ src/ZugferdDocumentReader.php | 19 ++++--------------- 2 files changed, 6 insertions(+), 33 deletions(-) diff --git a/src/ZugferdDocumentPdfMerger.php b/src/ZugferdDocumentPdfMerger.php index 844321a..16a37b5 100644 --- a/src/ZugferdDocumentPdfMerger.php +++ b/src/ZugferdDocumentPdfMerger.php @@ -10,9 +10,8 @@ namespace horstoeko\zugferd; use Exception; -use SimpleXMLElement; -use horstoeko\zugferd\ZugferdProfiles; use horstoeko\zugferd\ZugferdDocumentPdfBuilderAbstract; +use horstoeko\zugferd\ZugferdProfileResolver; /** * Class representing the facillity adding existing XML data (file or data-string) @@ -121,21 +120,6 @@ private function xmlDataIsFile(): bool */ private function getProfileDefinition(): array { - $xmlContent = $this->getXmlContent(); - - $xmldocument = new SimpleXMLElement($xmlContent); - $typeelement = $xmldocument->xpath('/rsm:CrossIndustryInvoice/rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID'); - - if (!is_array($typeelement) || !isset($typeelement[0])) { - throw new Exception('Coult not determaine the profile...'); - } - - foreach (ZugferdProfiles::PROFILEDEF as $profile => $profiledef) { - if ($typeelement[0] == $profiledef["contextparameter"]) { - return $profiledef; - } - } - - throw new Exception('Could not determine the profile...'); + return ZugferdProfileResolver::resolveProfileDef($this->getXmlContent()); } } diff --git a/src/ZugferdDocumentReader.php b/src/ZugferdDocumentReader.php index bf3c085..6e9b7b1 100644 --- a/src/ZugferdDocumentReader.php +++ b/src/ZugferdDocumentReader.php @@ -12,10 +12,10 @@ use \Closure; use \DateTime; use \Exception; -use \SimpleXMLElement; use \horstoeko\stringmanagement\FileUtils; -use \horstoeko\stringmanagement\StringUtils; use \horstoeko\stringmanagement\PathUtils; +use \horstoeko\stringmanagement\StringUtils; +use \horstoeko\zugferd\ZugferdProfileResolver; /** * Class representing the document reader for incoming XML-Documents with @@ -242,20 +242,9 @@ public static function readAndGuessFromFile(string $xmlfilename): ZugferdDocumen */ public static function readAndGuessFromContent(string $xmlcontent): ZugferdDocumentReader { - $xmldocument = new SimpleXMLElement($xmlcontent); - $typeelement = $xmldocument->xpath('/rsm:CrossIndustryInvoice/rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID'); - - if (!is_array($typeelement) || !isset($typeelement[0])) { - throw new Exception('Coult not determaine the profile...'); - } - - foreach (ZugferdProfiles::PROFILEDEF as $profile => $profiledef) { - if ($typeelement[0] == $profiledef["contextparameter"]) { - return (new self($profile))->readContent($xmlcontent); - } - } + $profileId = ZugferdProfileResolver::resolveProfileId($xmlcontent); - throw new Exception('Could not determine the profile...'); + return (new self($profileId))->readContent($xmlcontent); } /**