Skip to content

Commit

Permalink
Refactored DataResponse methods to be more efficient/easier. Caught s…
Browse files Browse the repository at this point in the history
…ome edge cases properly.
  • Loading branch information
elb98rm committed Apr 15, 2021
1 parent 9a5ade1 commit 1c215a4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
37 changes: 9 additions & 28 deletions src/Models/JsonApiFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,35 +637,15 @@ public function dataResourceResponseArray($data_resources = null): array
// clear errors: it must not be set in an dataResource response
unset($this->base_response_array['errors']);

if (!$data_resources) {
// if there's no resources provided, load internally

$data_resources = $this->getData();

// empty is not allowed::
if (!$data_resources) {
throw new JsonApiFormatterException('A Data resource requires data to be generated');
}
} else {
// Try and load external data:

// catch bad data:
if (!($data_resources instanceof DataResource || is_array($data_resources))) {
$error = '$data_resources needs to be a data resource or array of data resources';
throw new JsonApiFormatterException($error);
}
// load if it's not been provided:
if ($data_resources !== null) {
$this->setData($data_resources);
}

if ($data_resources instanceof DataResource) {
$this->setData($data_resources);
} else {
foreach ($data_resources as $data_resource) {
if (!$data_resource instanceof DataResource) {
$error = '$data_resources needs to be a data resource or array of data resources';
throw new JsonApiFormatterException($error);
}
$this->addData($data_resource);
}
}
// if it's still null, it needs data!:
if ($this->getData() === null) {
$message = 'Data needs to be set to a data resource or array of data resources';
throw new JsonApiFormatterException($message);
}

$this->autoIncludeJsonapi();
Expand Down Expand Up @@ -787,4 +767,5 @@ public function validateDataResourceArray(array $data_resource_array): bool
return true;
}


}
24 changes: 20 additions & 4 deletions tests/Unit/JsonApiFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,9 @@ public function testDataResourceResponseNoData()
{
$json_api_formatter = new JsonApiFormatter();

// First exception hit is 'id'
// Instantiates with a null:
$this->expectException(JsonApiFormatterException::class);
$this->expectExceptionMessage('A Data resource requires data to be generated');
$this->expectExceptionMessage('Data needs to be set to a data resource or array of data resources');
$json_api_formatter->dataResourceResponse();
}

Expand Down Expand Up @@ -539,6 +539,22 @@ public function testDataResourceResponse()
$response = $json_api_formatter->dataResourceResponse([$data_resource, $data_resource2]);

$this->assertEquals($validated_array_json, $response);

// Empty array (still valid)

$validated_array = [
'data' => [],
'meta' => (object)['status' => null],
'jsonapi' => (object)['version' => '1.0']
];

$validated_array_json = json_encode($validated_array, true);

$json_api_formatter = new JsonApiFormatter();
$response = $json_api_formatter->dataResourceResponse([]);

$this->assertEquals($validated_array_json, $response);

}

/**
Expand All @@ -551,7 +567,7 @@ public function testDataResourceResponseObjectException()
// Single data resource

$this->expectException(JsonApiFormatterException::class);
$this->expectExceptionMessage('$data_resources needs to be a data resource or array of data resources');
$this->expectExceptionMessage('$data needs to be either a DataResource or an array of DataResource objects');
$json_api_formatter->dataResourceResponse(new StdCLass());
}

Expand All @@ -565,7 +581,7 @@ public function testDataResourceResponseArrayException()
// Single data resource

$this->expectException(JsonApiFormatterException::class);
$this->expectExceptionMessage('$data_resources needs to be a data resource or array of data resources');
$this->expectExceptionMessage('$data needs to be either a DataResource or an array of DataResource objects');
$json_api_formatter->dataResourceResponse([new StdCLass()]);
}

Expand Down

0 comments on commit 1c215a4

Please sign in to comment.