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
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.