From dc8138d5e79ea1de08f0db5cfc1653b7283952d3 Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Fri, 26 Jan 2024 16:24:28 +0100 Subject: [PATCH 1/2] Added array values function for twig --- api/src/Twig/MappingExtension.php | 1 + api/src/Twig/MappingRuntime.php | 33 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/api/src/Twig/MappingExtension.php b/api/src/Twig/MappingExtension.php index d2df0d88d..6308c6c99 100644 --- a/api/src/Twig/MappingExtension.php +++ b/api/src/Twig/MappingExtension.php @@ -12,6 +12,7 @@ public function getFunctions() return [ new TwigFunction('map', [MappingRuntime::class, 'map']), new TwigFunction('dotToObject', [MappingRuntime::class, 'dotToArray']), + new TwigFunction('arrayValues', [MappingRuntime::class, 'arrayValues']), ]; } } diff --git a/api/src/Twig/MappingRuntime.php b/api/src/Twig/MappingRuntime.php index 850832aa2..87b385b24 100644 --- a/api/src/Twig/MappingRuntime.php +++ b/api/src/Twig/MappingRuntime.php @@ -18,6 +18,20 @@ public function __construct(MappingService $mappingService, EntityManagerInterfa $this->mappingService = $mappingService; } + /** + * Uses CoreBundle MappingService to map data. + * If $list is set to true you could use the key 'listInput' in the $data array to pass along the list of items to map. + * This makes it possible to also pass along other key+value pairs to use in mapping besides just one array of items to map. + * + * @param string $mappingString The reference of a Mapping object. + * @param array $data The data to map. Or one list of items to map. + * @param bool $list False by default, if set to true mapping will be done for each item in the $data array. + * + * @return array The mapped result. + * + * @throws \Twig\Error\LoaderError + * @throws \Twig\Error\SyntaxError + */ public function map(string $mappingString, array $data, bool $list = false): array { $mapping = $this->entityManager->getRepository('App:Mapping')->findOneBy(['reference' => $mappingString]); @@ -27,10 +41,29 @@ public function map(string $mappingString, array $data, bool $list = false): arr return $value; } + /** + * Turns given array into an Adbar\Dot (dot notation). + * + * @param array $array The array to turn into a dot array. + * + * @return array The dot aray. + */ public function dotToArray(array $array): array { $dotArray = new Dot($array, true); return $dotArray->all(); } + + /** + * Makes it possible to use the php function array_values in twig. + * + * @param array $array The array to use array_values on. + * + * @return array The updated array. + */ + public function arrayValues(array $array): array + { + return array_values($array); + } } From ab35dd51757b9eefa641c6a6ce04a91a96215561 Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Fri, 26 Jan 2024 16:59:03 +0100 Subject: [PATCH 2/2] Fix test endpoint for mapping --- api/src/Controller/ConvenienceController.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/api/src/Controller/ConvenienceController.php b/api/src/Controller/ConvenienceController.php index 32f7f46c6..f4d642310 100755 --- a/api/src/Controller/ConvenienceController.php +++ b/api/src/Controller/ConvenienceController.php @@ -40,7 +40,6 @@ class ConvenienceController extends AbstractController private ActionSubscriber $actionSubscriber; private ObjectEntityService $objectEntityService; private MappingService $mappingService; - private Environment $twig; public function __construct( EntityManagerInterface $entityManager, @@ -50,7 +49,7 @@ public function __construct( HandlerService $handlerService, ActionSubscriber $actionSubscriber, ObjectEntityService $objectEntityService, - Environment $twig + MappingService $mappingService ) { $this->entityManager = $entityManager; $this->serializer = $serializer; @@ -60,8 +59,7 @@ public function __construct( $this->handlerService = $handlerService; $this->actionSubscriber = $actionSubscriber; $this->objectEntityService = $objectEntityService; - $this->twig = $twig; - $this->mappingService = new MappingService($twig); + $this->mappingService = $mappingService; } /**