diff --git a/src/Forms/ResourceLocatorField.php b/src/Forms/ResourceLocatorField.php index 874d6bd..2b475ce 100644 --- a/src/Forms/ResourceLocatorField.php +++ b/src/Forms/ResourceLocatorField.php @@ -132,7 +132,7 @@ public function setValue($value, $data = null) public function setSubmittedValue($value, $data = null) { - return $this->setValue(json_decode($value, true)); + return $this->setValue(json_decode($value ?? '', true)); } public function dataValue() diff --git a/src/Forms/ResultConditionsField.php b/src/Forms/ResultConditionsField.php index 15a9d95..5907bf8 100644 --- a/src/Forms/ResultConditionsField.php +++ b/src/Forms/ResultConditionsField.php @@ -43,7 +43,7 @@ public function Type() public function performReadonlyTransformation() { // If we have no value then defer to the parent that renders a "none" field - $value = trim($this->Value()); + $value = trim($this->Value() ?? ''); if (empty($value)) { return parent::performReadonlyTransformation(); } diff --git a/src/Model/ResourceField.php b/src/Model/ResourceField.php index 3b4ecd8..adbdc3c 100644 --- a/src/Model/ResourceField.php +++ b/src/Model/ResourceField.php @@ -105,7 +105,7 @@ public function getCMSFields() // See https://github.com/silverstripe/silverstripe-framework/issues/8696 foreach ([$summary, $detail, $readableLabel, $originalLabel] as $field) { - $field->setTitle(ucfirst(strtolower($field->Title()))); + $field->setTitle(ucfirst(strtolower($field->Title() ?? ''))); } }); return parent::getCMSFields(); diff --git a/src/Model/ResourceFilter.php b/src/Model/ResourceFilter.php index f639fb5..daaf09d 100644 --- a/src/Model/ResourceFilter.php +++ b/src/Model/ResourceFilter.php @@ -99,7 +99,7 @@ public function getCMSFields() // See https://github.com/silverstripe/silverstripe-framework/issues/8696 foreach (['AllColumns', 'FilterLabel'] as $fieldName) { $field = $fields->dataFieldByName($fieldName); - $field->setTitle(ucfirst(strtolower($field->Title()))); + $field->setTitle(ucfirst(strtolower($field->Title() ?? ''))); } }); @@ -146,7 +146,7 @@ public function getClientConfig() 'label' => $field->ReadableLabel, 'target' => $field->OriginalLabel, ]; - }, $this->FilterFields()->toArray()), + }, $this->FilterFields()->toArray() ?? []), ]; } diff --git a/src/Model/ResourceFilter/Dropdown.php b/src/Model/ResourceFilter/Dropdown.php index b21100e..af361ab 100644 --- a/src/Model/ResourceFilter/Dropdown.php +++ b/src/Model/ResourceFilter/Dropdown.php @@ -56,7 +56,7 @@ public function getClientConfig() */ protected function getConfiguredOptions() { - $spec = json_decode($this->Options, true) ?: []; + $spec = json_decode($this->Options ?? '', true) ?: []; if (!isset($spec['selectType'])) { return []; diff --git a/src/Page/CKANRegistryPage.php b/src/Page/CKANRegistryPage.php index b04e22e..90b070d 100644 --- a/src/Page/CKANRegistryPage.php +++ b/src/Page/CKANRegistryPage.php @@ -111,9 +111,9 @@ public function getCMSFields() $columns['Position'] = _t(__CLASS__ . '.POS', 'Pos.', 'Abbreviated version of position'); $editable = array_flip(['ShowInResultsView', 'ShowInDetailView']); - $component->setDisplayFields(array_diff_key($columns, $editable)); + $component->setDisplayFields(array_diff_key($columns ?? [], $editable)); // set this way so that title translations are preserved - $editableColumns->setDisplayFields(array_intersect_key($columns, $editable)); + $editableColumns->setDisplayFields(array_intersect_key($columns ?? [], $editable)); } } $columnsConfig->addComponent($editableColumns, $before); diff --git a/src/Page/CKANRegistryPageController.php b/src/Page/CKANRegistryPageController.php index 10c38e4..ce67d74 100644 --- a/src/Page/CKANRegistryPageController.php +++ b/src/Page/CKANRegistryPageController.php @@ -68,7 +68,7 @@ function (ResourceField $field) { 'ReadableLabel' => $field->ReadableLabel, 'ShowInResultsView' => (bool) $field->ShowInResultsView, 'ShowInDetailView' => (bool) $field->ShowInDetailView, - 'DisplayConditions' => json_decode($field->DisplayConditions, true), + 'DisplayConditions' => json_decode($field->DisplayConditions ?? '', true), 'RemoveDuplicates' => (bool) $field->RemoveDuplicates, 'Type' => $field->Type, ]; @@ -77,7 +77,7 @@ function (ResourceField $field) { 'ShowInResultsView' => true, 'ShowInDetailView' => true, 'RemoveDuplicates' => true, - ])->Sort('Position', 'ASC')->toArray() + ])->Sort('Position', 'ASC')->toArray() ?? [] ), 'filters' => array_map( function (ResourceFilter $filter) { @@ -86,7 +86,7 @@ function (ResourceFilter $filter) { 'type' => array_pop($explodedClassName), ] + $filter->getClientConfig(); }, - $resource->Filters()->sort('Order')->toArray() + $resource->Filters()->sort('Order')->toArray() ?? [] ) ]; @@ -121,6 +121,6 @@ public function getBasePath(DataObject $holder = null) } $link = $holder->RelativeLink(); - return Director::baseURL() . trim($link, '/'); + return Director::baseURL() . trim($link ?? '', '/'); } } diff --git a/src/Service/APIClient.php b/src/Service/APIClient.php index f747322..95acb08 100644 --- a/src/Service/APIClient.php +++ b/src/Service/APIClient.php @@ -44,7 +44,7 @@ public function getData(Resource $resource, $action = 'datastore_search', $id = { $endpoint = sprintf( '%s/api/%s/action/%s?id=%s', - trim($resource->Endpoint, '/'), + trim($resource->Endpoint ?? '', '/'), APIClientInterface::API_VERSION, $action, $resource->{$id} @@ -63,11 +63,11 @@ public function getData(Resource $resource, $action = 'datastore_search', $id = throw new RuntimeException('CKAN API is not available. Error code ' . $statusCode); } - if (!count(preg_grep('#application/json#', $response->getHeader('Content-Type')))) { + if (!count(preg_grep('#application/json#', $response->getHeader('Content-Type') ?? []) ?? [])) { throw new RuntimeException('CKAN API returns an invalid response: Content-Type is not JSON'); } - $responseBody = json_decode($response->getBody()->getContents(), true); + $responseBody = json_decode($response->getBody()->getContents() ?? '', true); if (!$responseBody || !isset($responseBody['success']) || !$responseBody['success']) { throw new RuntimeException('CKAN API returns an invalid response: Responded as invalid'); diff --git a/src/Service/ResourcePopulator.php b/src/Service/ResourcePopulator.php index 19cb5fe..021e018 100644 --- a/src/Service/ResourcePopulator.php +++ b/src/Service/ResourcePopulator.php @@ -116,18 +116,18 @@ protected function validateResource(Resource $resource) protected function parseName($id) { // Parse "camelCase" to "space case" - $name = strtolower(preg_replace('/(?<=[a-z\d])([A-Z])/', ' \1', $id)); + $name = strtolower(preg_replace('/(?<=[a-z\d])([A-Z])/', ' \1', $id ?? '') ?? ''); // Swap out certain characters with spaces - $name = str_replace(['_', '-'], ' ', $name); + $name = str_replace(['_', '-'], ' ', $name ?? ''); // Remove some non-alphanumeric characters - $name = trim(preg_replace('/[^\/\w\s]/', '', $name)); + $name = trim(preg_replace('/[^\/\w\s]/', '', $name ?? '') ?? ''); // Remove extra spaces around slashes - $name = str_replace(' / ', '/', $name); + $name = str_replace(' / ', '/', $name ?? ''); - return ucfirst($name); + return ucfirst($name ?? ''); } /** diff --git a/tests/php/Forms/GridField/GridFieldDetailForm/ResourceFieldItemRequestTest.php b/tests/php/Forms/GridField/GridFieldDetailForm/ResourceFieldItemRequestTest.php index 55f8ec4..151c366 100644 --- a/tests/php/Forms/GridField/GridFieldDetailForm/ResourceFieldItemRequestTest.php +++ b/tests/php/Forms/GridField/GridFieldDetailForm/ResourceFieldItemRequestTest.php @@ -143,7 +143,7 @@ protected function assertArrayEqualsInOrder($expected, $actual) '. Actual: ' . print_r($actual, true); foreach ($actual as $key => $value) { - $expectedKey = key($expected); + $expectedKey = key($expected ?? []); $expectedValue = array_shift($expected); $this->assertSame($expectedKey, $key, $message); $this->assertEquals($expectedValue, $value, $message);