1 <?php
2 3 4
5 namespace Team3\PayU\Order\Model\Money;
6
7 use Symfony\Component\Validator\Constraints as Assert;
8
9 10 11 12 13 14
15 class Money implements MoneyInterface
16 {
17 18 19 20
21 protected $value;
22
23 24 25 26
27 protected $currency;
28
29 30 31 32
33 protected $precision;
34
35 36 37 38 39 40 41
42 public function __construct($value, $currency = null, $precision = 2)
43 {
44 $this->checkValueValidity($value);
45
46 $this->value = $value;
47 $this->currency = $currency;
48 $this->precision = $precision;
49 }
50
51 52 53
54 public function __toString()
55 {
56 if (null === $this->currency) {
57 return (string) round($this->value, $this->precision);
58 }
59
60 return sprintf(
61 '%s %s',
62 round($this->value, $this->precision),
63 $this->currency
64 );
65 }
66
67 68 69
70 public function getValue()
71 {
72 return (double) $this->value;
73 }
74
75 76 77 78 79 80
81 public function getValueWithoutSeparation($precision = 2)
82 {
83 return (int) sprintf('%d', ($this->getValue() * (double) pow(10, $precision)));
84 }
85
86 87 88 89 90
91 public function add(MoneyInterface $money)
92 {
93 return new self(
94 (double) ($this->getValue() + $money->getValue()),
95 $this->currency,
96 $this->precision
97 );
98 }
99
100 101 102 103 104
105 public function multiply($multiplier)
106 {
107 return new self(
108 (double) ($this->getValue() * $multiplier),
109 $this->currency,
110 $this->precision
111 );
112 }
113
114 115 116 117 118
119 protected function checkValueValidity($value)
120 {
121 if (!is_numeric($value)) {
122 throw new WrongMoneyValueException(sprintf(
123 'Value passed to %s should be numeric, but is %s',
124 get_class($this),
125 gettype($value)
126 ));
127 }
128 }
129 }
130