diff --git a/src/Application/Import/CMCSVMouvementsImportHandler.php b/src/Application/Import/CMCSVMouvementsImportHandler.php index 73c8860..d305625 100644 --- a/src/Application/Import/CMCSVMouvementsImportHandler.php +++ b/src/Application/Import/CMCSVMouvementsImportHandler.php @@ -10,6 +10,8 @@ use App\Domain\Mouvement\Mouvement; use App\Domain\Mouvement\MouvementCollection; use App\Domain\Mouvement\MouvementId; +use App\Domain\Temps\Date; +use App\Domain\Temps\DateInvalideException; /** * Implémente un handler CSV d'import de mouvements de la banque Crédit Mutuel. @@ -71,8 +73,12 @@ protected function lireMouvements(\SplFileObject $file): MouvementCollection // Si on a dépassé la ligne d'en-têtes if ($currentLine > $headersLine) { // Si la date est valide et sans month shifting - $date = \DateTimeImmutable::createFromFormat('d/m/Y', (string) $cols[0]); - $isValidDate = $date instanceof \DateTimeImmutable && false === $date->getLastErrors(); + try { + Date::créer('d/m/Y H:i:s', "{$cols[0]} 00:00:00"); + $isValidDate = true; + } catch (DateInvalideException) { + $isValidDate = false; + } // Alors la ligne en cours est un mouvement if ($isValidDate) { @@ -89,9 +95,10 @@ protected function lireMouvements(\SplFileObject $file): MouvementCollection foreach ($rows as $row) { // Date - $date = \DateTimeImmutable::createFromFormat('d/m/Y H:i:s', "{$row['date_operation']} 00:00:00"); - if (!$date instanceof \DateTimeImmutable) { - throw new FichierImportInvalideException(sprintf('Date du mouvement invalide : [%s].', $row['date_operation'])); + try { + $date = Date::créer('d/m/Y H:i:s', "{$row['date_operation']} 00:00:00"); + } catch (DateInvalideException $e) { + throw new FichierImportInvalideException(sprintf('Date du mouvement invalide : [%s].', $row['date_operation']), previous: $e); } // Montant diff --git a/src/Application/Import/CMExcelMouvementsImportHandler.php b/src/Application/Import/CMExcelMouvementsImportHandler.php index 2d6c94a..85255aa 100644 --- a/src/Application/Import/CMExcelMouvementsImportHandler.php +++ b/src/Application/Import/CMExcelMouvementsImportHandler.php @@ -10,6 +10,8 @@ use App\Domain\Mouvement\Mouvement; use App\Domain\Mouvement\MouvementCollection; use App\Domain\Mouvement\MouvementId; +use App\Domain\Temps\Date; +use App\Domain\Temps\DateInvalideException; use Shuchkin\SimpleXLSX; /** @@ -103,14 +105,10 @@ protected function lireMouvements(\SplFileObject $file): MouvementCollection $montant = sprintf('%0.2f', $montant); $montant = new Montant((float) $montant); - // Date : une cellule sans format de date donne un nombre, une cellule vide une chaîne vide, une cellule absente null - if (!is_string($date) || '' === $date) { - throw new FichierImportInvalideException(sprintf('Date du mouvement invalide en ligne [%d] de la feuille [%s] : [%s].', $rowNumber, $sheetName, $date)); - } - + // Date : une cellule de date donne une chaîne Y-m-d H:i:s, une cellule sans format de date un nombre, une cellule vide une chaîne vide, une cellule absente null try { - $dateMouvement = new \DateTimeImmutable($date); - } catch (\DateMalformedStringException $e) { + $dateMouvement = Date::créer('Y-m-d H:i:s', (string) $date); + } catch (DateInvalideException $e) { throw new FichierImportInvalideException(sprintf('Date du mouvement invalide en ligne [%d] de la feuille [%s] : [%s].', $rowNumber, $sheetName, $date), previous: $e); } diff --git a/src/Application/Import/CaisseEpargneCSVMouvementsImportHandler.php b/src/Application/Import/CaisseEpargneCSVMouvementsImportHandler.php index 7fe6cb8..0aa54a6 100644 --- a/src/Application/Import/CaisseEpargneCSVMouvementsImportHandler.php +++ b/src/Application/Import/CaisseEpargneCSVMouvementsImportHandler.php @@ -10,6 +10,8 @@ use App\Domain\Mouvement\Mouvement; use App\Domain\Mouvement\MouvementCollection; use App\Domain\Mouvement\MouvementId; +use App\Domain\Temps\Date; +use App\Domain\Temps\DateInvalideException; /** * Implémente un handler CSV d'import de mouvements de la banque Caisse d'Épargne. @@ -72,8 +74,12 @@ protected function lireMouvements(\SplFileObject $file): MouvementCollection // Si on a dépassé la ligne d'en-têtes if ($currentLine > $headersLine) { // Si la date est valide et sans month shifting - $date = \DateTimeImmutable::createFromFormat('d/m/y', (string) $cols[0]); - $isValidDate = $date instanceof \DateTimeImmutable && false === $date->getLastErrors(); + try { + Date::créer('d/m/y H:i:s', "{$cols[0]} 00:00:00"); + $isValidDate = true; + } catch (DateInvalideException) { + $isValidDate = false; + } // Alors la ligne en cours est un mouvement if ($isValidDate) { @@ -90,9 +96,10 @@ protected function lireMouvements(\SplFileObject $file): MouvementCollection foreach ($rows as $row) { // Date - $date = \DateTimeImmutable::createFromFormat('d/m/y H:i:s', "{$row['date']} 00:00:00"); - if (!$date instanceof \DateTimeImmutable) { - throw new FichierImportInvalideException(sprintf('Date du mouvement invalide : [%s].', $row['date'])); + try { + $date = Date::créer('d/m/y H:i:s', "{$row['date']} 00:00:00"); + } catch (DateInvalideException $e) { + throw new FichierImportInvalideException(sprintf('Date du mouvement invalide : [%s].', $row['date']), previous: $e); } // Montant diff --git a/src/Domain/Temps/Date.php b/src/Domain/Temps/Date.php new file mode 100644 index 0000000..1b836e7 --- /dev/null +++ b/src/Domain/Temps/Date.php @@ -0,0 +1,32 @@ +format($format) !== $date) { + throw new DateInvalideException(sprintf('Date [%s] invalide au format [%s].', $date, $format)); + } + + return $dateTime; + } +} diff --git a/src/Domain/Temps/DateInvalideException.php b/src/Domain/Temps/DateInvalideException.php new file mode 100644 index 0000000..2775bc0 --- /dev/null +++ b/src/Domain/Temps/DateInvalideException.php @@ -0,0 +1,9 @@ +format('d-m-Y') !== $dateString) { - throw new BadRequestHttpException(sprintf('Date du mouvement invalide : [%s].', $dateString)); + try { + $date = Date::créer('d-m-Y H:i:s', "$dateString 00:00:00"); + } catch (DateInvalideException $e) { + throw new BadRequestHttpException(sprintf('Date du mouvement invalide : [%s].', $dateString), $e); } } diff --git a/src/Infrastructure/Controller/MouvementsImportController.php b/src/Infrastructure/Controller/MouvementsImportController.php index 5a4250d..d1185c3 100644 --- a/src/Infrastructure/Controller/MouvementsImportController.php +++ b/src/Infrastructure/Controller/MouvementsImportController.php @@ -16,6 +16,8 @@ use App\Domain\Mouvement\Mouvement; use App\Domain\Mouvement\MouvementCollection; use App\Domain\Mouvement\MouvementRepositoryInterface; +use App\Domain\Temps\Date; +use App\Domain\Temps\DateInvalideException; use App\Infrastructure\Configuration\ConfigurationLoader; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\File\UploadedFile; @@ -190,10 +192,10 @@ public function import(Request $request): Response // Modification éventuelle de la date if (isset($mouvementsData[$id]['date'])) { $dateString = $mouvementsData[$id]['date']; - $date = \DateTimeImmutable::createFromFormat('d-m-Y H:i:s', "$dateString 00:00:00"); - // createFromFormat décale les dates impossibles (31-02 devient 03-03) et lit 26 comme l'an 26 : la date relue doit être celle saisie - if (!$date instanceof \DateTimeImmutable || $date->format('d-m-Y') !== $dateString) { - throw new BadRequestHttpException(sprintf('Date du mouvement invalide : [%s].', $dateString)); + try { + $date = Date::créer('d-m-Y H:i:s', "$dateString 00:00:00"); + } catch (DateInvalideException $e) { + throw new BadRequestHttpException(sprintf('Date du mouvement invalide : [%s].', $dateString), $e); } $mouvement->date = $date; } diff --git a/src/Infrastructure/Denormalizer/CompteDenormalizer.php b/src/Infrastructure/Denormalizer/CompteDenormalizer.php index f90abb2..2bf7620 100644 --- a/src/Infrastructure/Denormalizer/CompteDenormalizer.php +++ b/src/Infrastructure/Denormalizer/CompteDenormalizer.php @@ -8,22 +8,17 @@ use App\Domain\Compte\CompteId; use App\Domain\Compte\Plafond; use App\Domain\Compte\Solde; +use App\Domain\Temps\Date; final readonly class CompteDenormalizer implements Denormalizer { public function denormalize(array $data): Compte { - $dateOuverture = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', "{$data['date_ouverture']} 00:00:00"); - if (!$dateOuverture instanceof \DateTimeImmutable) { - throw new \UnexpectedValueException(sprintf("Date d'ouverture du compte invalide : [%s].", $data['date_ouverture'])); - } + $dateOuverture = Date::créer('Y-m-d H:i:s', "{$data['date_ouverture']} 00:00:00"); $dateFermeture = null; if (is_string($data['date_fermeture'])) { - $dateFermeture = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', "{$data['date_fermeture']} 00:00:00"); - if (!$dateFermeture instanceof \DateTimeImmutable) { - throw new \UnexpectedValueException(sprintf('Date de fermeture du compte invalide : [%s].', $data['date_fermeture'])); - } + $dateFermeture = Date::créer('Y-m-d H:i:s', "{$data['date_fermeture']} 00:00:00"); } return new Compte( diff --git a/src/Infrastructure/Denormalizer/MouvementDenormalizer.php b/src/Infrastructure/Denormalizer/MouvementDenormalizer.php index 0e15301..041af6b 100644 --- a/src/Infrastructure/Denormalizer/MouvementDenormalizer.php +++ b/src/Infrastructure/Denormalizer/MouvementDenormalizer.php @@ -7,6 +7,7 @@ use App\Domain\Mouvement\Montant; use App\Domain\Mouvement\Mouvement; use App\Domain\Mouvement\MouvementId; +use App\Domain\Temps\Date; final readonly class MouvementDenormalizer implements Denormalizer { @@ -18,10 +19,7 @@ public function __construct( public function denormalize(array $data): Mouvement { - $date = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', "{$data['date']} 00:00:00"); - if (!$date instanceof \DateTimeImmutable) { - throw new \UnexpectedValueException(sprintf('Date du mouvement invalide : [%s].', $data['date'])); - } + $date = Date::créer('Y-m-d H:i:s', "{$data['date']} 00:00:00"); return new Mouvement( new MouvementId((string) $data['id']), diff --git a/src/Infrastructure/ValueResolver/PeriodeValueResolver.php b/src/Infrastructure/ValueResolver/PeriodeValueResolver.php index e1afe07..846ed7c 100644 --- a/src/Infrastructure/ValueResolver/PeriodeValueResolver.php +++ b/src/Infrastructure/ValueResolver/PeriodeValueResolver.php @@ -4,6 +4,8 @@ namespace App\Infrastructure\ValueResolver; +use App\Domain\Temps\Date; +use App\Domain\Temps\DateInvalideException; use App\Domain\Temps\Depuis; use App\Domain\Temps\Periode; use Symfony\Component\HttpFoundation\InputBag; @@ -27,15 +29,14 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable $dateStartString = $dateFilter->getString('start'); $dateEndString = $dateFilter->getString('end'); - $dateStart = \DateTimeImmutable::createFromFormat('d-m-Y H:i:s', "$dateStartString 00:00:00"); - $dateEnd = \DateTimeImmutable::createFromFormat('d-m-Y H:i:s', "$dateEndString 23:59:59"); - - if (!($dateStart instanceof \DateTimeImmutable) || !($dateEnd instanceof \DateTimeImmutable) || $dateStart > $dateEnd) { - throw new BadRequestHttpException('La période de dates est invalide.'); + try { + $dateStart = Date::créer('d-m-Y H:i:s', "$dateStartString 00:00:00"); + $dateEnd = Date::créer('d-m-Y H:i:s', "$dateEndString 23:59:59"); + } catch (DateInvalideException $e) { + throw new BadRequestHttpException('La période de dates est invalide.', $e); } - // createFromFormat décale les dates impossibles (31-02 devient 03-03) et lit 25 comme l'an 25 : la date relue doit être celle reçue - if ($dateStart->format('d-m-Y') !== $dateStartString || $dateEnd->format('d-m-Y') !== $dateEndString) { + if ($dateStart > $dateEnd) { throw new BadRequestHttpException('La période de dates est invalide.'); } @@ -70,15 +71,8 @@ public function depuisUnAn(): Periode $year = (int) $year; $lastDayOfMonth = (int) $lastDayOfMonth; - $dateStart = \DateTimeImmutable::createFromFormat('Y-n-j H:i:s', "$year-$month-1 00:00:00"); - if ($dateStart instanceof \DateTimeImmutable) { - $dateStart = $dateStart->modify('-1 year')->setTime(0, 0); // Depuis un an - } - $dateEnd = \DateTimeImmutable::createFromFormat('Y-n-j H:i:s', "$year-$month-$lastDayOfMonth 23:59:59"); - - if (!($dateStart instanceof \DateTimeImmutable) || !($dateEnd instanceof \DateTimeImmutable) || $dateStart > $dateEnd) { - throw new BadRequestHttpException('La période de dates est invalide.'); - } + $dateStart = Date::créer('Y-n-j H:i:s', "$year-$month-1 00:00:00")->modify('-1 year')->setTime(0, 0); // Depuis un an + $dateEnd = Date::créer('Y-n-j H:i:s', "$year-$month-$lastDayOfMonth 23:59:59"); return new Periode($dateStart, $dateEnd); } @@ -94,12 +88,8 @@ public function depuisUnMois(): Periode $year = (int) $year; $lastDayOfMonth = (int) $lastDayOfMonth; - $dateStart = \DateTimeImmutable::createFromFormat('Y-n-j H:i:s', "$year-$month-1 00:00:00"); - $dateEnd = \DateTimeImmutable::createFromFormat('Y-n-j H:i:s', "$year-$month-$lastDayOfMonth 23:59:59"); - - if (!($dateStart instanceof \DateTimeImmutable) || !($dateEnd instanceof \DateTimeImmutable) || $dateStart > $dateEnd) { - throw new BadRequestHttpException('La période de dates est invalide.'); - } + $dateStart = Date::créer('Y-n-j H:i:s', "$year-$month-1 00:00:00"); + $dateEnd = Date::créer('Y-n-j H:i:s', "$year-$month-$lastDayOfMonth 23:59:59"); return new Periode($dateStart, $dateEnd); }