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\ListItemCampaignDefaults; |
9
|
|
|
use AlexCk\MailchimpBundle\Model\MailChimp\ListItemContact; |
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 ListItemNormalizer 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 ListItem $item */ |
25
|
|
|
$item = &$object; |
26
|
|
|
|
27
|
|
|
$data = [ |
28
|
|
|
'name' => $item->getName(), |
29
|
|
|
'contact' => $this->serializer->normalize($item->getContact(), 'json', []), |
|
|
|
|
30
|
|
|
'campaign_defaults' => $this->serializer->normalize($item->getCampaignDefaults(), 'json', []), |
31
|
|
|
'permission_reminder' => $item->getPermissionReminder(), |
32
|
|
|
'email_type_option' => $item->isEmailTypeOption(), |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
return $data; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function denormalize($data, $class, $format = null, array $context = []) |
39
|
|
|
{ |
40
|
|
|
if (is_string($data)) { |
41
|
|
|
$data = json_decode($data, true); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** @var ListItem $item */ |
45
|
|
|
$item = new $class(); |
46
|
|
|
|
47
|
|
|
if ($data) { |
48
|
|
|
foreach ($data as $fieldName => $fieldValue) { |
49
|
|
|
switch ($fieldName) { |
50
|
|
|
case 'id': |
51
|
|
|
$item->setId($fieldValue); |
52
|
|
|
break; |
53
|
|
|
case 'name': |
54
|
|
|
$item->setName($fieldValue); |
55
|
|
|
break; |
56
|
|
|
case 'permission_reminder': |
57
|
|
|
$item->setPermissionReminder($fieldValue); |
58
|
|
|
break; |
59
|
|
|
case 'email_type_option': |
60
|
|
|
$item->setEmailTypeOption($fieldValue); |
61
|
|
|
break; |
62
|
|
|
case 'contact': |
63
|
|
|
$item->setContact($this->serializer->denormalize($fieldValue, ListItemContact::class, 'json', [])); |
|
|
|
|
64
|
|
|
break; |
65
|
|
|
case 'campaign_defaults': |
66
|
|
|
$item->setCampaignDefaults($this->serializer->denormalize($fieldValue, ListItemCampaignDefaults::class, 'json', [])); |
67
|
|
|
break; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $item; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
79
|
|
|
{ |
80
|
|
|
return ListItem::class == $type; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function supportsNormalization($data, $format = null) |
87
|
|
|
{ |
88
|
|
|
return $this->supportsClass($data); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function supportsClass($data) |
92
|
|
|
{ |
93
|
|
|
return $data instanceof ListItem; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|