From 2766b9d9bbedbdeed1558f0020bbc65d762dd5b4 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Mon, 26 Aug 2024 12:43:57 +1200 Subject: [PATCH] FIX Respect new typehints --- 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)"; }