From 89ecfa924d99b4665f2aa28b7580a078d13ca1ef Mon Sep 17 00:00:00 2001 From: lotyp Date: Sun, 11 Jun 2023 16:34:48 +0300 Subject: [PATCH] docs: add usage examples --- README.md | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ff3f168..792a848 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,121 @@ The serializers available in this package are: `symfony-json`, `symfony-csv`, `s > The `yaml` encoder requires the `symfony/yaml` package and is disabled when the package is not installed. > Install the `symfony/yaml` package and the encoder will be automatically enabled. -@todo ... +We will use this example DTO for serialization purposes: + +```php +id = $id; + $this->name = $name; + $this->email = $email; + } + + public function id(): int + { + return $this->id; + } + + public function name(): string + { + return $this->name; + } + + public function email(): string + { + return $this->email; + } +} +``` + +### → Using SerializerManager in your Service Classes + +```php +getSerializer('json'); + $dto = new MyDTO(1, 'John Doe', 'john@example.com'); + + $content = $serializer->normalize( + data: $dto, + context: ['groups' => ['private']] + ); + + $serialized = $serializer->serialize($content); + } +} +``` + +### → Using ResponseFactory in Laravel Controllers + +Here's an example of how you can use the `ResponseFactory` in a Laravel controller: + +**Example Controller:** + +```php +response = $response; + } + + public function index() + { + $dto = new MyDTO(1, 'John Doe', 'john@example.com'); + + $this->response->withContext(['groups' => ['private']]); + $this->response->withStatusCode(200); + + return $this->response->create($dto); + } +} +```