From 781516dca5c73900c5e99e3cc5e95c849dd84593 Mon Sep 17 00:00:00 2001 From: Daniel Schreiber Date: Tue, 14 Nov 2023 22:21:30 +0100 Subject: [PATCH] fix: correct handling of null values for date/datetime fields * no deprecation warning (php 8) * return null (instead of false) --- CHANGES.md | 10 +++++++++- lib/PodioObject.php | 2 +- tests/PodioObjectTest.php | 11 +++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 43af745..f2af656 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,4 +1,12 @@ -[7.0.0](#v7.0.0) / [unreleased] +[7.0.2](#v7.0.2) / [UNRELEASED] +================== +* Bugfix: Correct handling of `null` values for date/datetime fields. #244 + +[7.0.1](#v7.0.1) / [2023-11-07] +================== +* Bugfix: Require guzzlehttp/psr7 >= 1.7.0 for Util class by @toby-griffiths in #243. + +[7.0.0](#v7.0.0) / [2023-08-22] ================== * BREAKING: Replace static `Podio` client with instantiable `PodioClient` class. #228 * BREAKING: Replace `save` (and `completed`/`incompleted`/`destroy` on `PodioTask`) methods on instances with static methods #234 diff --git a/lib/PodioObject.php b/lib/PodioObject.php index 8c68ddc..9dba4b4 100644 --- a/lib/PodioObject.php +++ b/lib/PodioObject.php @@ -99,7 +99,7 @@ public function __get($name) } if ($this->has_attribute($name)) { // Create DateTime object if necessary - if ($this->has_property($name) && ($this->__properties[$name]['type'] == 'datetime' || $this->__properties[$name]['type'] == 'date')) { + if ($this->has_property($name) && ($this->__properties[$name]['type'] == 'datetime' || $this->__properties[$name]['type'] == 'date') && !empty($this->__attributes[$name])) { $tz = new DateTimeZone('UTC'); return DateTime::createFromFormat($this->date_format_for_property($name), $this->__attributes[$name], $tz); } diff --git a/tests/PodioObjectTest.php b/tests/PodioObjectTest.php index cd71df8..c79844a 100644 --- a/tests/PodioObjectTest.php +++ b/tests/PodioObjectTest.php @@ -314,4 +314,15 @@ public function test_can_add_child_relationship(): void $this->assertSame($instance, $relationship['instance']); $this->assertSame('fields', $relationship['property']); } + + public function test_null_with_datetime(): void + { + $object = new PodioObject(); + $object->property('datetime_property', 'datetime'); + $object->datetime_property = null; + + $this->assertNull($object->datetime_property); + // expect no (php deprecation) warning: + $this->expectOutputRegex('/^\s*$/'); + } }