exports: fix buildForm in SocialWorkType Filter

This commit is contained in:
Mathieu Jaumotte 2022-09-08 15:00:37 +02:00
parent 9b1e464011
commit 4cb6e8e564

View File

@ -50,72 +50,42 @@ class SocialWorkTypeFilter implements FilterInterface
$builder
->add('actionType', HiddenType::class)
->get('actionType')
->addModelTransformer(new CallbackTransformer(
static function (?iterable $actionsAsIterable): string {
if (null === $actionsAsIterable) { return ''; }
$actionIds = [];
foreach ($actionsAsIterable as $value) {
$actionIds[] = $value->getId();
}
return implode(',', $actionIds);
},
function (?string $actionsAsString): array {
if (null === $actionsAsString) { return []; }
return array_map(
fn (string $id): ?SocialAction
=> $this->socialActionRepository->findOneBy(['id' => (int) $id]),
explode(',', $actionsAsString)
);
}
))
->addModelTransformer($this->iterableToIdTransformer(SocialAction::class))
;
$builder
->add('goal', HiddenType::class)
->get('goal')
->addModelTransformer(new CallbackTransformer(
static function (?iterable $goalsAsIterable): string {
if (null === $goalsAsIterable) { return ''; }
$goalIds = [];
foreach ($goalsAsIterable as $value) {
$goalIds[] = $value->getId();
}
return implode(',', $goalIds);
},
function (?string $goalsAsString): array {
if (null === $goalsAsString) { return []; }
return array_map(
fn (string $id): ?Goal
=> $this->socialActionRepository->findOneBy(['id' => (int) $id]),
explode(',', $goalsAsString)
);
}
))
->addModelTransformer($this->iterableToIdTransformer(Goal::class))
;
$builder
->add('result', HiddenType::class)
->get('result')
->addModelTransformer(new CallbackTransformer(
static function (?iterable $resultsAsIterable): string {
if (null === $resultsAsIterable) { return ''; }
$resultIds = [];
foreach ($resultsAsIterable as $value) {
$resultIds[] = $value->getId();
}
return implode(',', $resultIds);
},
function (?string $resultsAsString): array {
if (null === $resultsAsString) { return []; }
return array_map(
fn (string $id): ?Result
=> $this->socialActionRepository->findOneBy(['id' => (int) $id]),
explode(',', $resultsAsString)
);
}
))
->addModelTransformer($this->iterableToIdTransformer(Result::class))
;
}
private function iterableToIdTransformer(string $entity): CallbackTransformer
{
return new CallbackTransformer(
static function (?iterable $asIterable): string {
if (null === $asIterable) { return ''; }
$ids = [];
foreach ($asIterable as $value) {
$ids[] = $value->getId();
}
return implode(',', $ids);
},
function (?string $asString) use ($entity): array {
if (null === $asString) { return []; }
return array_map(
fn (string $id)
=> $this->em
->getRepository($entity)
->findOneBy(['id' => (int) $id]),
explode(',', $asString)
);
}
);
}
public function getTitle(): string