1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the MindbazBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) David DELEVOYE <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Kozikaza\MindbazBundle\Serializer; |
13
|
|
|
|
14
|
|
|
use mbzSubscriber\Subscriber; |
15
|
|
|
use mbzSubscriber\SubscriberFieldData; |
16
|
|
|
use Symfony\Component\Serializer\Encoder\DecoderInterface; |
17
|
|
|
use Symfony\Component\Serializer\Encoder\EncoderInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Vincent Chalamon <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class SubscriberEncoder implements EncoderInterface, DecoderInterface |
23
|
|
|
{ |
24
|
|
|
const FORMAT = 'mindbaz'; |
25
|
|
|
const FIELDS = [ |
26
|
|
|
'id' => 0, |
27
|
|
|
'email' => 1, |
28
|
|
|
'firstSubscriptionDate' => 2, |
29
|
|
|
'lastSubscriptionDate' => 3, |
30
|
|
|
'unsubscriptionDate' => 4, |
31
|
|
|
'status' => 7, |
32
|
|
|
'civility' => 13, |
33
|
|
|
'lastName' => 14, |
34
|
|
|
'firstName' => 15, |
35
|
|
|
'city' => 17, |
36
|
|
|
'zicode' => 18, |
37
|
|
|
'country' => 19, |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $data |
42
|
|
|
* @param string $format |
43
|
|
|
* @param array $context |
44
|
|
|
* |
45
|
|
|
* @return Subscriber |
46
|
|
|
*/ |
47
|
1 |
View Code Duplication |
public function encode($data, $format, array $context = []) |
|
|
|
|
48
|
|
|
{ |
49
|
1 |
|
$fld = []; |
50
|
1 |
|
foreach ($data as $key => $value) { |
51
|
1 |
|
$fld[] = (new SubscriberFieldData(self::FIELDS[$key]))->setValue($value); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
1 |
|
$subscriber = new Subscriber(isset($data['id']) ? $data['id'] : -1); |
55
|
1 |
|
$subscriber->setFld($fld); |
56
|
|
|
|
57
|
1 |
|
return $subscriber; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
2 |
|
public function supportsEncoding($format) |
64
|
|
|
{ |
65
|
2 |
|
return self::FORMAT === $format; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Subscriber $subscriber |
70
|
|
|
* @param string $format |
71
|
|
|
* @param array $context |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
1 |
View Code Duplication |
public function decode($subscriber, $format, array $context = []) |
|
|
|
|
76
|
|
|
{ |
77
|
1 |
|
$fields = array_flip(SubscriberEncoder::FIELDS); |
78
|
1 |
|
$data = []; |
79
|
1 |
|
foreach ($subscriber->getFld() as $fld) { |
80
|
1 |
|
$data[$fields[$fld->getIdField()]] = $fld->getValue(); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
1 |
|
return $data; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
2 |
|
public function supportsDecoding($format) |
90
|
|
|
{ |
91
|
2 |
|
return self::FORMAT === $format; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.