@@ -7,124 +7,124 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Converter implements ConverterInterface |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @var fixer.io api key string |
|
| 12 | - */ |
|
| 13 | - protected $api_key; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * sortage api key lenght |
|
| 17 | - * |
|
| 18 | - * @var int |
|
| 19 | - */ |
|
| 20 | - public $api_length = 32; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * sortage cache or not |
|
| 24 | - * |
|
| 25 | - * @var bool |
|
| 26 | - */ |
|
| 27 | - public $isCache = false; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * contains storage cache in minutes |
|
| 31 | - * |
|
| 32 | - * @var int |
|
| 33 | - */ |
|
| 34 | - public $cacheTime = 60; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * connect with API fixer.io |
|
| 38 | - * |
|
| 39 | - * @param string $api_key |
|
| 40 | - * @return bool |
|
| 41 | - */ |
|
| 42 | - public function __construct($api_key) |
|
| 43 | - { |
|
| 44 | - if(strlen($api_key)==$this->api_length){ |
|
| 45 | - $this->api_key = $api_key; |
|
| 46 | - }else{ |
|
| 47 | - throw new Exception\InvalidArgumentException('Invalid length of API KEY'); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - return true; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * initial currency conversion |
|
| 55 | - * |
|
| 56 | - * @param string|array $from |
|
| 57 | - * @param string|array $to |
|
| 58 | - * @param float $amount |
|
| 59 | - * @param bool|int $short |
|
| 60 | - * @return string|array |
|
| 61 | - */ |
|
| 62 | - public function cconv($from, $to, $amount = 1, $short = false) |
|
| 63 | - { |
|
| 64 | - $getFrom = $this->checkInput($from); |
|
| 65 | - $getTo = $this->checkInput($to); |
|
| 66 | - |
|
| 67 | - $calculate = new Calculate($this->api_key); |
|
| 68 | - |
|
| 69 | - $output = $calculate->getValues($getFrom, $getTo, $amount, $short, $this->isCache, $this->cacheTime); |
|
| 70 | - |
|
| 71 | - return $output; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * validate input data |
|
| 76 | - * |
|
| 77 | - * @param string $data |
|
| 78 | - * @return bool |
|
| 79 | - */ |
|
| 80 | - public function validateInput($data) |
|
| 81 | - { |
|
| 82 | - if(strlen($data)==2 || strlen($data)==3){ |
|
| 83 | - $output = ConverterCurrency::getCurrency($data); |
|
| 84 | - }else{ |
|
| 85 | - $output = false; |
|
| 86 | - } |
|
| 87 | - return $output; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * initial cache or not |
|
| 92 | - * |
|
| 93 | - * @param bool $is |
|
| 94 | - * @param int $time |
|
| 95 | - * @return bool |
|
| 96 | - */ |
|
| 97 | - public function cache($is = false, $time = 60) |
|
| 98 | - { |
|
| 99 | - $parm = Cache::setCache($is, $time); |
|
| 100 | - $this->isCache = $parm; |
|
| 101 | - $this->cacheTime = $time; |
|
| 102 | - |
|
| 103 | - return true; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * check input data for validator |
|
| 108 | - * |
|
| 109 | - * @param string|array $data |
|
| 110 | - * @return string|array |
|
| 111 | - * @throws Exception\InvalidArgumentException |
|
| 112 | - */ |
|
| 113 | - protected function checkInput($data) |
|
| 114 | - { |
|
| 115 | - if(is_string($data)){ |
|
| 116 | - $value = $this->validateInput($data); |
|
| 117 | - if($value===false) throw new Exception\InvalidArgumentException('Invalid data. Please enter a valid country or currency name'); |
|
| 118 | - }else if(is_array($data)){ |
|
| 119 | - $value = array(); |
|
| 120 | - foreach($data as $single){ |
|
| 121 | - $opt = $this->validateInput($single); |
|
| 122 | - if($opt===false) throw new Exception\InvalidArgumentException('Invalid data. Please enter array with a valid country or currency names'); |
|
| 123 | - array_push($value, $opt); |
|
| 124 | - } |
|
| 125 | - }else{ |
|
| 126 | - throw new Exception\InvalidArgumentException('Invalid data. Please use string or array value'); |
|
| 127 | - } |
|
| 128 | - return $value; |
|
| 129 | - } |
|
| 10 | + /** |
|
| 11 | + * @var fixer.io api key string |
|
| 12 | + */ |
|
| 13 | + protected $api_key; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * sortage api key lenght |
|
| 17 | + * |
|
| 18 | + * @var int |
|
| 19 | + */ |
|
| 20 | + public $api_length = 32; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * sortage cache or not |
|
| 24 | + * |
|
| 25 | + * @var bool |
|
| 26 | + */ |
|
| 27 | + public $isCache = false; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * contains storage cache in minutes |
|
| 31 | + * |
|
| 32 | + * @var int |
|
| 33 | + */ |
|
| 34 | + public $cacheTime = 60; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * connect with API fixer.io |
|
| 38 | + * |
|
| 39 | + * @param string $api_key |
|
| 40 | + * @return bool |
|
| 41 | + */ |
|
| 42 | + public function __construct($api_key) |
|
| 43 | + { |
|
| 44 | + if(strlen($api_key)==$this->api_length){ |
|
| 45 | + $this->api_key = $api_key; |
|
| 46 | + }else{ |
|
| 47 | + throw new Exception\InvalidArgumentException('Invalid length of API KEY'); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + return true; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * initial currency conversion |
|
| 55 | + * |
|
| 56 | + * @param string|array $from |
|
| 57 | + * @param string|array $to |
|
| 58 | + * @param float $amount |
|
| 59 | + * @param bool|int $short |
|
| 60 | + * @return string|array |
|
| 61 | + */ |
|
| 62 | + public function cconv($from, $to, $amount = 1, $short = false) |
|
| 63 | + { |
|
| 64 | + $getFrom = $this->checkInput($from); |
|
| 65 | + $getTo = $this->checkInput($to); |
|
| 66 | + |
|
| 67 | + $calculate = new Calculate($this->api_key); |
|
| 68 | + |
|
| 69 | + $output = $calculate->getValues($getFrom, $getTo, $amount, $short, $this->isCache, $this->cacheTime); |
|
| 70 | + |
|
| 71 | + return $output; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * validate input data |
|
| 76 | + * |
|
| 77 | + * @param string $data |
|
| 78 | + * @return bool |
|
| 79 | + */ |
|
| 80 | + public function validateInput($data) |
|
| 81 | + { |
|
| 82 | + if(strlen($data)==2 || strlen($data)==3){ |
|
| 83 | + $output = ConverterCurrency::getCurrency($data); |
|
| 84 | + }else{ |
|
| 85 | + $output = false; |
|
| 86 | + } |
|
| 87 | + return $output; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * initial cache or not |
|
| 92 | + * |
|
| 93 | + * @param bool $is |
|
| 94 | + * @param int $time |
|
| 95 | + * @return bool |
|
| 96 | + */ |
|
| 97 | + public function cache($is = false, $time = 60) |
|
| 98 | + { |
|
| 99 | + $parm = Cache::setCache($is, $time); |
|
| 100 | + $this->isCache = $parm; |
|
| 101 | + $this->cacheTime = $time; |
|
| 102 | + |
|
| 103 | + return true; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * check input data for validator |
|
| 108 | + * |
|
| 109 | + * @param string|array $data |
|
| 110 | + * @return string|array |
|
| 111 | + * @throws Exception\InvalidArgumentException |
|
| 112 | + */ |
|
| 113 | + protected function checkInput($data) |
|
| 114 | + { |
|
| 115 | + if(is_string($data)){ |
|
| 116 | + $value = $this->validateInput($data); |
|
| 117 | + if($value===false) throw new Exception\InvalidArgumentException('Invalid data. Please enter a valid country or currency name'); |
|
| 118 | + }else if(is_array($data)){ |
|
| 119 | + $value = array(); |
|
| 120 | + foreach($data as $single){ |
|
| 121 | + $opt = $this->validateInput($single); |
|
| 122 | + if($opt===false) throw new Exception\InvalidArgumentException('Invalid data. Please enter array with a valid country or currency names'); |
|
| 123 | + array_push($value, $opt); |
|
| 124 | + } |
|
| 125 | + }else{ |
|
| 126 | + throw new Exception\InvalidArgumentException('Invalid data. Please use string or array value'); |
|
| 127 | + } |
|
| 128 | + return $value; |
|
| 129 | + } |
|
| 130 | 130 | } |