diff --git a/src/Bundle/ChillMainBundle/Export/ExportDataNormalizerTrait.php b/src/Bundle/ChillMainBundle/Export/ExportDataNormalizerTrait.php new file mode 100644 index 000000000..0e24ce375 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Export/ExportDataNormalizerTrait.php @@ -0,0 +1,42 @@ + $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; + } +} diff --git a/src/Bundle/ChillPersonBundle/Export/Export/CountAccompanyingCourse.php b/src/Bundle/ChillPersonBundle/Export/Export/CountAccompanyingCourse.php index 42f2c0f90..6c75e9726 100644 --- a/src/Bundle/ChillPersonBundle/Export/Export/CountAccompanyingCourse.php +++ b/src/Bundle/ChillPersonBundle/Export/Export/CountAccompanyingCourse.php @@ -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); } diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/StepFilterOnDate.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/StepFilterOnDate.php index 332c4f7ed..7f6a11e99 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/StepFilterOnDate.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/StepFilterOnDate.php @@ -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 diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/UserJobFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/UserJobFilter.php index c328e9b21..6477aaa66 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/UserJobFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/UserJobFilter.php @@ -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(); diff --git a/src/Bundle/ChillPersonBundle/Tests/Export/Filter/AccompanyingCourseFilters/StepFilterTest.php b/src/Bundle/ChillPersonBundle/Tests/Export/Filter/AccompanyingCourseFilters/StepFilterOnDateTest.php similarity index 96% rename from src/Bundle/ChillPersonBundle/Tests/Export/Filter/AccompanyingCourseFilters/StepFilterTest.php rename to src/Bundle/ChillPersonBundle/Tests/Export/Filter/AccompanyingCourseFilters/StepFilterOnDateTest.php index c0c2ec249..ad12a48ef 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Export/Filter/AccompanyingCourseFilters/StepFilterTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Export/Filter/AccompanyingCourseFilters/StepFilterOnDateTest.php @@ -22,7 +22,7 @@ use Doctrine\ORM\EntityManagerInterface; * * @coversNothing */ -final class StepFilterTest extends AbstractFilterTest +final class StepFilterOnDateTest extends AbstractFilterTest { private StepFilterOnDate $filter;