Passed
Push — master ( 18b32f...fa2b73 )
by Jens
08:15
created

Money::fromArray()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 4
1
<?php
2
/**
3
 * @author @jenschude <[email protected]>
4
 * @created: 04.02.15, 16:44
5
 */
6
7
namespace Commercetools\Core\Model\Common;
8
9
/**
10
 * @package Commercetools\Core\Model\Common
11
 * @link https://docs.commercetools.com/http-api-types.html#money
12
 * @method string getCurrencyCode()
13
 * @method int getCentAmount()
14
 * @method Money setCurrencyCode(string $currencyCode = null)
15
 * @method Money setCentAmount(int $centAmount = null)
16
 * @method string getType()
17
 * @method Money setType(string $type = null)
18
 * @method string getFractionDigits()
19
 * @method Money setFractionDigits(string $fractionDigits = null)
20
 */
21
class Money extends JsonObject
22
{
23
    const CURRENCY_CODE = 'currencyCode';
24
    const CENT_AMOUNT = 'centAmount';
25
    const FRACTION_DIGITS = 'fractionDigits';
26
    const TYPE_CENT_PRECISION = 'centPrecision';
27
    const TYPE_HIGH_PRECISION = 'highPrecision';
28
29 265
    public function fieldDefinitions()
30
    {
31
        return [
32 265
            static::CURRENCY_CODE => [self::TYPE => 'string'],
33 265
            static::CENT_AMOUNT => [self::TYPE => 'int'],
34
        ];
35
    }
36
37
    /**
38
     * @param array $data
39
     * @param Context|callable $context
40
     * @return Money
41
     */
42 84
    public static function fromArray(array $data, $context = null)
43
    {
44 84
        if (get_called_class() == Money::class && isset($data[static::TYPE])) {
45 42
            $className = static::moneyType($data[static::TYPE]);
46 42
            if (class_exists($className)) {
47 42
                return new $className($data, $context);
48
            }
49
        }
50 42
        return new static($data, $context);
51
    }
52
53 42
    protected static function moneyType($type)
54
    {
55
        $types = [
56 42
            static::TYPE_CENT_PRECISION => CentPrecisionMoney::class,
57 42
            static::TYPE_HIGH_PRECISION => HighPrecisionMoney::class,
58
        ];
59 42
        return isset($types[$type]) ? $types[$type] : Money::class;
60
    }
61
62
    /**
63
     * @return string
64
     */
65 4
    public function __toString()
66
    {
67 4
        return $this->getContext()->getCurrencyFormatter()->format($this->getCentAmount(), $this->getCurrencyCode());
68
    }
69
70
    /**
71
     * @param $currencyCode
72
     * @param $centAmount
73
     * @param Context|callable $context
74
     * @return Money
75
     */
76 233
    public static function ofCurrencyAndAmount($currencyCode, $centAmount, $context = null)
77
    {
78 233
        $money = static::of($context);
79 233
        return $money->setCurrencyCode($currencyCode)->setCentAmount($centAmount);
80
    }
81
}
82