19 private $baseCurrency;
26 private $counterCurrency;
31 private $conversionRatio;
40 public function __construct(
Currency $baseCurrency,
Currency $counterCurrency, $conversionRatio)
42 if (!is_numeric($conversionRatio)) {
43 throw new \InvalidArgumentException(
'Conversion ratio must be numeric');
46 $this->counterCurrency = $counterCurrency;
47 $this->baseCurrency = $baseCurrency;
48 $this->conversionRatio = (float) $conversionRatio;
60 public static function createFromIso($iso)
62 $currency =
'([A-Z]{2,3})';
63 $ratio =
"([0-9]*\.?[0-9]+)";
64 $pattern =
'#'.$currency.
'/'.$currency.
' '.$ratio.
'#';
68 if (!preg_match($pattern, $iso, $matches)) {
69 throw new \InvalidArgumentException(
71 'Cannot create currency pair from ISO string "%s", format of string is invalid',
77 return new self(
new Currency($matches[1]),
new Currency($matches[2]), $matches[3]);
85 public function getCounterCurrency()
87 return $this->counterCurrency;
95 public function getBaseCurrency()
97 return $this->baseCurrency;
105 public function getConversionRatio()
107 return $this->conversionRatio;
117 public function equals(CurrencyPair $other)
120 $this->baseCurrency->equals($other->baseCurrency)
121 && $this->counterCurrency->equals($other->counterCurrency)
122 && $this->conversionRatio === $other->conversionRatio
131 public function jsonSerialize()
134 'baseCurrency' => $this->baseCurrency,
135 'counterCurrency' => $this->counterCurrency,
136 'ratio' => $this->conversionRatio,