| @@ 17-163 (lines=147) @@ | ||
| 14 | * Return currency rate from one currency to an other |
|
| 15 | * output type from HTML to JSON format. |
|
| 16 | */ |
|
| 17 | class CurrencyConverterComponent extends Component |
|
| 18 | { |
|
| 19 | /** |
|
| 20 | * CurrencyratesTable Object |
|
| 21 | * @var \Cake\ORM\Table |
|
| 22 | */ |
|
| 23 | private $_currencyratesTable; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * Default CurrencyConverterComponent settings. |
|
| 27 | * |
|
| 28 | * When calling CurrencyConverterComponent() these settings will be merged with the configuration |
|
| 29 | * you provide. |
|
| 30 | * |
|
| 31 | * - `database` - Mention if Component have to store currency rate in database |
|
| 32 | * - `refresh` - Time interval for Component to refresh currency rate in database |
|
| 33 | * - `decimal` - Number of decimal to use when formatting amount float number |
|
| 34 | * |
|
| 35 | * @var array |
|
| 36 | */ |
|
| 37 | protected $_defaultConfig = [ |
|
| 38 | 'database' => true, // Mention if Component have to store currency rate in database |
|
| 39 | 'refresh' => 24, // Time interval for Component to refresh currency rate in database |
|
| 40 | 'decimal' => 2, // Number of decimal to use when formatting amount float number |
|
| 41 | ]; |
|
| 42 | ||
| 43 | /** |
|
| 44 | * @param array $config |
|
| 45 | * @return void |
|
| 46 | */ |
|
| 47 | public function initialize(array $config = []) { |
|
| 48 | ||
| 49 | $config = $this->getConfig(); |
|
| 50 | ||
| 51 | $this->database = $config['database']; |
|
| 52 | $this->refresh = $config['refresh']; |
|
| 53 | $this->decimal = $config['decimal']; |
|
| 54 | ||
| 55 | $this->_currencyratesTable = TableRegistry::get('CurrencyConverter.Currencyrates'); |
|
| 56 | } |
|
| 57 | ||
| 58 | /** |
|
| 59 | * Convert method take an amount as first parameter and convert it using $from currency and $to currency. |
|
| 60 | * |
|
| 61 | * @param float|string $amount the amount to convert. |
|
| 62 | * @param string $from currency to convert from |
|
| 63 | * @param string $to currency to convert to |
|
| 64 | * @return float $amount converted |
|
| 65 | */ |
|
| 66 | public function convert($amount, $from, $to) |
|
| 67 | { |
|
| 68 | $amount = floatval($amount); |
|
| 69 | $rate = $this->getRateToUse($from, $to); |
|
| 70 | ||
| 71 | return $convert = $this->_formatConvert($rate * $amount); |
|
| 72 | } |
|
| 73 | ||
| 74 | /** |
|
| 75 | * Rate method return the rate of two currencies |
|
| 76 | * |
|
| 77 | * @param string $from currency to get the rate from |
|
| 78 | * @param string $to currency to get the rate to |
|
| 79 | * @return float|null $rate |
|
| 80 | */ |
|
| 81 | public function rate($from, $to) |
|
| 82 | { |
|
| 83 | return $this->getRateToUse($from, $to); |
|
| 84 | } |
|
| 85 | ||
| 86 | /** |
|
| 87 | * getRateToUse return rate to use |
|
| 88 | * Using $from and $to parameters representing currency to deal with and the configuration settings |
|
| 89 | * This method save or update currencyrates Table if necesseray too. |
|
| 90 | * |
|
| 91 | * @param string $from currency to get the rate from |
|
| 92 | * @param string $to currency to get the rate to |
|
| 93 | * @return float|null $rate |
|
| 94 | */ |
|
| 95 | public function getRateToUse($from, $to) |
|
| 96 | { |
|
| 97 | if ($from == $to) return 1; |
|
| 98 | if ($this->database) { |
|
| 99 | // Get a currency rate from table |
|
| 100 | $result = $this->_currencyratesTable->find('all')->where(['from_currency' => $from, 'to_currency' => $to])->first(); |
|
| 101 | // If currency rate is in table and it doesn't have to be updated |
|
| 102 | if ($result && $result->modified->wasWithinLast($this->refresh . ' hours')) return $rate = $result->rate; |
|
| 103 | // If currency rate is in table and it have to be updated |
|
| 104 | if ($result && !$result->modified->wasWithinLast($this->refresh . ' hours')) { |
|
| 105 | if ($rate = $this->_getRateFromAPI($from, $to)) { |
|
| 106 | $result->rate = $rate; |
|
| 107 | $this->_currencyratesTable->save($result); |
|
| 108 | } |
|
| 109 | return $rate; |
|
| 110 | } |
|
| 111 | // If currency rate isn't in table |
|
| 112 | if (!$result) { |
|
| 113 | if ($rate = $this->_getRateFromAPI($from, $to)) { |
|
| 114 | $entity = $this->_currencyratesTable->newEntity([ |
|
| 115 | 'from_currency' => $from, |
|
| 116 | 'to_currency' => $to, |
|
| 117 | 'rate' => $rate |
|
| 118 | ]); |
|
| 119 | $this->_currencyratesTable->save($entity); |
|
| 120 | } |
|
| 121 | return $rate; |
|
| 122 | } |
|
| 123 | } |
|
| 124 | ||
| 125 | return $this->_getRateFromAPI($from, $to); |
|
| 126 | } |
|
| 127 | ||
| 128 | /** |
|
| 129 | * Format float number using configuration |
|
| 130 | * |
|
| 131 | * @return floatval |
|
| 132 | */ |
|
| 133 | private function _formatConvert($number) |
|
| 134 | { |
|
| 135 | return number_format($number, $this->decimal); |
|
| 136 | } |
|
| 137 | ||
| 138 | /** |
|
| 139 | * Call free.currencyconverterapi.com API to get a rate for one currency to an other one currency |
|
| 140 | * |
|
| 141 | * @param string $from the currency |
|
| 142 | * @param string $to the currency |
|
| 143 | * @return int|null $rate |
|
| 144 | */ |
|
| 145 | private function _getRateFromAPI($from, $to) |
|
| 146 | { |
|
| 147 | $rate = null; |
|
| 148 | ||
| 149 | $url = 'https://free.currencyconverterapi.com/api/v5/convert?q=' . $from . '_' . $to . '&compact=ultra'; |
|
| 150 | $request = @fopen($url, 'r'); |
|
| 151 | ||
| 152 | if ($request) { |
|
| 153 | $response = fgets($request, 4096); |
|
| 154 | fclose($request); |
|
| 155 | $response = json_decode($response, true); |
|
| 156 | if (isset($response[$from . '_' . $to])) { |
|
| 157 | $rate = $response[$from . '_' . $to]; |
|
| 158 | } |
|
| 159 | } |
|
| 160 | ||
| 161 | return $rate; |
|
| 162 | } |
|
| 163 | } |
|
| @@ 14-154 (lines=141) @@ | ||
| 11 | * @property \Cake\View\Helper\FormHelper $Form |
|
| 12 | * @property \Cake\View\Helper\NumberHelper $Number |
|
| 13 | */ |
|
| 14 | class CurrencyConverterHelper extends Helper |
|
| 15 | { |
|
| 16 | /** |
|
| 17 | * CurrencyratesTable Object |
|
| 18 | * @var \Cake\ORM\Table |
|
| 19 | */ |
|
| 20 | private $_currencyratesTable; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * Default settings |
|
| 24 | * |
|
| 25 | * @var array |
|
| 26 | */ |
|
| 27 | protected $_defaultConfig = [ |
|
| 28 | 'database' => 2, // Use database to store rate |
|
| 29 | 'refresh' => 24, // Time interval for refreshing rate in database |
|
| 30 | 'decimal' => 2, // Number of decimal to use for convert number |
|
| 31 | ]; |
|
| 32 | ||
| 33 | /** |
|
| 34 | * @param array $config |
|
| 35 | * @return void |
|
| 36 | */ |
|
| 37 | public function initialize(array $config = []) { |
|
| 38 | ||
| 39 | $config = $this->getConfig(); |
|
| 40 | ||
| 41 | $this->database = $config['database']; |
|
| 42 | $this->refresh = $config['refresh']; |
|
| 43 | $this->decimal = $config['decimal']; |
|
| 44 | ||
| 45 | $this->_currencyratesTable = TableRegistry::get('CurrencyConverter.Currencyrates'); |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * Convert method take an amount as first parameter and convert it using $from currency and $to currency. |
|
| 50 | * |
|
| 51 | * @param float|string $amount the amount to convert. |
|
| 52 | * @param string $from currency to convert from |
|
| 53 | * @param string $to currency to convert to |
|
| 54 | * @return float $amount converted |
|
| 55 | */ |
|
| 56 | public function convert($amount, $from, $to) |
|
| 57 | { |
|
| 58 | $amount = floatval($amount); |
|
| 59 | $rate = $this->getRateToUse($from, $to); |
|
| 60 | ||
| 61 | return $convert = $this->_formatConvert($rate * $amount); |
|
| 62 | } |
|
| 63 | ||
| 64 | /** |
|
| 65 | * Rate method return the rate of two currencies |
|
| 66 | * |
|
| 67 | * @param string $from currency to get the rate from |
|
| 68 | * @param string $to currency to get the rate to |
|
| 69 | * @return float|null $rate |
|
| 70 | */ |
|
| 71 | public function rate($from, $to) |
|
| 72 | { |
|
| 73 | return $this->getRateToUse($from, $to); |
|
| 74 | } |
|
| 75 | ||
| 76 | /** |
|
| 77 | * getRateToUse return rate to use |
|
| 78 | * Using $from and $to parameters representing currency to deal with and the configuration settings |
|
| 79 | * This method save or update currencyrates Table if necesseray too. |
|
| 80 | * |
|
| 81 | * @param string $from currency to get the rate from |
|
| 82 | * @param string $to currency to get the rate to |
|
| 83 | * @return float|null $rate |
|
| 84 | */ |
|
| 85 | public function getRateToUse($from, $to) |
|
| 86 | { |
|
| 87 | if ($from == $to) return 1; |
|
| 88 | if ($this->database) { |
|
| 89 | // Get a currency rate from table |
|
| 90 | $result = $this->_currencyratesTable->find('all')->where(['from_currency' => $from, 'to_currency' => $to])->first(); |
|
| 91 | // If currency rate is in table and it doesn't have to be updated |
|
| 92 | if ($result && $result->modified->wasWithinLast($this->refresh . ' hours')) return $rate = $result->rate; |
|
| 93 | // If currency rate is in table and it have to be updated |
|
| 94 | if ($result && !$result->modified->wasWithinLast($this->refresh . ' hours')) { |
|
| 95 | if ($rate = $this->_getRateFromAPI($from, $to)) { |
|
| 96 | $result->rate = $rate; |
|
| 97 | $this->_currencyratesTable->save($result); |
|
| 98 | } |
|
| 99 | return $rate; |
|
| 100 | } |
|
| 101 | // If currency rate isn't in table |
|
| 102 | if (!$result) { |
|
| 103 | if ($rate = $this->_getRateFromAPI($from, $to)) { |
|
| 104 | $entity = $this->_currencyratesTable->newEntity([ |
|
| 105 | 'from_currency' => $from, |
|
| 106 | 'to_currency' => $to, |
|
| 107 | 'rate' => $rate |
|
| 108 | ]); |
|
| 109 | $this->_currencyratesTable->save($entity); |
|
| 110 | } |
|
| 111 | return $rate; |
|
| 112 | } |
|
| 113 | } |
|
| 114 | ||
| 115 | return $this->_getRateFromAPI($from, $to); |
|
| 116 | } |
|
| 117 | ||
| 118 | /** |
|
| 119 | * Format float number using configuration |
|
| 120 | * |
|
| 121 | * @return floatval |
|
| 122 | */ |
|
| 123 | private function _formatConvert($number) |
|
| 124 | { |
|
| 125 | return number_format($number, $this->decimal); |
|
| 126 | } |
|
| 127 | ||
| 128 | /** |
|
| 129 | * Call free.currencyconverterapi.com API to get a rate for one currency to an other one currency |
|
| 130 | * |
|
| 131 | * @param string $from the currency |
|
| 132 | * @param string $to the currency |
|
| 133 | * @return int|null $rate |
|
| 134 | */ |
|
| 135 | private function _getRateFromAPI($from, $to) |
|
| 136 | { |
|
| 137 | $rate = null; |
|
| 138 | ||
| 139 | $url = 'https://free.currencyconverterapi.com/api/v5/convert?q=' . $from . '_' . $to . '&compact=ultra'; |
|
| 140 | $request = @fopen($url, 'r'); |
|
| 141 | ||
| 142 | if ($request) { |
|
| 143 | $response = fgets($request, 4096); |
|
| 144 | fclose($request); |
|
| 145 | ||
| 146 | $response = json_decode($response, true); |
|
| 147 | if (isset($response[$from . '_' . $to])) { |
|
| 148 | $rate = $response[$from . '_' . $to]; |
|
| 149 | } |
|
| 150 | } |
|
| 151 | ||
| 152 | return $rate; |
|
| 153 | } |
|
| 154 | } |
|