Skip to content

Commit 600b391

Browse files
authored
feat(SimpleDTO): implement JsonSerializable interface (#795)
Add jsonSerialize method to SimpleDTO class to enable native JSON serialization support via json_encode(). The method utilizes toArray() for data transformation. Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
1 parent b6be502 commit 600b391

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/SimpleDTO.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@
3535
use Hyperf\Database\Model\Model;
3636
use Hyperf\HttpMessage\Upload\UploadedFile;
3737
use Hyperf\Validation\ValidationException;
38+
use JsonSerializable;
3839
use ReflectionClass;
3940
use ReflectionProperty;
4041
use UnitEnum;
4142

42-
abstract class SimpleDTO implements BaseDTO, CastsAttributes
43+
abstract class SimpleDTO implements BaseDTO, CastsAttributes, JsonSerializable
4344
{
4445
use DataResolver;
4546
use DataTransformer;
@@ -142,6 +143,14 @@ public function set($model, $key, $value, $attributes)
142143
};
143144
}
144145

146+
/*
147+
* JsonSerializable
148+
*/
149+
public function jsonSerialize(): mixed
150+
{
151+
return $this->toArray();
152+
}
153+
145154
/**
146155
* Defines the default values for the properties of the DTO.
147156
*/

0 commit comments

Comments
 (0)