Skip to content

Commit e74e2d8

Browse files
Neutron-ProNeutron-Pro
authored andcommitted
Add readme.md
1 parent 0d3bfd4 commit e74e2d8

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

README.MD

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Symfony Normalizer Enum Type
2+
3+
---
4+
5+
## Read the enum php doc:
6+
7+
> https://github.com/Neutron-Pro/enum-php
8+
9+
---
10+
11+
## Installation
12+
13+
```
14+
composer require neutronstars/symfony-normalizer-enum-php
15+
```
16+
17+
### Add doctrine:
18+
19+
```
20+
composer require neutronstars/doctrine-enum-php-type
21+
```
22+
23+
> https://github.com/Neutron-Pro/doctrine-enum-php-type
24+
25+
---
26+
27+
### Example for serialize an object entity with an enum field:
28+
29+
```php
30+
/**
31+
* @method static self WAITING()
32+
* @method static self PUBLISHED()
33+
*/
34+
class MyStringEnum extends \NeutronStars\Enum\Enum {
35+
public const WAITING = 'Waiting';
36+
public const PUBLISHED = 'Published';
37+
}
38+
39+
/**
40+
* @Entity()
41+
*/
42+
class Post {
43+
/**
44+
* @ORM\Id
45+
* @ORM\GeneratedValue
46+
* @ORM\Column(type="integer")
47+
* @Groups(["post:details"])
48+
* @var int
49+
*/
50+
private $id;
51+
52+
// ... Other field (name, createdAt, updatedAt etc..)
53+
54+
/**
55+
* For the customs type, please check the doctrine enum type documentation.
56+
* @ORM\Column(type="my_string_enum")
57+
* @Groups(["post:details"])
58+
* @var MyStringEnum
59+
*/
60+
private $state;
61+
62+
public function __construct() {
63+
$this->state = MyStringEnum::WAITING();
64+
}
65+
66+
// ... All methods implemented.
67+
}
68+
69+
/**
70+
* @Route("/posts", name="list_post")
71+
*/
72+
class ListPostController {
73+
74+
/** @var PostRepository PostRepository */
75+
private $postRepository;
76+
77+
public function __construct(PostRepository $postRepository) {
78+
$this->postRepository = $postRepository;
79+
}
80+
81+
public function __invoke(): JSONResponse
82+
{
83+
$serializer = new \Symfony\Component\Serializer\Serializer([
84+
new \NeutronStars\Symfony\Enum\Normalizer\EnumNormalizer(MyStringEnum::class)
85+
], [new \Symfony\Component\Serializer\Encoder\JsonEncode()]);
86+
87+
return new JSONResponse(
88+
$serializer->normalize(
89+
$this->postRepository->findAll(),
90+
null,
91+
[ 'groups' => ['post:details'] ]
92+
)
93+
);
94+
}
95+
}
96+
97+
```

0 commit comments

Comments
 (0)