-
Notifications
You must be signed in to change notification settings - Fork 1
/
serializer_test.php
112 lines (89 loc) · 3.37 KB
/
serializer_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
require 'vendor/autoload.php';
use App\Model\Person;
use App\Model\Photo;
use App\Serializer\PersonNormalizer;
use App\Service\PhotoService;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;
$defaultContext = [
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context) {
return $object->getPid();
},
AbstractNormalizer::CIRCULAR_REFERENCE_LIMIT => 1,
];
AnnotationRegistry::registerLoader('class_exists');
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$getSetMethodNormalizer = new GetSetMethodNormalizer($classMetadataFactory, null, null, null, null, $defaultContext);
$normalizers = [
new DateTimeNormalizer,
new PersonNormalizer($getSetMethodNormalizer, new PhotoService()),
new PropertyNormalizer, // addFriend checks and add friend
$getSetMethodNormalizer
];
$encoders = [
new XmlEncoder(),
new JsonEncoder(),
];
// let's faker libraray
$faker = Faker\Factory::create();
$person = new Person($faker->uuid, $faker->name, $faker->email, $faker->dateTime);
$person->setPhoto(new Photo(640, 480, $faker->imageUrl));
$friend1 = new Person($faker->uuid, $faker->name, $faker->email, $faker->dateTime);
$friend1->setPhoto(new Photo(640, 480, $faker->imageUrl));
$person->addFriend($friend1);
$friend2 = new Person($faker->uuid, $faker->name, $faker->email, $faker->dateTime);
$friend2->setPhoto(new Photo(640, 480, $faker->imageUrl));
$friend3 = new Person($faker->uuid, $faker->name, $faker->email, $faker->dateTime);
$friend3->setPhoto(new Photo(640, 480, $faker->imageUrl));
$friend3->addFriend($friend2);
$person->addFriend($friend3);
$serializer = new Serializer($normalizers, $encoders);
// public or private groups
//$serialized = $serializer->serialize($person, 'xml', ['groups' => 'public']);
//echo $serialized;
$serialized = <<<JSON
{
"name": "Jane",
"email": "[email protected]",
"birthDate": "1988-06-15T00:00:00+00:00",
"friends": [
{
"pid": "0d95cddc-d32b-49b6-92e0-6d9569e188fs",
"name": "Jane",
"email": "[email protected]",
"birthDate": "1988-06-15T00:00:00+00:00"
}
]
}
JSON;
$context = [
AbstractNormalizer::DEFAULT_CONSTRUCTOR_ARGUMENTS => [
Person::class => [
'pid' => '0d95cddc-d32b-49b6-92e0-6d9569e188fa',
]
]
];
//$object = $serializer->deserialize($serialized, Person::class, 'json', $context);
// Needs property access component to set addFriend method
$serialized = <<<JSON
{
"name": "Johnny",
"email": "[email protected]"
}
JSON;
$context = [
AbstractNormalizer::OBJECT_TO_POPULATE => $friend1
];
$object = $serializer->deserialize($serialized, Person::class, 'json', $context);
print_r($object);