From fdab697a8f1444f406e7c60b7f93c49e8f096ed0 Mon Sep 17 00:00:00 2001 From: Matthieu Marcillaud Date: Thu, 13 Jul 2023 15:55:36 +0200 Subject: [PATCH 1/3] tests: Add test for `\DateTimeImmutable` class Refs: #51 --- tests/JsonSerializerTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/JsonSerializerTest.php b/tests/JsonSerializerTest.php index 0953b1d..beb28b1 100644 --- a/tests/JsonSerializerTest.php +++ b/tests/JsonSerializerTest.php @@ -296,6 +296,10 @@ public function testSerializationOfDateTime() $date = new \DateTime('2014-06-15 12:00:00', new \DateTimeZone('UTC')); $obj = $this->serializer->unserialize($this->serializer->serialize($date)); $this->assertSame($date->getTimestamp(), $obj->getTimestamp()); + + $date = new \DateTimeImmutable('2014-06-15 12:00:00', new \DateTimeZone('UTC')); + $obj = $this->serializer->unserialize($this->serializer->serialize($date)); + $this->assertSame($date->getTimestamp(), $obj->getTimestamp()); } /** From 0707ef3c75a5e87d8f58abefd89be66d9acdc3b0 Mon Sep 17 00:00:00 2001 From: Matthieu Marcillaud Date: Thu, 13 Jul 2023 15:56:25 +0200 Subject: [PATCH 2/3] fix: Deprecated when unserialize a `\DateTimeImmutable` class on PHP 8.2 Refs: #51 --- src/JsonSerializer/JsonSerializer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonSerializer/JsonSerializer.php b/src/JsonSerializer/JsonSerializer.php index bb82f2d..626868c 100644 --- a/src/JsonSerializer/JsonSerializer.php +++ b/src/JsonSerializer/JsonSerializer.php @@ -428,7 +428,7 @@ protected function unserializeObject($value) throw new JsonSerializerException('Unable to find class ' . $className); } - if ($className === 'DateTime') { + if ($className === 'DateTime' || $className === 'DateTimeImmutable') { $obj = $this->restoreUsingUnserialize($className, $value); $this->objectMapping[$this->objectMappingIndex++] = $obj; return $obj; From c685af5168f6400a5419b19c9bc01b9fe8d311ff Mon Sep 17 00:00:00 2001 From: Matthieu Marcillaud Date: Thu, 13 Jul 2023 15:59:37 +0200 Subject: [PATCH 3/3] docs(changelog): update fix #51 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index be807ad..d6c8143 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [Unreleased] +### Fixed +- Fixed deprecated with DateTimeImmutable deserialization with PHP 8.2 + ## [3.0.0] - 2020-07-20 ### Fixed - Fixed DateTime & DateTimeImmutable serialization in PHP 7.4+. Thanks @przemyslaw-bogusz