@@ -67,7 +67,121 @@ The serializers available in this package are: `symfony-json`, `symfony-csv`, `s
67
67
> The ` yaml ` encoder requires the ` symfony/yaml ` package and is disabled when the package is not installed.
68
68
> Install the ` symfony/yaml ` package and the encoder will be automatically enabled.
69
69
70
- @todo ...
70
+ We will use this example DTO for serialization purposes:
71
+
72
+ ``` php
73
+ <?php
74
+
75
+ namespace Application\DTO;
76
+
77
+ use Symfony\Component\Serializer\Annotation\Groups;
78
+ use Symfony\Component\Serializer\Annotation\SerializedName;
79
+
80
+ class MyDTO
81
+ {
82
+ #[Groups(['public'])]
83
+ #[SerializedName('id')]
84
+ private int $id;
85
+
86
+ #[Groups(['public'])]
87
+ #[SerializedName('name')]
88
+ private string $name;
89
+
90
+ #[Groups(['private', 'public'])]
91
+ #[SerializedName('email')]
92
+ private string $email;
93
+
94
+ public function __construct(int $id, string $name, string $email)
95
+ {
96
+ $this->id = $id;
97
+ $this->name = $name;
98
+ $this->email = $email;
99
+ }
100
+
101
+ public function id(): int
102
+ {
103
+ return $this->id;
104
+ }
105
+
106
+ public function name(): string
107
+ {
108
+ return $this->name;
109
+ }
110
+
111
+ public function email(): string
112
+ {
113
+ return $this->email;
114
+ }
115
+ }
116
+ ```
117
+
118
+ ### → Using SerializerManager in your Service Classes
119
+
120
+ ``` php
121
+ <?php
122
+
123
+ namespace Application\Services;
124
+
125
+ use WayOfDev\Serializer\SerializerManager;
126
+ use Application\DTO\MyDTO;
127
+
128
+ class MyService
129
+ {
130
+ public function __construct(
131
+ private readonly SerializerManager $serializer,
132
+ ) {
133
+ }
134
+
135
+ public function someMethod(): void
136
+ {
137
+ $serializer = $serializer->getSerializer('json');
138
+ $dto = new MyDTO(1, 'John Doe', '
[email protected] ');
139
+
140
+ $content = $serializer->normalize(
141
+ data: $dto,
142
+ context: ['groups' => ['private']]
143
+ );
144
+
145
+ $serialized = $serializer->serialize($content);
146
+ }
147
+ }
148
+ ```
149
+
150
+ ### → Using ResponseFactory in Laravel Controllers
151
+
152
+ Here's an example of how you can use the ` ResponseFactory ` in a Laravel controller:
153
+
154
+ ** Example Controller:**
155
+
156
+ ``` php
157
+ <?php
158
+
159
+ namespace Laravel\Http\Controllers;
160
+
161
+ use Application\DTO\MyDTO;
162
+ use Illuminate\Http\Request;
163
+ use WayOfDev\Serializer\ResponseFactory;
164
+
165
+ class MyController extends Controller
166
+ {
167
+ private ResponseFactory $response;
168
+
169
+ public function __construct(ResponseFactory $response)
170
+ {
171
+ $this->response = $response;
172
+ }
173
+
174
+ public function index()
175
+ {
176
+ $dto = new MyDTO(1, 'John Doe', '
[email protected] ');
177
+
178
+ $this->response->withContext(['groups' => ['private']]);
179
+ $this->response->withStatusCode(200);
180
+
181
+ return $this->response->create($dto);
182
+ }
183
+ }
184
+ ```
71
185
72
186
<br >
73
187
0 commit comments