Calculator
4 years ago
Currencies
4 years ago
Exception
4 years ago
Exchange
4 years ago
Formatter
4 years ago
PHPUnit
4 years ago
Parser
4 years ago
Calculator.php
4 years ago
Converter.php
4 years ago
Currencies.php
4 years ago
Currency.php
4 years ago
CurrencyPair.php
4 years ago
Exception.php
4 years ago
Exchange.php
4 years ago
Money.php
4 years ago
MoneyFactory.php
4 years ago
MoneyFormatter.php
4 years ago
MoneyParser.php
4 years ago
Number.php
4 years ago
CurrencyPair.php
140 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Money; |
| 4 | |
| 5 | /** |
| 6 | * Currency Pair holding a base, a counter currency and a conversion ratio. |
| 7 | * |
| 8 | * @author Mathias Verraes |
| 9 | * |
| 10 | * @see http://en.wikipedia.org/wiki/Currency_pair |
| 11 | */ |
| 12 | final class CurrencyPair implements \JsonSerializable |
| 13 | { |
| 14 | /** |
| 15 | * Currency to convert from. |
| 16 | * |
| 17 | * @var Currency |
| 18 | */ |
| 19 | private $baseCurrency; |
| 20 | |
| 21 | /** |
| 22 | * Currency to convert to. |
| 23 | * |
| 24 | * @var Currency |
| 25 | */ |
| 26 | private $counterCurrency; |
| 27 | |
| 28 | /** |
| 29 | * @var float |
| 30 | */ |
| 31 | private $conversionRatio; |
| 32 | |
| 33 | /** |
| 34 | * @param Currency $baseCurrency |
| 35 | * @param Currency $counterCurrency |
| 36 | * @param float $conversionRatio |
| 37 | * |
| 38 | * @throws \InvalidArgumentException If conversion ratio is not numeric |
| 39 | */ |
| 40 | public function __construct(Currency $baseCurrency, Currency $counterCurrency, $conversionRatio) |
| 41 | { |
| 42 | if (!is_numeric($conversionRatio)) { |
| 43 | throw new \InvalidArgumentException('Conversion ratio must be numeric'); |
| 44 | } |
| 45 | |
| 46 | $this->counterCurrency = $counterCurrency; |
| 47 | $this->baseCurrency = $baseCurrency; |
| 48 | $this->conversionRatio = (float) $conversionRatio; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Creates a new Currency Pair based on "EUR/USD 1.2500" form representation. |
| 53 | * |
| 54 | * @param string $iso String representation of the form "EUR/USD 1.2500" |
| 55 | * |
| 56 | * @return CurrencyPair |
| 57 | * |
| 58 | * @throws \InvalidArgumentException Format of $iso is invalid |
| 59 | */ |
| 60 | public static function createFromIso($iso) |
| 61 | { |
| 62 | $currency = '([A-Z]{2,3})'; |
| 63 | $ratio = "([0-9]*\.?[0-9]+)"; // @see http://www.regular-expressions.info/floatingpoint.html |
| 64 | $pattern = '#'.$currency.'/'.$currency.' '.$ratio.'#'; |
| 65 | |
| 66 | $matches = []; |
| 67 | |
| 68 | if (!preg_match($pattern, $iso, $matches)) { |
| 69 | throw new \InvalidArgumentException( |
| 70 | sprintf( |
| 71 | 'Cannot create currency pair from ISO string "%s", format of string is invalid', |
| 72 | $iso |
| 73 | ) |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | return new self(new Currency($matches[1]), new Currency($matches[2]), $matches[3]); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns the counter currency. |
| 82 | * |
| 83 | * @return Currency |
| 84 | */ |
| 85 | public function getCounterCurrency() |
| 86 | { |
| 87 | return $this->counterCurrency; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns the base currency. |
| 92 | * |
| 93 | * @return Currency |
| 94 | */ |
| 95 | public function getBaseCurrency() |
| 96 | { |
| 97 | return $this->baseCurrency; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns the conversion ratio. |
| 102 | * |
| 103 | * @return float |
| 104 | */ |
| 105 | public function getConversionRatio() |
| 106 | { |
| 107 | return $this->conversionRatio; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Checks if an other CurrencyPair has the same parameters as this. |
| 112 | * |
| 113 | * @param CurrencyPair $other |
| 114 | * |
| 115 | * @return bool |
| 116 | */ |
| 117 | public function equals(CurrencyPair $other) |
| 118 | { |
| 119 | return |
| 120 | $this->baseCurrency->equals($other->baseCurrency) |
| 121 | && $this->counterCurrency->equals($other->counterCurrency) |
| 122 | && $this->conversionRatio === $other->conversionRatio |
| 123 | ; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * {@inheritdoc} |
| 128 | * |
| 129 | * @return array |
| 130 | */ |
| 131 | public function jsonSerialize() |
| 132 | { |
| 133 | return [ |
| 134 | 'baseCurrency' => $this->baseCurrency, |
| 135 | 'counterCurrency' => $this->counterCurrency, |
| 136 | 'ratio' => $this->conversionRatio, |
| 137 | ]; |
| 138 | } |
| 139 | } |
| 140 |