From 21568704a6e0b933ac7be61995fbfa0cfc933944 Mon Sep 17 00:00:00 2001 From: kim_light Date: Sat, 13 Jun 2020 12:31:59 +0300 Subject: [PATCH 1/2] Added handler for correct processing RFC 3339 formatted date/times --- src/Resources/config/services.yaml | 5 ++ src/Services/DateRFC3339Handler.php | 96 +++++++++++++++++++++++ tests/Services/DateRFC3339HandlerTest.php | 50 ++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 src/Services/DateRFC3339Handler.php create mode 100644 tests/Services/DateRFC3339HandlerTest.php diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 80dc336..bc4230d 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -8,6 +8,11 @@ services: tags: - { name: jms_serializer.subscribing_handler } + lenats.jms_serializer.datetime.date_rfc3339 _handler: + class: LeNats\Services\DateRFC3339Handler + tags: + - { name: jms_serializer.subscribing_handler } + LeNats\: resource: '../../{Services,Listeners,Commands}' diff --git a/src/Services/DateRFC3339Handler.php b/src/Services/DateRFC3339Handler.php new file mode 100644 index 0000000..a7a2b9f --- /dev/null +++ b/src/Services/DateRFC3339Handler.php @@ -0,0 +1,96 @@ +decoratedHandler = new DateHandler($defaultFormat, $defaultTimezone, $xmlCData); + $this->defaultFormat = $defaultFormat; + } + + /** + * @param $name + * @param $arguments + * + * @return mixed + */ + public function __call($name, array $arguments) + { + if (method_exists($this->decoratedHandler, $name)) { + switch ($this->defaultFormat) { + case DateTime::ATOM: + case DateTime::RFC3339: + case DateTime::RFC3339_EXTENDED: + case DateTime::W3C: + $arguments[1] = $this->prepare($arguments[1]); + break; + } + + return call_user_func_array([$this->decoratedHandler, $name], $arguments); + } + + return null; + } + + /** + * @inheritDoc + */ + public static function getSubscribingMethods() + { + return DateHandler::getSubscribingMethods(); + } + + /** + * Удаляет секцию *time-secfrac* из строки времени + * @see https://bugs.php.net/bug.php?id=64814 + * + * @param string $paramDatetime + * + * @return string|null + */ + private function prepare(string $paramDatetime): ?string + { + preg_match( + '#' . // open tag + '(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})' // date-time + . '(\.\d+)?' // time-offset = "Z" | time-numoffset + . '((Z)|([+-].+))?' // time-offset + . '#' // close tag + , + $paramDatetime, + $matches + ); + $datetime = $matches[1]; + $timeNumoffset = $matches[5] ?? '+00:00'; + + return $datetime . $timeNumoffset; + } +} \ No newline at end of file diff --git a/tests/Services/DateRFC3339HandlerTest.php b/tests/Services/DateRFC3339HandlerTest.php new file mode 100644 index 0000000..9e5e2cd --- /dev/null +++ b/tests/Services/DateRFC3339HandlerTest.php @@ -0,0 +1,50 @@ +getMethod('prepare'); + $method->setAccessible(true); + $actual = $method->invoke($handler, $datetimeRfc3339); + $this->assertEquals($expected, $actual); + } + + public function datetimeProvider(): array + { + return [ + ' 1' => [ + '2020-06-08T12:46:41+00:00', + '2020-06-08T12:46:41+00:00', + ], + ' 2' => [ + '2020-06-08T14:50:22.257114', + '2020-06-08T14:50:22+00:00', + ], + ' 3' => [ + '2020-06-08T14:50:22.257114Z', + '2020-06-08T14:50:22+00:00', + ], + ' 4' => [ + '2006-12-12T10:01:02.999999999-04:00', + '2006-12-12T10:01:02-04:00', + ], + ]; + } +} From a9064667f87813d30ce7f8ed8e4b8ab06e06da17 Mon Sep 17 00:00:00 2001 From: kim_light Date: Wed, 16 Sep 2020 10:23:15 +0300 Subject: [PATCH 2/2] Fix "NULL returned" for compatibility with symphony 4.4 --- src/Commands/SubscribeCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Commands/SubscribeCommand.php b/src/Commands/SubscribeCommand.php index abcbb48..0ac698e 100644 --- a/src/Commands/SubscribeCommand.php +++ b/src/Commands/SubscribeCommand.php @@ -94,7 +94,7 @@ protected function configure(): void ->setHelp('bin/console nats:subscribe your.queue.name [-t timeout]'); } - protected function execute(InputInterface $input, OutputInterface $output): ?int + protected function execute(InputInterface $input, OutputInterface $output): int { $queue = $input->getArgument('queue'); @@ -123,7 +123,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int return 1; } - return null; + return 0; } private function configureSubscription(