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

BasePrice::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
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
10
class BasePrice implements SerializableInterface
11
{
12
    /**
13
     * @var Price
14
     */
15
    private $price;
16
17
    /**
18
     * @var string
19
     */
20
    private $currencyCodeString;
21
22
    /**
23
     * @param Price $price
24
     * @param Currency $currency
25
     */
26
    public function __construct(
27
        Price $price,
28
        Currency $currency
29
    ) {
30
        $this->price = $price;
31
        $this->currencyCodeString = $currency->getCode()->toNative();
32
    }
33
34
    /**
35
     * @return Price
36
     */
37
    public function getPrice()
38
    {
39
        return $this->price;
40
    }
41
42
    /**
43
     * @return Currency
44
     */
45
    public function getCurrency()
46
    {
47
        return Currency::fromNative($this->currencyCodeString);
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function serialize()
54
    {
55
        return [
56
            'price' => $this->price->toNative(),
57
            'currency' => $this->currencyCodeString,
58
        ];
59
    }
60
61
    /**
62
     * @param array $data
63
     * @return BasePrice
64
     */
65
    public static function deserialize(array $data)
66
    {
67
        return new BasePrice(
68
            new Price($data['price']),
69
            Currency::fromNative($data['currency'])
70
        );
71
    }
72
73
    /**
74
     * @param Udb3ModelTariff $tariff
75
     * @return BasePrice
76
     */
77
    public static function fromUdb3ModelTariff(Udb3ModelTariff $tariff)
78
    {
79
        return new BasePrice(
80
            new Price($tariff->getPrice()->getAmount()),
81
            new Currency(CurrencyCode::fromNative($tariff->getPrice()->getCurrency()->getName()))
82
        );
83
    }
84
}
85