mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
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:
parent
2c91d2e10f
commit
4fc433cd63
@ -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;
|
||||
}
|
||||
}
|
@ -92,7 +92,7 @@ class CountAccompanyingCourse implements ExportInterface, GroupedExportInterface
|
||||
return ['export_result'];
|
||||
}
|
||||
|
||||
public function getResult($query, $data, \Chill\MainBundle\Export\ExportGenerationContext $context)
|
||||
public function getResult($query, $data, ExportGenerationContext $context)
|
||||
{
|
||||
return $query->getQuery()->getResult(Query::HYDRATE_SCALAR);
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters;
|
||||
|
||||
use Chill\MainBundle\Export\ExportDataNormalizerTrait;
|
||||
use Chill\MainBundle\Export\FilterInterface;
|
||||
use Chill\MainBundle\Form\Type\PickRollingDateType;
|
||||
use Chill\MainBundle\Service\RollingDate\RollingDate;
|
||||
@ -25,6 +26,8 @@ use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class StepFilterOnDate implements FilterInterface
|
||||
{
|
||||
use ExportDataNormalizerTrait;
|
||||
|
||||
private const A = 'acp_filter_bystep_stephistories';
|
||||
|
||||
private const DEFAULT_CHOICE = [
|
||||
@ -94,12 +97,18 @@ class StepFilterOnDate implements FilterInterface
|
||||
|
||||
public function normalizeFormData(array $formData): array
|
||||
{
|
||||
// TODO: Implement normalizeFormData() method.
|
||||
return [
|
||||
'accepted_steps_multi' => $formData['accepted_steps_multi'],
|
||||
'calc_date' => $formData['calc_date']->normalize(),
|
||||
];
|
||||
}
|
||||
|
||||
public function denormalizeFormData(array $formData, int $fromVersion): array
|
||||
{
|
||||
// TODO: Implement denormalizeFormData() method.
|
||||
return [
|
||||
'accepted_steps_multi' => $formData['accepted_steps_multi'],
|
||||
'calc_date' => RollingDate::fromNormalized($formData['calc_date']),
|
||||
];
|
||||
}
|
||||
|
||||
public function getVersion(): int
|
||||
|
@ -22,6 +22,7 @@ use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\UserHistory;
|
||||
use Chill\PersonBundle\Export\Declarations;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
@ -125,6 +126,29 @@ final readonly class UserJobFilter implements FilterInterface, DataTransformerIn
|
||||
];
|
||||
}
|
||||
|
||||
public function normalizeFormData(array $formData): array
|
||||
{
|
||||
return [
|
||||
'jobs' => array_map(static fn (UserJob $job) => $job->getId(), (array) $formData['jobs']),
|
||||
'start_date' => $formData['start_date']->normalize(),
|
||||
'end_date' => $formData['end_date']->normalize(),
|
||||
];
|
||||
}
|
||||
|
||||
public function denormalizeFormData(array $formData, int $fromVersion): array
|
||||
{
|
||||
return [
|
||||
'jobs' => new ArrayCollection(array_filter(array_map(static fn (int $id) => $this->userJobRepository->find($id), $formData['jobs']), fn (?UserJob $job) => null !== $job)),
|
||||
'start_date' => RollingDate::fromNormalized($formData['start_date']),
|
||||
'end_date' => RollingDate::fromNormalized($formData['end_date']),
|
||||
];
|
||||
}
|
||||
|
||||
public function getVersion(): int
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
public function transformData(?array $before): array
|
||||
{
|
||||
$default = $this->getFormDefaultData();
|
||||
|
@ -22,7 +22,7 @@ use Doctrine\ORM\EntityManagerInterface;
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
final class StepFilterTest extends AbstractFilterTest
|
||||
final class StepFilterOnDateTest extends AbstractFilterTest
|
||||
{
|
||||
private StepFilterOnDate $filter;
|
||||
|
Loading…
x
Reference in New Issue
Block a user