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
|
|
|
/** |
30
|
|
|
* @inheritDoc |
31
|
|
|
*/ |
32
|
275 |
|
public function __construct(array $data = [], $context = null) |
33
|
|
|
{ |
34
|
275 |
|
parent::__construct($data, $context); |
35
|
275 |
|
} |
36
|
|
|
|
37
|
259 |
|
public function fieldDefinitions() |
38
|
|
|
{ |
39
|
|
|
return [ |
40
|
259 |
|
static::CURRENCY_CODE => [self::TYPE => 'string'], |
41
|
259 |
|
static::CENT_AMOUNT => [self::TYPE => 'int'], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $data |
47
|
|
|
* @param Context|callable $context |
48
|
|
|
* @return Money |
49
|
|
|
*/ |
50
|
84 |
|
public static function fromArray(array $data, $context = null) |
51
|
|
|
{ |
52
|
84 |
|
if (get_called_class() == Money::class && isset($data[static::TYPE])) { |
53
|
42 |
|
$className = static::moneyType($data[static::TYPE]); |
54
|
42 |
|
if (class_exists($className)) { |
55
|
42 |
|
return new $className($data, $context); |
56
|
|
|
} |
57
|
|
|
} |
58
|
42 |
|
return new static($data, $context); |
59
|
|
|
} |
60
|
|
|
|
61
|
42 |
|
protected static function moneyType($type) |
62
|
|
|
{ |
63
|
|
|
$types = [ |
64
|
42 |
|
static::TYPE_CENT_PRECISION => CentPrecisionMoney::class, |
65
|
42 |
|
static::TYPE_HIGH_PRECISION => HighPrecisionMoney::class, |
66
|
|
|
]; |
67
|
42 |
|
return isset($types[$type]) ? $types[$type] : Money::class; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
4 |
|
public function __toString() |
74
|
|
|
{ |
75
|
4 |
|
return $this->getContext()->getCurrencyFormatter()->format($this->getCentAmount(), $this->getCurrencyCode()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $currencyCode |
80
|
|
|
* @param $centAmount |
81
|
|
|
* @param Context|callable $context |
82
|
|
|
* @return Money |
83
|
|
|
*/ |
84
|
227 |
|
public static function ofCurrencyAndAmount($currencyCode, $centAmount, $context = null) |
85
|
|
|
{ |
86
|
227 |
|
$money = static::of($context); |
87
|
227 |
|
return $money->setCurrencyCode($currencyCode)->setCentAmount($centAmount); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|