Skip to content

Commit

Permalink
Merge pull request #1614 from ConductionNL/feature/PC108-3/twig-array…
Browse files Browse the repository at this point in the history
…-values

Added array values function for twig
  • Loading branch information
WilcoLouwerse authored Jan 26, 2024
2 parents af782d3 + ab35dd5 commit b1d3e68
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
6 changes: 2 additions & 4 deletions api/src/Controller/ConvenienceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -50,7 +49,7 @@ public function __construct(
HandlerService $handlerService,
ActionSubscriber $actionSubscriber,
ObjectEntityService $objectEntityService,
Environment $twig
MappingService $mappingService
) {
$this->entityManager = $entityManager;
$this->serializer = $serializer;
Expand All @@ -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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions api/src/Twig/MappingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
];
}
}
33 changes: 33 additions & 0 deletions api/src/Twig/MappingRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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);
}
}

0 comments on commit b1d3e68

Please sign in to comment.