-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjms_serializer.php
45 lines (38 loc) · 994 Bytes
/
jms_serializer.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
<?php
$autoloader = require_once 'vendor/autoload.php';
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializationContext;
use PHPSerializer\JMSSerializer\Entity\Pet;
use PHPSerializer\JMSSerializer\Entity\Person;
use Doctrine\Common\Annotations\AnnotationRegistry;
AnnotationRegistry::registerLoader([$autoloader, 'loadClass']);
$person = new Person(
'Alef',
'(99) 99999-9999'
);
$person->addPet(new Pet(
'simba',
'golden retriever',
'big'
));
$person->addFriend(new Person(
'Alessandro',
'(99) 99999-9999'
));
$serializer = SerializerBuilder::create()->build();
$context = new SerializationContext();
$output = $serializer->serialize(
$person,
'json',
$context->setSerializeNull(true)
->setGroups([
'person_detail',
'pet_list',
'friends' => [
'person_list'
]
])
);
print $output . PHP_EOL;