Skip to content

Commit 98d8e00

Browse files
committed
Add usage example
1 parent 28b7050 commit 98d8e00

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77

88
/tests export-ignore
99

10+
/example.php export-ignore
1011
/phpunit.xml.dist export-ignore

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,57 @@ The utility can be installed with [Composer]:
1212
$ composer require gamez/ramsey-uuid-normalizer
1313
```
1414

15+
## Usage
16+
17+
### Symfony Serializer Component
18+
19+
The usage example requires the [PropertyAccess Component] component,
20+
which can also be installed with [Composer]:
21+
22+
```bash
23+
$ composer require symfony/property-access
24+
```
25+
26+
```php
27+
use Gamez\Symfony\Component\Serializer\Normalizer\UuidNormalizer;
28+
use Ramsey\Uuid\Uuid;
29+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
30+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
31+
use Symfony\Component\Serializer\Serializer;
32+
33+
class Person
34+
{
35+
public $id;
36+
public $name;
37+
}
38+
39+
$person = new Person();
40+
$person->id = Uuid::uuid4();
41+
$person->name = 'Jérôme Gamez';
42+
43+
$encoders = [new JsonEncoder()];
44+
$normalizers = [new UuidNormalizer(), new ObjectNormalizer()];
45+
$serializer = new Serializer($normalizers, $encoders);
46+
47+
$json = $serializer->serialize($person, 'json');
48+
echo $json.PHP_EOL;
49+
// {"id":"3d79048c-29e7-482f-979a-5b9a708b2ede","name":"J\u00e9r\u00f4me Gamez"}
50+
51+
$person = $serializer->deserialize($json, Person::class, 'json');
52+
var_dump($person);
53+
/*
54+
class Person#27 (2) {
55+
public $id =>
56+
string(36) "3d79048c-29e7-482f-979a-5b9a708b2ede"
57+
public $name =>
58+
string(14) "Jérôme Gamez"
59+
}
60+
*/
61+
```
62+
63+
For further information on how to use the Symfony Serializer Component,
64+
please see [The Serializer Component] in the official documentation.
65+
1566
[Composer]: https://getcomposer.org
67+
[PropertyAccess Component]: https://github.com/symfony/property-access
68+
[The Serializer Component]: https://symfony.com/doc/current/components/serializer.html

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"ramsey/uuid": "^3.0"
1616
},
1717
"require-dev": {
18-
"phpunit/phpunit": "^6.3"
18+
"phpunit/phpunit": "^6.3",
19+
"symfony/property-access": "^3.3"
1920
},
2021
"autoload": {
2122
"files": [

example.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
require_once __DIR__.'/vendor/autoload.php';
4+
5+
use Gamez\Symfony\Component\Serializer\Normalizer\UuidNormalizer;
6+
use Ramsey\Uuid\Uuid;
7+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
8+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
9+
use Symfony\Component\Serializer\Serializer;
10+
11+
class Person
12+
{
13+
public $id;
14+
public $name;
15+
}
16+
17+
$person = new Person();
18+
$person->id = Uuid::uuid4();
19+
$person->name = 'Jérôme Gamez';
20+
21+
$encoders = [new JsonEncoder()];
22+
$normalizers = [new UuidNormalizer(), new ObjectNormalizer()];
23+
$serializer = new Serializer($normalizers, $encoders);
24+
25+
$json = $serializer->serialize($person, 'json');
26+
echo $json.PHP_EOL;
27+
28+
$person = $serializer->deserialize($json, Person::class, 'json');
29+
var_dump($person);
30+
31+

0 commit comments

Comments
 (0)