Rename and implement form data normalization for filters

The `StepFilterTest` was renamed to `StepFilterOnDateTest` for improved clarity. Added `normalizeFormData` and `denormalizeFormData` methods to `UserJobFilter` and `StepFilterOnDate` for handling form data conversions. Also introduced `ExportDataNormalizerTrait` to enhance consistency in data normalization processes.
This commit is contained in:
2025-03-10 21:08:32 +01:00
parent 2c91d2e10f
commit 4fc433cd63
5 changed files with 79 additions and 4 deletions

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Export;
use Doctrine\Persistence\ObjectRepository;
trait ExportDataNormalizerTrait
{
/**
* @param object|list<object> $entity
*/
public function normalizeDoctrineEntity(object|array $entity): array|int
{
if (is_array($entity)) {
return array_values(array_map(static fn (object $entity) => $entity->getId(), $entity));
}
return $entity->getId();
}
public function denormalizeDoctrineEntity(array|int $id, ObjectRepository $repository): object|array
{
if (is_array($id)) {
return $repository->findBy(['id' => $id]);
}
if (null === $object = $repository->find($id)) {
throw new \UnexpectedValueException(sprintf('Object with id "%s" does not exist.', $id));
}
return $object;
}
}