|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AlexCk\MailchimpBundle\Normalize\MailChimp; |
|
6
|
|
|
|
|
7
|
|
|
use AlexCk\MailchimpBundle\Model\MailChimp\ListItem; |
|
8
|
|
|
use AlexCk\MailchimpBundle\Model\MailChimp\MailChimpResponse; |
|
9
|
|
|
use AlexCk\MailchimpBundle\Model\MailChimp\MailChimpWebHook; |
|
10
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
|
11
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
12
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface; |
|
13
|
|
|
use Symfony\Component\Serializer\SerializerAwareTrait; |
|
14
|
|
|
|
|
15
|
|
|
class MailChimpResponseNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use SerializerAwareTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
*/ |
|
22
|
|
|
public function normalize($object, $format = null, array $context = []) |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var MailChimpResponse $item */ |
|
25
|
|
|
$item = &$object; |
|
26
|
|
|
|
|
27
|
|
|
$data = [ |
|
28
|
|
|
'lists' => $this->serializer->normalize($item->getLists(), 'json'), |
|
|
|
|
|
|
29
|
|
|
'webhooks' => $this->serializer->normalize($item->getWebhooks(), 'json'), |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
return $data; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function denormalize($data, $class, $format = null, array $context = []) |
|
36
|
|
|
{ |
|
37
|
|
|
$data = json_decode($data, true); |
|
38
|
|
|
|
|
39
|
|
|
/** @var MailChimpResponse $item */ |
|
40
|
|
|
$item = new $class(); |
|
41
|
|
|
|
|
42
|
|
|
if ($data) { |
|
43
|
|
|
foreach ($data as $fieldName => $fieldValue) { |
|
44
|
|
|
switch ($fieldName) { |
|
45
|
|
|
case 'lists': |
|
46
|
|
|
foreach ($fieldValue as $value) { |
|
47
|
|
|
/** @var ListItem $listItem */ |
|
48
|
|
|
$listItem = $this->serializer->denormalize($value, ListItem::class, 'json', []); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
$item->addList($listItem); |
|
51
|
|
|
} |
|
52
|
|
|
break; |
|
53
|
|
|
case 'webhooks': |
|
54
|
|
|
foreach ($fieldValue as $value) { |
|
55
|
|
|
/** @var MailChimpWebHook $listItem */ |
|
56
|
|
|
$listItem = $this->serializer->denormalize($value, MailChimpWebHook::class, 'json', []); |
|
57
|
|
|
|
|
58
|
|
|
$item->addWebhook($listItem); |
|
59
|
|
|
} |
|
60
|
|
|
break; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $item; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
|
72
|
|
|
{ |
|
73
|
|
|
return MailChimpResponse::class == $type; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function supportsNormalization($data, $format = null) |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->supportsClass($data); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function supportsClass($data) |
|
85
|
|
|
{ |
|
86
|
|
|
return $data instanceof MailChimpResponse; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|