From ff73a25ed52c0b20fd8b09127e459c4a07cc8361 Mon Sep 17 00:00:00 2001 From: Abdellatif Ait boudad Date: Sun, 30 Jun 2019 11:44:19 +0100 Subject: [PATCH] add DateTimeImmutableGuesser --- .../Guesser/DateTimeImmutableGuesser.php | 32 +++++++++++++++++++ .../FriendlyContexts/services/guessers.yml | 6 ++++ 2 files changed, 38 insertions(+) create mode 100644 src/Knp/FriendlyContexts/Guesser/DateTimeImmutableGuesser.php diff --git a/src/Knp/FriendlyContexts/Guesser/DateTimeImmutableGuesser.php b/src/Knp/FriendlyContexts/Guesser/DateTimeImmutableGuesser.php new file mode 100644 index 00000000..94bfda15 --- /dev/null +++ b/src/Knp/FriendlyContexts/Guesser/DateTimeImmutableGuesser.php @@ -0,0 +1,32 @@ + null], $mapping); + + return in_array($mapping['type'], ['date_immutable', 'datetime_immutable']); + } + + public function transform($str, array $mapping = null) + { + try { + return new \DateTimeImmutable($str); + } catch (\Exception $e) { + throw new \Exception(sprintf('"%s" is not a supported date_immutable/datetime_immutable format. To know which formats are supported, please visit http://www.php.net/manual/en/datetime.formats.php', $str)); + } + } + + public function fake(array $mapping) + { + return new \DateTimeImmutable(); + } + + public function getName() + { + return 'datetime_immutable'; + } +} diff --git a/src/Knp/FriendlyContexts/services/guessers.yml b/src/Knp/FriendlyContexts/services/guessers.yml index 074ce6a9..30295894 100644 --- a/src/Knp/FriendlyContexts/services/guessers.yml +++ b/src/Knp/FriendlyContexts/services/guessers.yml @@ -36,6 +36,12 @@ services: tags: - { name: friendly.format.guesser } + friendly.guesser.datetime_immutable: + class: Knp\FriendlyContexts\Guesser\DateTimeImmutableGuesser + public: true + tags: + - { name: friendly.format.guesser } + friendly.guesser.string: class: Knp\FriendlyContexts\Guesser\StringGuesser public: true