1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\PriceInfo; |
4
|
|
|
|
5
|
|
|
use Broadway\Serializer\SerializableInterface; |
6
|
|
|
use CultuurNet\UDB3\Model\ValueObject\Price\Tariff as Udb3ModelTariff; |
7
|
|
|
use CultuurNet\UDB3\ValueObject\MultilingualString; |
8
|
|
|
use ValueObjects\Money\Currency; |
9
|
|
|
use ValueObjects\Money\CurrencyCode; |
10
|
|
|
|
11
|
|
|
class Tariff implements SerializableInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var MultilingualString |
15
|
|
|
*/ |
16
|
|
|
private $name; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Price |
20
|
|
|
*/ |
21
|
|
|
private $price; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $currencyCodeString; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param MultilingualString $name |
30
|
|
|
* @param Price $price |
31
|
|
|
* @param Currency $currency |
32
|
|
|
*/ |
33
|
|
|
public function __construct( |
34
|
|
|
MultilingualString $name, |
35
|
|
|
Price $price, |
36
|
|
|
Currency $currency |
37
|
|
|
) { |
38
|
|
|
$this->name = $name; |
39
|
|
|
$this->price = $price; |
40
|
|
|
$this->currencyCodeString = $currency->getCode()->toNative(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return MultilingualString |
45
|
|
|
*/ |
46
|
|
|
public function getName() |
47
|
|
|
{ |
48
|
|
|
return $this->name; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return Price |
53
|
|
|
*/ |
54
|
|
|
public function getPrice() |
55
|
|
|
{ |
56
|
|
|
return $this->price; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return Currency |
61
|
|
|
*/ |
62
|
|
|
public function getCurrency() |
63
|
|
|
{ |
64
|
|
|
return Currency::fromNative($this->currencyCodeString); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function serialize() |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
'name' => $this->name->serialize(), |
74
|
|
|
'price' => $this->price->toNative(), |
75
|
|
|
'currency' => $this->currencyCodeString, |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $data |
81
|
|
|
* @return Tariff |
82
|
|
|
*/ |
83
|
|
|
public static function deserialize(array $data) |
84
|
|
|
{ |
85
|
|
|
return new Tariff( |
86
|
|
|
MultilingualString::deserialize($data['name']), |
87
|
|
|
new Price($data['price']), |
88
|
|
|
Currency::fromNative($data['currency']) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Udb3ModelTariff $udb3ModelTariff |
94
|
|
|
* @return Tariff |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public static function fromUdb3ModelTariff(Udb3ModelTariff $udb3ModelTariff) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
return new Tariff( |
99
|
|
|
MultilingualString::fromUdb3ModelTranslatedValueObject($udb3ModelTariff->getName()), |
100
|
|
|
new Price($udb3ModelTariff->getPrice()->getAmount()), |
101
|
|
|
new Currency(CurrencyCode::fromNative($udb3ModelTariff->getPrice()->getCurrency()->getName())) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.