|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Samsara\Fermat\Values; |
|
4
|
|
|
|
|
5
|
|
|
use Samsara\Fermat\Numbers; |
|
6
|
|
|
use Samsara\Fermat\Types\Base\DecimalInterface; |
|
7
|
|
|
use Samsara\Fermat\Types\Base\NumberInterface; |
|
8
|
|
|
use Samsara\Fermat\Types\Number; |
|
9
|
|
|
|
|
10
|
|
|
class Currency extends Number implements NumberInterface, DecimalInterface |
|
11
|
|
|
{ |
|
12
|
|
|
const DOLLAR = '$'; |
|
13
|
|
|
const EURO = '€'; |
|
14
|
|
|
const POUND = '£'; |
|
15
|
|
|
const YUAN = '¥'; |
|
16
|
|
|
const YEN = '¥'; |
|
17
|
|
|
const WON = '₩'; |
|
18
|
|
|
const RUBLE = '₽'; |
|
19
|
|
|
const RIYAL = '﷼'; |
|
20
|
|
|
const RUPEE_INDIA = '₹'; |
|
21
|
|
|
|
|
22
|
|
|
protected $symbol; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct($value, $symbol = Currency::DOLLAR, $precision = null, $base = 10) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->symbol = $symbol; |
|
27
|
|
|
|
|
28
|
|
|
parent::__construct($value, $precision, $base); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function __toString() |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->symbol.$this->multiply(100)->floor()->divide(100)->getValue(); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function modulo($mod) |
|
37
|
|
|
{ |
|
38
|
|
|
$oldBase = $this->convertForModification(); |
|
39
|
|
|
|
|
40
|
|
|
return (new Currency(bcmod($this->getValue(), $mod), $this->getPrecision(), $this->getBase()))->convertFromModification($oldBase); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
View Code Duplication |
public function continuousModulo($mod) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
$mod = Numbers::makeOrDont(Numbers::IMMUTABLE, $mod); |
|
46
|
|
|
|
|
47
|
|
|
$multiple = $this->divide($mod)->floor(); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
$remainder = $this->subtract($mod->multiply($multiple)); |
|
50
|
|
|
|
|
51
|
|
|
return $remainder; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function compoundInterest($interest, $periods) |
|
55
|
|
|
{ |
|
56
|
|
|
|
|
57
|
|
|
$interest = Numbers::makeOrDont(Numbers::IMMUTABLE, $interest); |
|
58
|
|
|
$periods = Numbers::makeOrDont(Numbers::IMMUTABLE, $periods); |
|
59
|
|
|
$e = Numbers::makeE(); |
|
60
|
|
|
|
|
61
|
|
|
return $this->setValue($this->multiply($e->pow($periods->multiply($interest)))); |
|
62
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param $value |
|
67
|
|
|
* |
|
68
|
|
|
* @return Currency |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function setValue($value) |
|
71
|
|
|
{ |
|
72
|
|
|
return new Currency($value, $this->getPrecision(), $this->getBase()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: