@@ -6,133 +6,133 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Calculate implements CalculateInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @var contains fixer.io api key string |
|
| 11 | - */ |
|
| 12 | - protected $api_key; |
|
| 9 | + /** |
|
| 10 | + * @var contains fixer.io api key string |
|
| 11 | + */ |
|
| 12 | + protected $api_key; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @const contains fixer.io API URL |
|
| 16 | - */ |
|
| 17 | - const API_URL = "http://data.fixer.io/api/latest"; |
|
| 14 | + /** |
|
| 15 | + * @const contains fixer.io API URL |
|
| 16 | + */ |
|
| 17 | + const API_URL = "http://data.fixer.io/api/latest"; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * get api key |
|
| 21 | - * |
|
| 22 | - * @param string $key |
|
| 23 | - * @return bool |
|
| 24 | - */ |
|
| 25 | - public function __construct($key) |
|
| 26 | - { |
|
| 27 | - $this->api_key = $key; |
|
| 19 | + /** |
|
| 20 | + * get api key |
|
| 21 | + * |
|
| 22 | + * @param string $key |
|
| 23 | + * @return bool |
|
| 24 | + */ |
|
| 25 | + public function __construct($key) |
|
| 26 | + { |
|
| 27 | + $this->api_key = $key; |
|
| 28 | 28 | |
| 29 | - return true; |
|
| 30 | - } |
|
| 29 | + return true; |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * change or not float decimals |
|
| 34 | - * |
|
| 35 | - * @param float $calc |
|
| 36 | - * @param int $short |
|
| 37 | - * @return float |
|
| 38 | - */ |
|
| 39 | - public static function roundValue($calc, $short) |
|
| 40 | - { |
|
| 41 | - if($short){ $number = 2; }else{ $number = 5; } |
|
| 42 | - $output = round($calc, $number); |
|
| 32 | + /** |
|
| 33 | + * change or not float decimals |
|
| 34 | + * |
|
| 35 | + * @param float $calc |
|
| 36 | + * @param int $short |
|
| 37 | + * @return float |
|
| 38 | + */ |
|
| 39 | + public static function roundValue($calc, $short) |
|
| 40 | + { |
|
| 41 | + if($short){ $number = 2; }else{ $number = 5; } |
|
| 42 | + $output = round($calc, $number); |
|
| 43 | 43 | |
| 44 | - return $output; |
|
| 45 | - } |
|
| 44 | + return $output; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * currency conversion, cache check and fixer.io api connection |
|
| 49 | - * |
|
| 50 | - * @param string|array $getFrom |
|
| 51 | - * @param string|array $getTo |
|
| 52 | - * @param float $amount |
|
| 53 | - * @param bool|int $short |
|
| 54 | - * @param bool $isCache |
|
| 55 | - * @param int $cacheTime |
|
| 56 | - * @return string|array |
|
| 57 | - */ |
|
| 58 | - public function getValues($getFrom, $getTo, $amount, $short, $isCache, $cacheTime) |
|
| 59 | - { |
|
| 60 | - if($isCache && CacheFile::isCacheDir()){ |
|
| 61 | - if(CacheFile::isFile()){ |
|
| 62 | - $file = CacheFile::getFileName(); |
|
| 63 | - if(CacheFile::isCurrent($file)){ |
|
| 64 | - $data = CacheFile::getCache($file); |
|
| 65 | - }else{ |
|
| 66 | - $data = file_get_contents(self::API_URL."?access_key=".$this->api_key); |
|
| 67 | - CacheFile::setNewCacheFile($file, $data, $cacheTime); |
|
| 68 | - } |
|
| 69 | - }else{ |
|
| 70 | - $data = file_get_contents(self::API_URL."?access_key=".$this->api_key); |
|
| 71 | - CacheFile::setNewCacheFile(false, $data, $cacheTime); |
|
| 72 | - } |
|
| 73 | - }else{ |
|
| 74 | - $data = file_get_contents(self::API_URL."?access_key=".$this->api_key); |
|
| 75 | - } |
|
| 76 | - $data = json_decode($data); |
|
| 47 | + /** |
|
| 48 | + * currency conversion, cache check and fixer.io api connection |
|
| 49 | + * |
|
| 50 | + * @param string|array $getFrom |
|
| 51 | + * @param string|array $getTo |
|
| 52 | + * @param float $amount |
|
| 53 | + * @param bool|int $short |
|
| 54 | + * @param bool $isCache |
|
| 55 | + * @param int $cacheTime |
|
| 56 | + * @return string|array |
|
| 57 | + */ |
|
| 58 | + public function getValues($getFrom, $getTo, $amount, $short, $isCache, $cacheTime) |
|
| 59 | + { |
|
| 60 | + if($isCache && CacheFile::isCacheDir()){ |
|
| 61 | + if(CacheFile::isFile()){ |
|
| 62 | + $file = CacheFile::getFileName(); |
|
| 63 | + if(CacheFile::isCurrent($file)){ |
|
| 64 | + $data = CacheFile::getCache($file); |
|
| 65 | + }else{ |
|
| 66 | + $data = file_get_contents(self::API_URL."?access_key=".$this->api_key); |
|
| 67 | + CacheFile::setNewCacheFile($file, $data, $cacheTime); |
|
| 68 | + } |
|
| 69 | + }else{ |
|
| 70 | + $data = file_get_contents(self::API_URL."?access_key=".$this->api_key); |
|
| 71 | + CacheFile::setNewCacheFile(false, $data, $cacheTime); |
|
| 72 | + } |
|
| 73 | + }else{ |
|
| 74 | + $data = file_get_contents(self::API_URL."?access_key=".$this->api_key); |
|
| 75 | + } |
|
| 76 | + $data = json_decode($data); |
|
| 77 | 77 | |
| 78 | - if($this->validateErrors($data)){ |
|
| 79 | - if(is_array($getFrom) || is_array($getTo)){ |
|
| 80 | - $newArray = array(); |
|
| 78 | + if($this->validateErrors($data)){ |
|
| 79 | + if(is_array($getFrom) || is_array($getTo)){ |
|
| 80 | + $newArray = array(); |
|
| 81 | 81 | |
| 82 | - if(is_array($getFrom)){ |
|
| 83 | - if(is_array($getTo)){ |
|
| 84 | - foreach($getFrom as $currency){ |
|
| 85 | - $getA = $data->rates->$currency; |
|
| 86 | - foreach($getTo as $currency2){ |
|
| 87 | - $getB = $data->rates->$currency2; |
|
| 88 | - $calc = Calculate::roundValue(($amount * $getB)/$getA, $short); |
|
| 89 | - array_push($newArray, array($currency, array($currency2 => $calc))); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - }else{ |
|
| 93 | - foreach($getFrom as $currency){ |
|
| 94 | - $getA = $data->rates->$currency; |
|
| 95 | - $getB = $data->rates->$getTo; |
|
| 96 | - $calc = Calculate::roundValue(($amount * $getB)/$getA, $short); |
|
| 97 | - array_push($newArray, array($currency, array($getTo => $calc))); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - }else{ |
|
| 101 | - foreach($getTo as $currency){ |
|
| 102 | - $getA = $data->rates->$getFrom; |
|
| 103 | - $getB = $data->rates->$currency; |
|
| 104 | - $calc = Calculate::roundValue(($amount * $getB)/$getA, $short); |
|
| 105 | - array_push($newArray, array($getFrom, array($currency => $calc))); |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - $output = $newArray; |
|
| 109 | - }else{ |
|
| 110 | - $getA = $data->rates->$getFrom; |
|
| 111 | - $getB = $data->rates->$getTo; |
|
| 82 | + if(is_array($getFrom)){ |
|
| 83 | + if(is_array($getTo)){ |
|
| 84 | + foreach($getFrom as $currency){ |
|
| 85 | + $getA = $data->rates->$currency; |
|
| 86 | + foreach($getTo as $currency2){ |
|
| 87 | + $getB = $data->rates->$currency2; |
|
| 88 | + $calc = Calculate::roundValue(($amount * $getB)/$getA, $short); |
|
| 89 | + array_push($newArray, array($currency, array($currency2 => $calc))); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + }else{ |
|
| 93 | + foreach($getFrom as $currency){ |
|
| 94 | + $getA = $data->rates->$currency; |
|
| 95 | + $getB = $data->rates->$getTo; |
|
| 96 | + $calc = Calculate::roundValue(($amount * $getB)/$getA, $short); |
|
| 97 | + array_push($newArray, array($currency, array($getTo => $calc))); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + }else{ |
|
| 101 | + foreach($getTo as $currency){ |
|
| 102 | + $getA = $data->rates->$getFrom; |
|
| 103 | + $getB = $data->rates->$currency; |
|
| 104 | + $calc = Calculate::roundValue(($amount * $getB)/$getA, $short); |
|
| 105 | + array_push($newArray, array($getFrom, array($currency => $calc))); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + $output = $newArray; |
|
| 109 | + }else{ |
|
| 110 | + $getA = $data->rates->$getFrom; |
|
| 111 | + $getB = $data->rates->$getTo; |
|
| 112 | 112 | |
| 113 | - $output = Calculate::roundValue(($amount * $getB)/$getA, $short); |
|
| 114 | - } |
|
| 115 | - }else{ |
|
| 116 | - $output = false; |
|
| 117 | - } |
|
| 113 | + $output = Calculate::roundValue(($amount * $getB)/$getA, $short); |
|
| 114 | + } |
|
| 115 | + }else{ |
|
| 116 | + $output = false; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - return $output; |
|
| 120 | - } |
|
| 119 | + return $output; |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * validate errors in conncection with fixer.io api |
|
| 124 | - * |
|
| 125 | - * @param string|array $data |
|
| 126 | - * @return bool |
|
| 127 | - * @throws \InvalidArgumentException |
|
| 128 | - */ |
|
| 129 | - public function validateErrors($data) |
|
| 130 | - { |
|
| 131 | - if(!$data->success){ |
|
| 132 | - $getErrorCode = $data->error->code; |
|
| 133 | - $getErrorInfo = $data->error->info; |
|
| 134 | - throw new \InvalidArgumentException("[$getErrorCode] $getErrorInfo"); |
|
| 135 | - } |
|
| 136 | - return true; |
|
| 137 | - } |
|
| 122 | + /** |
|
| 123 | + * validate errors in conncection with fixer.io api |
|
| 124 | + * |
|
| 125 | + * @param string|array $data |
|
| 126 | + * @return bool |
|
| 127 | + * @throws \InvalidArgumentException |
|
| 128 | + */ |
|
| 129 | + public function validateErrors($data) |
|
| 130 | + { |
|
| 131 | + if(!$data->success){ |
|
| 132 | + $getErrorCode = $data->error->code; |
|
| 133 | + $getErrorInfo = $data->error->info; |
|
| 134 | + throw new \InvalidArgumentException("[$getErrorCode] $getErrorInfo"); |
|
| 135 | + } |
|
| 136 | + return true; |
|
| 137 | + } |
|
| 138 | 138 | } |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | public static function roundValue($calc, $short) |
| 40 | 40 | { |
| 41 | - if($short){ $number = 2; }else{ $number = 5; } |
|
| 41 | + if ($short) { $number = 2; }else { $number = 5; } |
|
| 42 | 42 | $output = round($calc, $number); |
| 43 | 43 | |
| 44 | 44 | return $output; |
@@ -57,62 +57,62 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | public function getValues($getFrom, $getTo, $amount, $short, $isCache, $cacheTime) |
| 59 | 59 | { |
| 60 | - if($isCache && CacheFile::isCacheDir()){ |
|
| 61 | - if(CacheFile::isFile()){ |
|
| 60 | + if ($isCache && CacheFile::isCacheDir()) { |
|
| 61 | + if (CacheFile::isFile()) { |
|
| 62 | 62 | $file = CacheFile::getFileName(); |
| 63 | - if(CacheFile::isCurrent($file)){ |
|
| 63 | + if (CacheFile::isCurrent($file)) { |
|
| 64 | 64 | $data = CacheFile::getCache($file); |
| 65 | - }else{ |
|
| 65 | + }else { |
|
| 66 | 66 | $data = file_get_contents(self::API_URL."?access_key=".$this->api_key); |
| 67 | 67 | CacheFile::setNewCacheFile($file, $data, $cacheTime); |
| 68 | 68 | } |
| 69 | - }else{ |
|
| 69 | + }else { |
|
| 70 | 70 | $data = file_get_contents(self::API_URL."?access_key=".$this->api_key); |
| 71 | 71 | CacheFile::setNewCacheFile(false, $data, $cacheTime); |
| 72 | 72 | } |
| 73 | - }else{ |
|
| 73 | + }else { |
|
| 74 | 74 | $data = file_get_contents(self::API_URL."?access_key=".$this->api_key); |
| 75 | 75 | } |
| 76 | 76 | $data = json_decode($data); |
| 77 | 77 | |
| 78 | - if($this->validateErrors($data)){ |
|
| 79 | - if(is_array($getFrom) || is_array($getTo)){ |
|
| 78 | + if ($this->validateErrors($data)) { |
|
| 79 | + if (is_array($getFrom) || is_array($getTo)) { |
|
| 80 | 80 | $newArray = array(); |
| 81 | 81 | |
| 82 | - if(is_array($getFrom)){ |
|
| 83 | - if(is_array($getTo)){ |
|
| 84 | - foreach($getFrom as $currency){ |
|
| 82 | + if (is_array($getFrom)) { |
|
| 83 | + if (is_array($getTo)) { |
|
| 84 | + foreach ($getFrom as $currency) { |
|
| 85 | 85 | $getA = $data->rates->$currency; |
| 86 | - foreach($getTo as $currency2){ |
|
| 86 | + foreach ($getTo as $currency2) { |
|
| 87 | 87 | $getB = $data->rates->$currency2; |
| 88 | - $calc = Calculate::roundValue(($amount * $getB)/$getA, $short); |
|
| 88 | + $calc = Calculate::roundValue(($amount*$getB)/$getA, $short); |
|
| 89 | 89 | array_push($newArray, array($currency, array($currency2 => $calc))); |
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | - }else{ |
|
| 93 | - foreach($getFrom as $currency){ |
|
| 92 | + }else { |
|
| 93 | + foreach ($getFrom as $currency) { |
|
| 94 | 94 | $getA = $data->rates->$currency; |
| 95 | 95 | $getB = $data->rates->$getTo; |
| 96 | - $calc = Calculate::roundValue(($amount * $getB)/$getA, $short); |
|
| 96 | + $calc = Calculate::roundValue(($amount*$getB)/$getA, $short); |
|
| 97 | 97 | array_push($newArray, array($currency, array($getTo => $calc))); |
| 98 | 98 | } |
| 99 | 99 | } |
| 100 | - }else{ |
|
| 101 | - foreach($getTo as $currency){ |
|
| 100 | + }else { |
|
| 101 | + foreach ($getTo as $currency) { |
|
| 102 | 102 | $getA = $data->rates->$getFrom; |
| 103 | 103 | $getB = $data->rates->$currency; |
| 104 | - $calc = Calculate::roundValue(($amount * $getB)/$getA, $short); |
|
| 104 | + $calc = Calculate::roundValue(($amount*$getB)/$getA, $short); |
|
| 105 | 105 | array_push($newArray, array($getFrom, array($currency => $calc))); |
| 106 | 106 | } |
| 107 | 107 | } |
| 108 | 108 | $output = $newArray; |
| 109 | - }else{ |
|
| 109 | + }else { |
|
| 110 | 110 | $getA = $data->rates->$getFrom; |
| 111 | 111 | $getB = $data->rates->$getTo; |
| 112 | 112 | |
| 113 | - $output = Calculate::roundValue(($amount * $getB)/$getA, $short); |
|
| 113 | + $output = Calculate::roundValue(($amount*$getB)/$getA, $short); |
|
| 114 | 114 | } |
| 115 | - }else{ |
|
| 115 | + }else { |
|
| 116 | 116 | $output = false; |
| 117 | 117 | } |
| 118 | 118 | |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | */ |
| 129 | 129 | public function validateErrors($data) |
| 130 | 130 | { |
| 131 | - if(!$data->success){ |
|
| 131 | + if (!$data->success) { |
|
| 132 | 132 | $getErrorCode = $data->error->code; |
| 133 | 133 | $getErrorInfo = $data->error->info; |
| 134 | 134 | throw new \InvalidArgumentException("[$getErrorCode] $getErrorInfo"); |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | public static function roundValue($calc, $short) |
| 40 | 40 | { |
| 41 | - if($short){ $number = 2; }else{ $number = 5; } |
|
| 41 | + if($short){ $number = 2; } else{ $number = 5; } |
|
| 42 | 42 | $output = round($calc, $number); |
| 43 | 43 | |
| 44 | 44 | return $output; |
@@ -62,15 +62,15 @@ discard block |
||
| 62 | 62 | $file = CacheFile::getFileName(); |
| 63 | 63 | if(CacheFile::isCurrent($file)){ |
| 64 | 64 | $data = CacheFile::getCache($file); |
| 65 | - }else{ |
|
| 65 | + } else{ |
|
| 66 | 66 | $data = file_get_contents(self::API_URL."?access_key=".$this->api_key); |
| 67 | 67 | CacheFile::setNewCacheFile($file, $data, $cacheTime); |
| 68 | 68 | } |
| 69 | - }else{ |
|
| 69 | + } else{ |
|
| 70 | 70 | $data = file_get_contents(self::API_URL."?access_key=".$this->api_key); |
| 71 | 71 | CacheFile::setNewCacheFile(false, $data, $cacheTime); |
| 72 | 72 | } |
| 73 | - }else{ |
|
| 73 | + } else{ |
|
| 74 | 74 | $data = file_get_contents(self::API_URL."?access_key=".$this->api_key); |
| 75 | 75 | } |
| 76 | 76 | $data = json_decode($data); |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | array_push($newArray, array($currency, array($currency2 => $calc))); |
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | - }else{ |
|
| 92 | + } else{ |
|
| 93 | 93 | foreach($getFrom as $currency){ |
| 94 | 94 | $getA = $data->rates->$currency; |
| 95 | 95 | $getB = $data->rates->$getTo; |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | array_push($newArray, array($currency, array($getTo => $calc))); |
| 98 | 98 | } |
| 99 | 99 | } |
| 100 | - }else{ |
|
| 100 | + } else{ |
|
| 101 | 101 | foreach($getTo as $currency){ |
| 102 | 102 | $getA = $data->rates->$getFrom; |
| 103 | 103 | $getB = $data->rates->$currency; |
@@ -106,13 +106,13 @@ discard block |
||
| 106 | 106 | } |
| 107 | 107 | } |
| 108 | 108 | $output = $newArray; |
| 109 | - }else{ |
|
| 109 | + } else{ |
|
| 110 | 110 | $getA = $data->rates->$getFrom; |
| 111 | 111 | $getB = $data->rates->$getTo; |
| 112 | 112 | |
| 113 | 113 | $output = Calculate::roundValue(($amount * $getB)/$getA, $short); |
| 114 | 114 | } |
| 115 | - }else{ |
|
| 115 | + } else{ |
|
| 116 | 116 | $output = false; |
| 117 | 117 | } |
| 118 | 118 | |