@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | |
| 15 | 15 | use Exception; |
| 16 | 16 | |
| 17 | -class MirzaClient{
|
|
| 17 | +class MirzaClient {
|
|
| 18 | 18 | /** |
| 19 | 19 | * Yandex Translation API Key Variable |
| 20 | 20 | * Publish the configuration using `php artisan vendor:publish`, |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | * @var string |
| 30 | 30 | */ |
| 31 | 31 | public $supportedLanguages; |
| 32 | - public function __construct($key){
|
|
| 32 | + public function __construct($key) {
|
|
| 33 | 33 | $this->isValidKey($key); |
| 34 | 34 | $this->key = $key; |
| 35 | 35 | $this->supportedLanguages = $this->getLanguages(true); |
@@ -41,14 +41,14 @@ discard block |
||
| 41 | 41 | * @return boolean |
| 42 | 42 | * @throws Exception if the key is invalid |
| 43 | 43 | */ |
| 44 | - private function isValidKey($key){
|
|
| 44 | + private function isValidKey($key) {
|
|
| 45 | 45 | $ch = curl_init(); |
| 46 | - curl_setopt($ch, CURLOPT_URL,"https://translate.yandex.net/api/v1.5/tr.json/detect"); |
|
| 46 | + curl_setopt($ch, CURLOPT_URL, "https://translate.yandex.net/api/v1.5/tr.json/detect"); |
|
| 47 | 47 | curl_setopt($ch, CURLOPT_POST, true); |
| 48 | 48 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
| 49 | 49 | curl_setopt($ch, CURLOPT_HEADER, true); |
| 50 | 50 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 51 | - curl_setopt($ch, CURLOPT_POSTFIELDS,"text=YTranslator&key=".$key); |
|
| 51 | + curl_setopt($ch, CURLOPT_POSTFIELDS, "text=YTranslator&key=".$key); |
|
| 52 | 52 | $response = curl_exec($ch); |
| 53 | 53 | $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 54 | 54 | curl_close($ch); |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | $errorMsg = 'An unexpected error has occured while trying to verify the API Key.'; |
| 74 | 74 | break; |
| 75 | 75 | } |
| 76 | - if(!$goodKey){
|
|
| 76 | + if (!$goodKey) {
|
|
| 77 | 77 | throw new \Exception($errorMsg); |
| 78 | 78 | } |
| 79 | 79 | |
@@ -87,18 +87,18 @@ discard block |
||
| 87 | 87 | * @return string |
| 88 | 88 | * @throws Exception if the string could not be translated |
| 89 | 89 | */ |
| 90 | - public function translate($text, $lang, $format = 'plain'){
|
|
| 90 | + public function translate($text, $lang, $format = 'plain') {
|
|
| 91 | 91 | |
| 92 | 92 | $ch = curl_init(); |
| 93 | - curl_setopt($ch, CURLOPT_URL,"https://translate.yandex.net/api/v1.5/tr.json/translate"); |
|
| 93 | + curl_setopt($ch, CURLOPT_URL, "https://translate.yandex.net/api/v1.5/tr.json/translate"); |
|
| 94 | 94 | curl_setopt($ch, CURLOPT_POST, true); |
| 95 | - curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, true); |
|
| 96 | - curl_setopt($ch, CURLOPT_POSTFIELDS,"text=".urlencode($text)."&lang=".$lang."&format=".$format."&key=".$this->key); |
|
| 95 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
|
| 96 | + curl_setopt($ch, CURLOPT_POSTFIELDS, "text=".urlencode($text)."&lang=".$lang."&format=".$format."&key=".$this->key); |
|
| 97 | 97 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 98 | 98 | |
| 99 | 99 | $response = json_decode(curl_exec($ch)); |
| 100 | 100 | curl_close($ch); |
| 101 | - if(array_key_exists('text',$response))
|
|
| 101 | + if (array_key_exists('text', $response))
|
|
| 102 | 102 | return $response->text[0]; |
| 103 | 103 | else |
| 104 | 104 | throw new Exception('This text could not be translated: the string you entered or the language code are maybe invalid. Run getSupportedLanguages() to get the list of supported languages.');
|
@@ -110,18 +110,18 @@ discard block |
||
| 110 | 110 | * @return string |
| 111 | 111 | * @throws Exception if it couldn't detect the language |
| 112 | 112 | */ |
| 113 | - public function detectLanguage($text){
|
|
| 113 | + public function detectLanguage($text) {
|
|
| 114 | 114 | $ch = curl_init(); |
| 115 | - curl_setopt($ch, CURLOPT_URL,"https://translate.yandex.net/api/v1.5/tr.json/detect"); |
|
| 115 | + curl_setopt($ch, CURLOPT_URL, "https://translate.yandex.net/api/v1.5/tr.json/detect"); |
|
| 116 | 116 | curl_setopt($ch, CURLOPT_POST, true); |
| 117 | - curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, true); |
|
| 118 | - curl_setopt($ch, CURLOPT_POSTFIELDS,"text=".urlencode($text)."&key=".$this->key); |
|
| 117 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
|
| 118 | + curl_setopt($ch, CURLOPT_POSTFIELDS, "text=".urlencode($text)."&key=".$this->key); |
|
| 119 | 119 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 120 | 120 | |
| 121 | 121 | $response = json_decode(curl_exec($ch)); |
| 122 | 122 | curl_close($ch); |
| 123 | 123 | |
| 124 | - if(array_key_exists("lang",$response) && $response->lang != null)
|
|
| 124 | + if (array_key_exists("lang", $response) && $response->lang != null)
|
|
| 125 | 125 | return $response->lang; |
| 126 | 126 | else |
| 127 | 127 | throw new Exception('Could not get the language code: the entered string may not be valid.');
|
@@ -134,18 +134,18 @@ discard block |
||
| 134 | 134 | * @return string |
| 135 | 135 | * @throws Exception if an unknown error occures while trying to fetch the list of supported languages |
| 136 | 136 | */ |
| 137 | - public function getLanguages($codes = false){
|
|
| 137 | + public function getLanguages($codes = false) {
|
|
| 138 | 138 | $ch = curl_init(); |
| 139 | - curl_setopt($ch, CURLOPT_URL,"https://translate.yandex.net/api/v1.5/tr.json/getLangs"); |
|
| 139 | + curl_setopt($ch, CURLOPT_URL, "https://translate.yandex.net/api/v1.5/tr.json/getLangs"); |
|
| 140 | 140 | curl_setopt($ch, CURLOPT_POST, true); |
| 141 | - curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, true); |
|
| 142 | - curl_setopt($ch, CURLOPT_POSTFIELDS,"ui=en&key=".$this->key); |
|
| 141 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
|
| 142 | + curl_setopt($ch, CURLOPT_POSTFIELDS, "ui=en&key=".$this->key); |
|
| 143 | 143 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 144 | 144 | |
| 145 | 145 | $response = json_decode(curl_exec($ch)); |
| 146 | 146 | curl_close($ch); |
| 147 | - if(array_key_exists("langs",$response))
|
|
| 148 | - return $codes ? array_keys(json_decode(json_encode($response->langs),true)) : $response->langs; |
|
| 147 | + if (array_key_exists("langs", $response))
|
|
| 148 | + return $codes ? array_keys(json_decode(json_encode($response->langs), true)) : $response->langs; |
|
| 149 | 149 | else |
| 150 | 150 | throw new Exception('An unknown error has occured while trying to fetch the list of supported languages.');
|
| 151 | 151 | } |