-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php
More file actions
75 lines (60 loc) · 1.77 KB
/
test.php
File metadata and controls
75 lines (60 loc) · 1.77 KB
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
<?php
require('vendor/autoload.php');
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
class Message
{
public $text;
}
class Envelpe
{
public $message;
}
class EnvelpeNormalizer implements SerializerAwareInterface, NormalizerInterface
{
public function normalize($envelope, string $format = null, array $context = [])
{
$xmlContent = $this->serializer->serialize($envelope->message, 'xml');
$encodedContent = base64_encode($xmlContent);
return [
'message' => $encodedContent,
];
}
public function supportsNormalization($data, string $format = null, array $context = [])
{
return $data instanceof Envelpe;
}
public function setSerializer(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
}
class MessageNormalizer implements NormalizerInterface
{
public function normalize($message, string $format = null, array $context = [])
{
return [
'text' => $message->text,
];
}
public function supportsNormalization($data, string $format = null, array $context = [])
{
return $data instanceof Message;
}
}
$xmlEncoder = new XmlEncoder();
$envelopeNormalizer = new EnvelpeNormalizer();
$messageNormalizer = new MessageNormalizer();
$serializer = new Serializer([
$envelopeNormalizer,
$messageNormalizer,
], [
'xml' => $xmlEncoder,
]);
$enveope = new Envelpe();
$enveope->message = new Message();
$enveope->message->text = 'Hello Word';
$serializer->serialize($enveope, 'xml');