diff --git a/src/Models/JsonApiFormatter.php b/src/Models/JsonApiFormatter.php index 72b6c0e..de6305a 100644 --- a/src/Models/JsonApiFormatter.php +++ b/src/Models/JsonApiFormatter.php @@ -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(); @@ -787,4 +767,5 @@ public function validateDataResourceArray(array $data_resource_array): bool return true; } + } diff --git a/tests/Unit/JsonApiFormatterTest.php b/tests/Unit/JsonApiFormatterTest.php index ab58b9b..acff950 100644 --- a/tests/Unit/JsonApiFormatterTest.php +++ b/tests/Unit/JsonApiFormatterTest.php @@ -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(); } @@ -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); + } /** @@ -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()); } @@ -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()]); }