Completed
Pull Request — master (#350)
by
unknown
10:34
created

Tariff::fromUdb3ModelTariff()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
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 ValueObjects\Money\Currency;
8
use ValueObjects\Money\CurrencyCode;
9
use ValueObjects\StringLiteral\StringLiteral;
10
11
class Tariff implements SerializableInterface
12
{
13
    /**
14
     * @var StringLiteral
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 StringLiteral $name
30
     * @param Price $price
31
     * @param Currency $currency
32
     */
33
    public function __construct(
34
        StringLiteral $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 StringLiteral
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->toNative(),
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
            new StringLiteral($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
    public static function fromUdb3ModelTariff(Udb3ModelTariff $udb3ModelTariff)
97
    {
98
        return new Tariff(
99
            new StringLiteral(
100
                $udb3ModelTariff->getName()->getTranslation(
101
                    $udb3ModelTariff->getName()->getOriginalLanguage()
102
                )->toString()
103
            ),
104
            new Price($udb3ModelTariff->getPrice()->getAmount()),
105
            new Currency(CurrencyCode::fromNative($udb3ModelTariff->getPrice()->getCurrency()->getName()))
106
        );
107
    }
108
}
109