transform gender data for saved exports

This commit is contained in:
Julie Lenaerts 2024-10-22 06:46:39 +02:00
parent 0d2e0b4e91
commit 566c40dd84

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Export\Filter\PersonFilters;
use Chill\MainBundle\Entity\Gender;
use Chill\MainBundle\Export\DataTransformerInterface;
use Chill\MainBundle\Export\ExportElementValidatedInterface;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Repository\GenderRepository;
@ -27,7 +28,8 @@ use Symfony\Contracts\Translation\TranslatorInterface;
class GenderFilter implements
ExportElementValidatedInterface,
FilterInterface
FilterInterface,
DataTransformerInterface
{
// inject gender repository and find the active genders so that you can pass them to the ChoiceType (ordered by ordering)
public function __construct(private readonly TranslatorInterface $translator, private readonly TranslatableStringHelperInterface $translatableStringHelper, private readonly GenderRepository $genderRepository) {}
@ -77,6 +79,26 @@ class GenderFilter implements
]);
}
public function transformData(?array $before): array
{
$transformedData = $before;
if (array_key_exists('accepted_genders', $before)) {
$genderMapping = [
'woman' => 2,
'man' => 1,
'both' => 3,
'unknown' => null,
];
$transformedData['accepted_genders_entity'] = array_map(function ($gender) use ($genderMapping) {
return $genderMapping[$gender] ?? null;
}, $before['accepted_genders']);
}
return $transformedData;
}
public function getFormDefaultData(): array
{
return [];