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