From f73c5b85921c7b2308862043f96c0c778256dbff Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Thu, 29 Aug 2024 15:12:39 +1200 Subject: [PATCH] FIX Respect new typehints (#133) --- src/DataFormatter/JSONDataFormatter.php | 10 ++++++++-- src/DataFormatter/XMLDataFormatter.php | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/DataFormatter/JSONDataFormatter.php b/src/DataFormatter/JSONDataFormatter.php index fbd2b96..e5ce165 100644 --- a/src/DataFormatter/JSONDataFormatter.php +++ b/src/DataFormatter/JSONDataFormatter.php @@ -89,7 +89,12 @@ public function convertDataObjectToJSONObject(DataObjectInterface $obj, $fields continue; } - $fieldValue = JSONDataFormatter::cast($obj->obj($fieldName)); + $dbField = $obj->obj($fieldName); + if ($dbField) { + $fieldValue = JSONDataFormatter::cast($dbField); + } else { + $fieldValue = null; + } $mappedFieldName = $this->getFieldAlias($className, $fieldName); $serobj->$mappedFieldName = $fieldValue; } @@ -117,10 +122,11 @@ public function convertDataObjectToJSONObject(DataObjectInterface $obj, $fields ? $this->sanitiseClassName($relClass) . '/' . $obj->$fieldName : $this->sanitiseClassName($className) . "/$id/$relName"; $href = Director::absoluteURL($rel); + $dbField = $obj->obj($fieldName); $serobj->$relName = ArrayData::array_to_object(array( "className" => $relClass, "href" => "$href.json", - "id" => JSONDataFormatter::cast($obj->obj($fieldName)) + "id" => $dbField ? JSONDataFormatter::cast($dbField): '' )); } diff --git a/src/DataFormatter/XMLDataFormatter.php b/src/DataFormatter/XMLDataFormatter.php index c4ba879..6077817 100644 --- a/src/DataFormatter/XMLDataFormatter.php +++ b/src/DataFormatter/XMLDataFormatter.php @@ -125,7 +125,7 @@ public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null, if ($fields && !in_array($fieldName, $fields ?? [])) { continue; } - $fieldValue = $obj->obj($fieldName)->forTemplate(); + $fieldValue = $obj->obj($fieldName)?->forTemplate(); if (!mb_check_encoding($fieldValue, 'utf-8')) { $fieldValue = "(data is badly encoded)"; }