Skip to content

Commit 8d4efcb

Browse files
gabplchVladyslav Yarysh
and
Vladyslav Yarysh
authored
Feature/upd dictionary normalizer (#64)
* add `default_normalization` option * add usage section with dictionary enums --------- Co-authored-by: Vladyslav Yarysh <[email protected]>
1 parent 8133e18 commit 8d4efcb

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,33 @@ stfalcon_api:
6262
redis_client_jwt_black_list: "@snc_redis.jwt_black_list"
6363
```
6464

65+
# Usage
66+
## Dictionary enums
67+
For simple dictionary enums, you can use the `DictionaryEnumInteface` interface on Enums.
68+
It will register for serialization like a dictionary, so the result will be like:
69+
```json
70+
{
71+
"id": 1,
72+
"value": "Enum name"
73+
}
74+
```
75+
76+
So, now the dictionary action will look like:
77+
```php
78+
#[Route(path: '/foo/bar', name: 'foo_bar', methods: [Request::METHOD_GET])]
79+
public function __invoke(): JsonResponse
80+
{
81+
// ...
82+
83+
return new JsonResponse(data: $this->serializer->serialize(FooBar::cases()), json: true);
84+
}
85+
```
86+
87+
In some cases, you may need to serialise dictionary value not as dictionary, for this just add in context parameter `default_normalization` with any value.
88+
```php
89+
$this->serializer->serialize($fooBar, 'json', ['default_normalization' => true]);
90+
```
91+
6592
## Contributing
6693

6794
Read the [CONTRIBUTING](https://github.com/stfalcon-studio/ApiBundle/blob/main/.github/CONTRIBUTING.md) file.

Serializer/Normalizer/DictionaryEnumNormalizer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class DictionaryEnumNormalizer implements NormalizerInterface
2828
*/
2929
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
3030
{
31-
return $data instanceof DictionaryEnumInterface;
31+
return $data instanceof DictionaryEnumInterface && !isset($context['default_normalization']);
3232
}
3333

3434
/**

0 commit comments

Comments
 (0)