@@ -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 | } |
@@ -41,9 +41,9 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | public function __construct($api_key) |
| 43 | 43 | { |
| 44 | - if(strlen($api_key)==$this->api_length){ |
|
| 44 | + if (strlen($api_key)==$this->api_length) { |
|
| 45 | 45 | $this->api_key = $api_key; |
| 46 | - }else{ |
|
| 46 | + }else { |
|
| 47 | 47 | throw new Exception\InvalidArgumentException('Invalid length of API KEY'); |
| 48 | 48 | } |
| 49 | 49 | |
@@ -79,9 +79,9 @@ discard block |
||
| 79 | 79 | */ |
| 80 | 80 | public function validateInput($data) |
| 81 | 81 | { |
| 82 | - if(strlen($data)==2 || strlen($data)==3){ |
|
| 82 | + if (strlen($data)==2 || strlen($data)==3) { |
|
| 83 | 83 | $output = ConverterCurrency::getCurrency($data); |
| 84 | - }else{ |
|
| 84 | + }else { |
|
| 85 | 85 | $output = false; |
| 86 | 86 | } |
| 87 | 87 | return $output; |
@@ -112,17 +112,17 @@ discard block |
||
| 112 | 112 | */ |
| 113 | 113 | protected function checkInput($data) |
| 114 | 114 | { |
| 115 | - if(is_string($data)){ |
|
| 115 | + if (is_string($data)) { |
|
| 116 | 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)){ |
|
| 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 | 119 | $value = array(); |
| 120 | - foreach($data as $single){ |
|
| 120 | + foreach ($data as $single) { |
|
| 121 | 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'); |
|
| 122 | + if ($opt===false) throw new Exception\InvalidArgumentException('Invalid data. Please enter array with a valid country or currency names'); |
|
| 123 | 123 | array_push($value, $opt); |
| 124 | 124 | } |
| 125 | - }else{ |
|
| 125 | + }else { |
|
| 126 | 126 | throw new Exception\InvalidArgumentException('Invalid data. Please use string or array value'); |
| 127 | 127 | } |
| 128 | 128 | return $value; |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | { |
| 44 | 44 | if(strlen($api_key)==$this->api_length){ |
| 45 | 45 | $this->api_key = $api_key; |
| 46 | - }else{ |
|
| 46 | + } else{ |
|
| 47 | 47 | throw new Exception\InvalidArgumentException('Invalid length of API KEY'); |
| 48 | 48 | } |
| 49 | 49 | |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | { |
| 82 | 82 | if(strlen($data)==2 || strlen($data)==3){ |
| 83 | 83 | $output = ConverterCurrency::getCurrency($data); |
| 84 | - }else{ |
|
| 84 | + } else{ |
|
| 85 | 85 | $output = false; |
| 86 | 86 | } |
| 87 | 87 | return $output; |
@@ -114,15 +114,19 @@ discard block |
||
| 114 | 114 | { |
| 115 | 115 | if(is_string($data)){ |
| 116 | 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)){ |
|
| 117 | + if($value===false) { |
|
| 118 | + throw new Exception\InvalidArgumentException('Invalid data. Please enter a valid country or currency name'); |
|
| 119 | + } |
|
| 120 | + } else if(is_array($data)){ |
|
| 119 | 121 | $value = array(); |
| 120 | 122 | foreach($data as $single){ |
| 121 | 123 | $opt = $this->validateInput($single); |
| 122 | - if($opt===false) throw new Exception\InvalidArgumentException('Invalid data. Please enter array with a valid country or currency names'); |
|
| 124 | + if($opt===false) { |
|
| 125 | + throw new Exception\InvalidArgumentException('Invalid data. Please enter array with a valid country or currency names'); |
|
| 126 | + } |
|
| 123 | 127 | array_push($value, $opt); |
| 124 | 128 | } |
| 125 | - }else{ |
|
| 129 | + } else{ |
|
| 126 | 130 | throw new Exception\InvalidArgumentException('Invalid data. Please use string or array value'); |
| 127 | 131 | } |
| 128 | 132 | return $value; |