Skip to content

Ensure toJson and toResponse call jsonSerialize #1012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Concerns/ResponsableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function toResponse($request)
}

return new JsonResponse(
data: $this->transform($contextFactory),
data: $this->jsonSerializeWithTransformationContext($contextFactory),
status: $this->calculateResponseStatus($request),
);
}
Expand Down
19 changes: 17 additions & 2 deletions src/Concerns/TransformableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

trait TransformableData
{
protected null|TransformationContextFactory|TransformationContext $_jsonSerializeContextOnce = null;

public function transform(
null|TransformationContextFactory|TransformationContext $transformationContext = null,
): array {
Expand Down Expand Up @@ -51,12 +53,25 @@ public function toArray(): array

public function toJson($options = 0): string
{
return json_encode($this->transform(), $options);
return json_encode($this->jsonSerialize(), $options);
}

public function jsonSerialize(): array
{
return $this->transform();
$transformationContext = $this->_jsonSerializeContextOnce;

if ($this->_jsonSerializeContextOnce) {
$this->_jsonSerializeContextOnce = null;
}

return $this->transform($transformationContext);
}

public function jsonSerializeWithTransformationContext(null|TransformationContextFactory|TransformationContext $transformationContext): array
{
$this->_jsonSerializeContextOnce = $transformationContext;

return $this->jsonSerialize();
}

public static function castUsing(array $arguments)
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/TransformableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public function toJson($options = 0): string;

public function jsonSerialize(): array;

public function jsonSerializeWithTransformationContext(null|TransformationContextFactory|TransformationContext $transformationContext): array;

public static function castUsing(array $arguments);
}
18 changes: 18 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Spatie\LaravelData\Data;

it('calls jsonSerialize when toResponse is invoked', function () {

$data = new class extends Data
{
public function jsonSerialize(): array
{
return [
'string' => 'Hello from serialize',
];
}
};

expect($data->toResponse(request())->getContent())->toBe('{"string":"Hello from serialize"}');
});
15 changes: 15 additions & 0 deletions tests/TransformationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ public function __construct(
->toEqual(json_encode(SimpleData::from('Hello')));
});

it('calls jsonSerialize when toJson is invoked', function () {

$data = new class extends Data
{
public function jsonSerialize(): array
{
return [
'string' => 'Hello from serialize',
];
}
};

expect($data->toJson())->toBe('{"string":"Hello from serialize"}');
});

it('can use a custom transformer for a data object and/or data collectable', function () {
$nestedData = new class (42, 'Hello World') extends Data {
public function __construct(
Expand Down
Loading