Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added array values function for twig #1614

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading