From 68ada5c39e5f7ba8d0493c170437e4a4dac661c3 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Fri, 10 Oct 2025 11:34:03 +0200 Subject: [PATCH 1/2] Add Call function to twig, small fixes on endpoint and call services --- src/Service/CallService.php | 70 +++++++++--------- src/Service/EndpointService.php | 123 +++++++++++++++++--------------- src/Twig/CallExtension.php | 21 ++++++ src/Twig/CallRuntime.php | 40 +++++++++++ 4 files changed, 163 insertions(+), 91 deletions(-) create mode 100644 src/Twig/CallExtension.php create mode 100644 src/Twig/CallRuntime.php diff --git a/src/Service/CallService.php b/src/Service/CallService.php index 2b971cce..0a0fe585 100644 --- a/src/Service/CallService.php +++ b/src/Service/CallService.php @@ -348,7 +348,7 @@ public function call( 'Request failed with error '.$exception, [ 'sourceCall' => $this->sourceCallLogData(['method' => $method, 'url' => $url, 'response' => $response ?? null], $config), - ] + ], ); if (empty($response) === false) { @@ -502,6 +502,8 @@ private function handleEndpointConfigOut(array $config, array $endpointConfigOut $body = $this->mappingService->mapping($mapping, $body); $config[$configKey] = \Safe\json_encode($body); + unset($config['headers']['Content-Type'], $config['headers']['Accept'], $config['headers']['accept'], $config['headers']['content-type']); + $config['headers']['content-type'] = 'application/json'; } catch (Exception | LoaderError | SyntaxError $exception) { $this->callLogger->error("Could not map with mapping {$endpointConfigOut[$configKey]['mapping']} while handling $configKey EndpointConfigOut for a Source. ".$exception->getMessage()); @@ -742,17 +744,17 @@ public function decodeResponse( // This if is statement prevents binary code from being used a string. if (in_array( - $contentType, - [ - 'application/pdf', - 'application/pdf; charset=utf-8', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8', - 'application/msword', - 'image/jpeg', - 'image/png', - ] - ) === false + $contentType, + [ + 'application/pdf', + 'application/pdf; charset=utf-8', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8', + 'application/msword', + 'image/jpeg', + 'image/png', + ] + ) === false ) { $this->callLogger->debug('Response content: '.$responseBody); } @@ -766,28 +768,28 @@ public function decodeResponse( } switch ($contentType) { - case 'text/plain': - return $responseBody; - case 'text/yaml': - case 'text/x-yaml': - case 'text/yaml; charset=utf-8': - return $yamlEncoder->decode($responseBody, 'yaml'); - case 'text/xml': - case 'text/xml; charset=utf-8': - case 'application/pdf': - case 'application/pdf; charset=utf-8': - case 'application/msword': - case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': - case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8': - case 'image/jpeg': - case 'image/png': - $this->callLogger->debug('Response content: binary code..'); - return base64_encode($responseBody); - case 'application/xml': - case 'application/xml; charset=utf-8': - return $xmlEncoder->decode($responseBody, 'xml'); - case 'application/json': - case 'application/json; charset=utf-8': + case 'text/plain': + return $responseBody; + case 'text/yaml': + case 'text/x-yaml': + case 'text/yaml; charset=utf-8': + return $yamlEncoder->decode($responseBody, 'yaml'); + case 'text/xml': + case 'text/xml; charset=utf-8': + case 'application/pdf': + case 'application/pdf; charset=utf-8': + case 'application/msword': + case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': + case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8': + case 'image/jpeg': + case 'image/png': + $this->callLogger->debug('Response content: binary code..'); + return base64_encode($responseBody); + case 'application/xml': + case 'application/xml; charset=utf-8': + return $xmlEncoder->decode($responseBody, 'xml'); + case 'application/json': + case 'application/json; charset=utf-8': default: $result = json_decode($responseBody, true); }//end switch diff --git a/src/Service/EndpointService.php b/src/Service/EndpointService.php index a2906beb..69765f26 100644 --- a/src/Service/EndpointService.php +++ b/src/Service/EndpointService.php @@ -220,39 +220,39 @@ private function determineAcceptType(string $acceptHeader): ?string // Determine the accept type. $this->logger->debug('Determine accept type from accept header'); switch ($acceptHeader) { - case 'application/pdf': - return 'pdf'; - case 'application/json': - return 'json'; - case 'application/json+hal': - case 'application/hal+json': - return 'jsonhal'; - case 'application/json+ld': - case 'application/ld+json': - return 'jsonld'; - case 'application/json+fromio': - case 'application/formio+json': - return 'formio'; - case 'application/json+schema': - case 'application/schema+json': - return 'schema'; - case 'application/json+graphql': - case 'application/graphql+json': - return 'graphql'; - case 'text/xml': - case 'application/xml': - return 'xml'; - case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': - return 'xlsx'; - case 'text/csv': - return 'csv'; - case 'text/html': - return 'html'; - case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': - return 'docx'; + case 'application/pdf': + return 'pdf'; + case 'application/json': + return 'json'; + case 'application/json+hal': + case 'application/hal+json': + return 'jsonhal'; + case 'application/json+ld': + case 'application/ld+json': + return 'jsonld'; + case 'application/json+fromio': + case 'application/formio+json': + return 'formio'; + case 'application/json+schema': + case 'application/schema+json': + return 'schema'; + case 'application/json+graphql': + case 'application/graphql+json': + return 'graphql'; + case 'text/xml': + case 'application/xml': + return 'xml'; + case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': + return 'xlsx'; + case 'text/csv': + return 'csv'; + case 'text/html': + return 'html'; + case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': + return 'docx'; break; - case 'application/json+aggregations': - return 'aggregations'; + case 'application/json+aggregations': + return 'aggregations'; }//end switch return null; @@ -302,8 +302,8 @@ public function getAcceptType(): string if (count($pathparts) >= 2) { $extension = end($pathparts); switch ($extension) { - case 'pdf': - return 'pdf'; + case 'pdf': + return 'pdf'; }//end switch } @@ -334,14 +334,14 @@ public function decodeBody(): ?array // Decode the body. switch ($contentType) { - case 'text/xml': - case 'application/xml': - case 'xml': - $xmlEncoder = new XmlEncoder(['load_options' => LIBXML_PARSEHUGE]); - - return $xmlEncoder->decode($this->request->getContent(), 'xml'); - default: - return json_decode($this->request->getContent(), true); + case 'text/xml': + case 'application/xml': + case 'xml': + $xmlEncoder = new XmlEncoder(['load_options' => LIBXML_PARSEHUGE]); + + return $xmlEncoder->decode($this->request->getContent(), 'xml'); + default: + return json_decode($this->request->getContent(), true); }//end switch }//end decodeBody() @@ -426,10 +426,10 @@ private function getPutData(): array $filename = null; if (preg_match( - '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/', - $headers['content-disposition'], - $matches - ) === false + '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/', + $headers['content-disposition'], + $matches + ) === false ) { preg_match( '/^(.+); *name=([-+.\w]+)(; *filename=([-+.\w]+))?/', @@ -444,14 +444,14 @@ private function getPutData(): array // Handle your fields here. switch ($name) { // This is a file upload. - case 'userfile': - file_put_contents($filename, $body); - break; + case 'userfile': + file_put_contents($filename, $body); + break; // Default for all other files is to populate $data. - default: - $data[$name] = substr($body, 0, (strlen($body) - 2)); - break; + default: + $data[$name] = substr($body, 0, (strlen($body) - 2)); + break; } }//end foreach @@ -551,14 +551,23 @@ private function getNormalPath(array $parameters): array $this->logger->debug('EndpointService->getNormalPath(): '.$exception->getMessage()); // Todo: When an id is not given the last element of the path array should be removed to ensure the arrays are of the same length. - array_pop($path); - $combinedArray = array_combine($path, explode('/', $pathRaw)); + + if(count(explode('/', $pathRaw)) > count($path)) { + $combinedArray = array_combine($path, explode('/', $pathRaw, count($path))); + } else { + array_pop($path); + $combinedArray = array_combine($path, explode('/', $pathRaw)); + } } if ($combinedArray === false) { - // Todo: When an id is not given the last element of the path array should be removed to ensure the arrays are of the same length. - array_pop($path); - $combinedArray = array_combine($path, explode('/', $pathRaw)); + // When an id is not given the last element of the path array should be removed to ensure the arrays are of the same length. + if(count(explode('/', $pathRaw)) > count($path)) { + $combinedArray = array_combine($path, explode('/', $pathRaw, count($path))); + } else { + array_pop($path); + $combinedArray = array_combine($path, explode('/', $pathRaw)); + } } if ($combinedArray === false) { diff --git a/src/Twig/CallExtension.php b/src/Twig/CallExtension.php new file mode 100644 index 00000000..5c84cb42 --- /dev/null +++ b/src/Twig/CallExtension.php @@ -0,0 +1,21 @@ +resourceService->getSource($sourceId, 'common-gateway/zgw-to-zds-bundle'); + + $response = $this->callService->call($source, $endpoint, $method, $configuration); + return $this->callService->decodeResponse($source, $response); + + }//end call() + + +}//end class From 63309db5b6b257ac035d5cfacde8073fa99170c9 Mon Sep 17 00:00:00 2001 From: GitHub Actions <> Date: Fri, 10 Oct 2025 09:34:57 +0000 Subject: [PATCH 2/2] Update src from PHP Codesniffer --- src/Service/CallService.php | 66 +++++++++---------- src/Service/EndpointService.php | 109 ++++++++++++++++---------------- src/Twig/CallExtension.php | 4 -- src/Twig/CallRuntime.php | 7 +- 4 files changed, 88 insertions(+), 98 deletions(-) diff --git a/src/Service/CallService.php b/src/Service/CallService.php index 0a0fe585..ceaf1e3d 100644 --- a/src/Service/CallService.php +++ b/src/Service/CallService.php @@ -744,17 +744,17 @@ public function decodeResponse( // This if is statement prevents binary code from being used a string. if (in_array( - $contentType, - [ - 'application/pdf', - 'application/pdf; charset=utf-8', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8', - 'application/msword', - 'image/jpeg', - 'image/png', - ] - ) === false + $contentType, + [ + 'application/pdf', + 'application/pdf; charset=utf-8', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8', + 'application/msword', + 'image/jpeg', + 'image/png', + ] + ) === false ) { $this->callLogger->debug('Response content: '.$responseBody); } @@ -768,28 +768,28 @@ public function decodeResponse( } switch ($contentType) { - case 'text/plain': - return $responseBody; - case 'text/yaml': - case 'text/x-yaml': - case 'text/yaml; charset=utf-8': - return $yamlEncoder->decode($responseBody, 'yaml'); - case 'text/xml': - case 'text/xml; charset=utf-8': - case 'application/pdf': - case 'application/pdf; charset=utf-8': - case 'application/msword': - case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': - case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8': - case 'image/jpeg': - case 'image/png': - $this->callLogger->debug('Response content: binary code..'); - return base64_encode($responseBody); - case 'application/xml': - case 'application/xml; charset=utf-8': - return $xmlEncoder->decode($responseBody, 'xml'); - case 'application/json': - case 'application/json; charset=utf-8': + case 'text/plain': + return $responseBody; + case 'text/yaml': + case 'text/x-yaml': + case 'text/yaml; charset=utf-8': + return $yamlEncoder->decode($responseBody, 'yaml'); + case 'text/xml': + case 'text/xml; charset=utf-8': + case 'application/pdf': + case 'application/pdf; charset=utf-8': + case 'application/msword': + case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': + case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8': + case 'image/jpeg': + case 'image/png': + $this->callLogger->debug('Response content: binary code..'); + return base64_encode($responseBody); + case 'application/xml': + case 'application/xml; charset=utf-8': + return $xmlEncoder->decode($responseBody, 'xml'); + case 'application/json': + case 'application/json; charset=utf-8': default: $result = json_decode($responseBody, true); }//end switch diff --git a/src/Service/EndpointService.php b/src/Service/EndpointService.php index 69765f26..80465efe 100644 --- a/src/Service/EndpointService.php +++ b/src/Service/EndpointService.php @@ -220,39 +220,39 @@ private function determineAcceptType(string $acceptHeader): ?string // Determine the accept type. $this->logger->debug('Determine accept type from accept header'); switch ($acceptHeader) { - case 'application/pdf': - return 'pdf'; - case 'application/json': - return 'json'; - case 'application/json+hal': - case 'application/hal+json': - return 'jsonhal'; - case 'application/json+ld': - case 'application/ld+json': - return 'jsonld'; - case 'application/json+fromio': - case 'application/formio+json': - return 'formio'; - case 'application/json+schema': - case 'application/schema+json': - return 'schema'; - case 'application/json+graphql': - case 'application/graphql+json': - return 'graphql'; - case 'text/xml': - case 'application/xml': - return 'xml'; - case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': - return 'xlsx'; - case 'text/csv': - return 'csv'; - case 'text/html': - return 'html'; - case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': - return 'docx'; + case 'application/pdf': + return 'pdf'; + case 'application/json': + return 'json'; + case 'application/json+hal': + case 'application/hal+json': + return 'jsonhal'; + case 'application/json+ld': + case 'application/ld+json': + return 'jsonld'; + case 'application/json+fromio': + case 'application/formio+json': + return 'formio'; + case 'application/json+schema': + case 'application/schema+json': + return 'schema'; + case 'application/json+graphql': + case 'application/graphql+json': + return 'graphql'; + case 'text/xml': + case 'application/xml': + return 'xml'; + case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': + return 'xlsx'; + case 'text/csv': + return 'csv'; + case 'text/html': + return 'html'; + case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': + return 'docx'; break; - case 'application/json+aggregations': - return 'aggregations'; + case 'application/json+aggregations': + return 'aggregations'; }//end switch return null; @@ -302,8 +302,8 @@ public function getAcceptType(): string if (count($pathparts) >= 2) { $extension = end($pathparts); switch ($extension) { - case 'pdf': - return 'pdf'; + case 'pdf': + return 'pdf'; }//end switch } @@ -334,14 +334,14 @@ public function decodeBody(): ?array // Decode the body. switch ($contentType) { - case 'text/xml': - case 'application/xml': - case 'xml': - $xmlEncoder = new XmlEncoder(['load_options' => LIBXML_PARSEHUGE]); - - return $xmlEncoder->decode($this->request->getContent(), 'xml'); - default: - return json_decode($this->request->getContent(), true); + case 'text/xml': + case 'application/xml': + case 'xml': + $xmlEncoder = new XmlEncoder(['load_options' => LIBXML_PARSEHUGE]); + + return $xmlEncoder->decode($this->request->getContent(), 'xml'); + default: + return json_decode($this->request->getContent(), true); }//end switch }//end decodeBody() @@ -426,10 +426,10 @@ private function getPutData(): array $filename = null; if (preg_match( - '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/', - $headers['content-disposition'], - $matches - ) === false + '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/', + $headers['content-disposition'], + $matches + ) === false ) { preg_match( '/^(.+); *name=([-+.\w]+)(; *filename=([-+.\w]+))?/', @@ -444,14 +444,14 @@ private function getPutData(): array // Handle your fields here. switch ($name) { // This is a file upload. - case 'userfile': - file_put_contents($filename, $body); - break; + case 'userfile': + file_put_contents($filename, $body); + break; // Default for all other files is to populate $data. - default: - $data[$name] = substr($body, 0, (strlen($body) - 2)); - break; + default: + $data[$name] = substr($body, 0, (strlen($body) - 2)); + break; } }//end foreach @@ -551,8 +551,7 @@ private function getNormalPath(array $parameters): array $this->logger->debug('EndpointService->getNormalPath(): '.$exception->getMessage()); // Todo: When an id is not given the last element of the path array should be removed to ensure the arrays are of the same length. - - if(count(explode('/', $pathRaw)) > count($path)) { + if (count(explode('/', $pathRaw)) > count($path)) { $combinedArray = array_combine($path, explode('/', $pathRaw, count($path))); } else { array_pop($path); @@ -562,7 +561,7 @@ private function getNormalPath(array $parameters): array if ($combinedArray === false) { // When an id is not given the last element of the path array should be removed to ensure the arrays are of the same length. - if(count(explode('/', $pathRaw)) > count($path)) { + if (count(explode('/', $pathRaw)) > count($path)) { $combinedArray = array_combine($path, explode('/', $pathRaw, count($path))); } else { array_pop($path); diff --git a/src/Twig/CallExtension.php b/src/Twig/CallExtension.php index 5c84cb42..3bfdc0e8 100644 --- a/src/Twig/CallExtension.php +++ b/src/Twig/CallExtension.php @@ -7,8 +7,6 @@ class CallExtension extends AbstractExtension { - - public function getFunctions() { return [ @@ -16,6 +14,4 @@ public function getFunctions() ]; }//end getFunctions() - - }//end class diff --git a/src/Twig/CallRuntime.php b/src/Twig/CallRuntime.php index dceaa760..bfaa501a 100644 --- a/src/Twig/CallRuntime.php +++ b/src/Twig/CallRuntime.php @@ -10,8 +10,6 @@ class CallRuntime implements RuntimeExtensionInterface { - - public function __construct( private readonly CallService $callService, private readonly GatewayResourceService $resourceService @@ -19,7 +17,6 @@ public function __construct( }//end __construct() - /** * Call source of given id or reference * @@ -27,7 +24,7 @@ public function __construct( * * @return array The dot aray. */ - public function call(string $sourceId, string $endpoint, string $method='GET', array $configuration=[]): array + public function call(string $sourceId, string $endpoint, string $method = 'GET', array $configuration = []): array { $source = $this->resourceService->getSource($sourceId, 'common-gateway/zgw-to-zds-bundle'); @@ -35,6 +32,4 @@ public function call(string $sourceId, string $endpoint, string $method='GET', a return $this->callService->decodeResponse($source, $response); }//end call() - - }//end class