Skip to content

Commit

Permalink
Ongoing testing and debugging.
Browse files Browse the repository at this point in the history
  • Loading branch information
elb98rm committed Mar 27, 2021
1 parent 65a7c80 commit e816166
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
28 changes: 18 additions & 10 deletions src/Models/JsonApiFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,13 @@ private function correctEncode(?array $array = null): string
$content = $this->getBaseResponseArray();
}

// strip nulls from errors by converting to StdClass:
if ($content['errors'] ?? false) {
foreach ($content['errors'] as $key => $error) {
$content['errors'][$key] = (object)$error->toArray();
}
}

return json_encode($content, true);
}

Expand Down Expand Up @@ -616,15 +623,6 @@ public function errorResponseArray(
*/
public function dataResourceResponse($data_resources = null): string
{
// if there's no resources provided, load internally
if (!$data_resources) {
$data_resources = $this->getData();

// empty is not allowed::
if (!$data_resources) {
throw new JsonApiFormatterException('A Data resource requires data to be generated');
}
}
$this->dataResourceResponseArray($data_resources);
return $this->correctEncode();
}
Expand All @@ -634,11 +632,21 @@ public function dataResourceResponse($data_resources = null): string
* @return array
* @throws JsonApiFormatterException
*/
public function dataResourceResponseArray($data_resources): array
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 there's no resources provided, load internally
if (!$data_resources) {
$data_resources = $this->getData();

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

// 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';
Expand Down
11 changes: 7 additions & 4 deletions tests/Unit/JsonApiFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,15 +603,17 @@ public function testErrorResponse()
// make 2 manually checked correct arrays:
$validated_array = [
'errors' => [
$error,
$error2
// remember to clear nulls by flattening using toArray()
(object)$error->toArray(),
(object)$error2->toArray()
],
'meta' => (object)['status' => null],
'jsonapi' => (object)['version' => '1.0']
];
$validated_array2 = [
'errors' => [
$error2
// remember to clear nulls by flattening using toArray()
(object)$error2->toArray()
],
'meta' => (object)['status' => null],
'jsonapi' => (object)['version' => '1.0']
Expand Down Expand Up @@ -655,7 +657,8 @@ public function testExport()
$json_api_formatter->addErrors([$error]);

$error_response_array = [
'errors' => [$error],
// remember to clear nulls by flattening using toArray()
'errors' => [(object)$error->toArray()],
'meta' => [
'status' => null
],
Expand Down

0 comments on commit e816166

Please sign in to comment.