diff --git a/lib/Service/MappingService.php b/lib/Service/MappingService.php index 3ea528531..843dac74d 100644 --- a/lib/Service/MappingService.php +++ b/lib/Service/MappingService.php @@ -80,6 +80,20 @@ public function encodeArrayKeys(array $array, string $toReplace, string $replace }//end encodeArrayKeys() + /** + * Renders a Twig template string using the mapping Twig environment. + * + * @param string $template The Twig template to render. + * @param array $context The context available inside the template. + * + * @return string The rendered template result. + * @throws LoaderError|SyntaxError Twig exceptions + */ + public function renderTemplateString(string $template, array $context = []): string + { + return html_entity_decode($this->twig->createTemplate($template)->render($context)); + } + /** * Maps (transforms) an array (input) to a different array (output). * @@ -150,7 +164,7 @@ public function executeMapping(Mapping $mapping, array $input, bool $list = fals } try { - $dotArray->set($key, html_entity_decode($this->twig->createTemplate($value)->render($originalInput))); + $dotArray->set($key, $this->renderTemplateString($value, $originalInput)); } catch (Throwable $e) { throw new Exception("Error for mapping: {$mapping->getName()}, key: $key, value: $value and with message thrown: {$e->getMessage()}"); } diff --git a/lib/Service/SynchronizationService.php b/lib/Service/SynchronizationService.php index 26980b4cf..233da3899 100644 --- a/lib/Service/SynchronizationService.php +++ b/lib/Service/SynchronizationService.php @@ -67,6 +67,7 @@ class SynchronizationService const EXTRA_DATA_CONFIGS_LOCATION = 'extraDataConfigs'; const EXTRA_DATA_DYNAMIC_ENDPOINT_LOCATION = 'dynamicEndpointLocation'; const EXTRA_DATA_STATIC_ENDPOINT_LOCATION = 'staticEndpoint'; + const EXTRA_DATA_ENDPOINT_TEMPLATE_LOCATION = 'endpointTemplate'; const KEY_FOR_EXTRA_DATA_LOCATION = 'keyToSetExtraData'; const MERGE_EXTRA_DATA_OBJECT_LOCATION = 'mergeExtraData'; const UNSET_CONFIG_KEY_LOCATION = 'unsetConfigKey'; @@ -1003,6 +1004,21 @@ private function fetchExtraDataForObject( } } + if (isset($extraDataConfig[$this::EXTRA_DATA_ENDPOINT_TEMPLATE_LOCATION]) === true + && is_string($extraDataConfig[$this::EXTRA_DATA_ENDPOINT_TEMPLATE_LOCATION]) === true + && $extraDataConfig[$this::EXTRA_DATA_ENDPOINT_TEMPLATE_LOCATION] !== '' + ) { + $endpoint = $this->mappingService->renderTemplateString( + template: $extraDataConfig[$this::EXTRA_DATA_ENDPOINT_TEMPLATE_LOCATION], + context: [ + 'endpoint' => $endpoint ?? null, + 'object' => $object, + 'originId' => $originId, + 'extraDataConfig' => $extraDataConfig, + ] + ); + } + if (!$endpoint) { throw new Exception( sprintf(