From 5489178e4b9c70d3de9e49489a6c352977029818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 7 Nov 2022 14:06:47 +0100 Subject: [PATCH] DX: Create a RollingDate data transfer object and Form type --- .../Form/DataMapper/RollingDateDataMapper.php | 45 ++++++++ .../Form/Type/PickRollingDateType.php | 50 +++++++++ .../Service/RollingDate/RollingDate.php | 101 ++++++++++++++++++ .../Form/Type/PickRollingDateTypeTest.php | 53 +++++++++ .../translations/messages.fr.yml | 19 ++++ 5 files changed, 268 insertions(+) create mode 100644 src/Bundle/ChillMainBundle/Form/DataMapper/RollingDateDataMapper.php create mode 100644 src/Bundle/ChillMainBundle/Form/Type/PickRollingDateType.php create mode 100644 src/Bundle/ChillMainBundle/Service/RollingDate/RollingDate.php create mode 100644 src/Bundle/ChillMainBundle/Tests/Form/Type/PickRollingDateTypeTest.php diff --git a/src/Bundle/ChillMainBundle/Form/DataMapper/RollingDateDataMapper.php b/src/Bundle/ChillMainBundle/Form/DataMapper/RollingDateDataMapper.php new file mode 100644 index 000000000..21d0c3fde --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/DataMapper/RollingDateDataMapper.php @@ -0,0 +1,45 @@ +setData($viewData->getRoll()); + $forms['fixedDate']->setData($viewData->getFixedDate()); + } + + public function mapFormsToData($forms, &$viewData): void + { + $forms = iterator_to_array($forms); + + $viewData = new RollingDate( + $forms['roll']->getData(), + $forms['fixedDate']->getData() + ); + } +} diff --git a/src/Bundle/ChillMainBundle/Form/Type/PickRollingDateType.php b/src/Bundle/ChillMainBundle/Form/Type/PickRollingDateType.php new file mode 100644 index 000000000..a54dc778b --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/Type/PickRollingDateType.php @@ -0,0 +1,50 @@ +add('roll', ChoiceType::class, [ + 'choices' => array_combine( + array_map(static fn (string $item) => 'rolling_date.' . $item, RollingDate::ALL_T), + RollingDate::ALL_T + ), + 'multiple' => false, + 'expanded' => false, + 'label' => 'rolling_date.roll_movement', + ]) + ->add('fixedDate', ChillDateType::class, [ + 'input' => 'datetime_immutable', + 'label' => 'rolling_date.fixed_date_date', + ]); + + $builder->setDataMapper(new RollingDateDataMapper()); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'class' => RollingDate::class, + 'empty_data' => new RollingDate(RollingDate::T_TODAY), + ]); + } +} diff --git a/src/Bundle/ChillMainBundle/Service/RollingDate/RollingDate.php b/src/Bundle/ChillMainBundle/Service/RollingDate/RollingDate.php new file mode 100644 index 000000000..cc00c7777 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Service/RollingDate/RollingDate.php @@ -0,0 +1,101 @@ +roll = $roll; + $this->pivotDate = $pivotDate ?? new DateTimeImmutable('now'); + $this->fixedDate = $fixedDate; + } + + public function getFixedDate(): DateTimeImmutable + { + return $this->fixedDate; + } + + public function getPivotDate(): ?DateTimeImmutable + { + return $this->pivotDate; + } + + public function getRoll(): string + { + return $this->roll; + } +} diff --git a/src/Bundle/ChillMainBundle/Tests/Form/Type/PickRollingDateTypeTest.php b/src/Bundle/ChillMainBundle/Tests/Form/Type/PickRollingDateTypeTest.php new file mode 100644 index 000000000..771dd0310 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Form/Type/PickRollingDateTypeTest.php @@ -0,0 +1,53 @@ + 'year_previous_start', + 'fixedDate' => null, + ]; + + $form = $this->factory->create(PickRollingDateType::class); + + $form->submit($formData); + + $this->assertTrue($form->isSynchronized()); + + /** @var RollingDate $rollingDate */ + $rollingDate = $form->getData(); + + $this->assertInstanceOf(RollingDate::class, $rollingDate); + $this->assertEquals(RollingDate::T_YEAR_PREVIOUS_START, $rollingDate->getRoll()); + } + + protected function getExtensions(): array + { + $type = new PickRollingDateType(); + + return [ + new PreloadedExtension([$type], []), + ]; + } +} diff --git a/src/Bundle/ChillMainBundle/translations/messages.fr.yml b/src/Bundle/ChillMainBundle/translations/messages.fr.yml index 3e229b8d4..c532ba30f 100644 --- a/src/Bundle/ChillMainBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillMainBundle/translations/messages.fr.yml @@ -538,3 +538,22 @@ export: isNoAddress: Adresse incomplète ? _lat: Latitude _lon: Longitude + +rolling_date: + year_previous_start: Début de l'année précédente + quarter_previous_start: Début du trimestre précédent + month_previous_start: Début du mois précédent + week_previous_start: Début de la semaine précédente + year_current_start: Début de l'année courante + quarter_current_start: Début du trimestre courant + month_current_start: Début du mois courant + week_current_start: Début de la semaine courante + today: Aujourd'hui (aucune modification de la date courante) + year_next_start: Début de l'année suivante + quarter_next_start: Début du trimestre suivante + month_next_start: Début du mois suivant + week_next_start: Début de la semaine suivante + fixed_date: Date fixe + roll_movement: Modification par rapport à aujourd'hui + fixed_date_date: Date fixe +