Completed
Branch master (bac057)
by Raed
04:45
created
src/Mirza.php 2 patches
Spacing   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -16,7 +16,7 @@  discard block
 block discarded – undo
16 16
 
17 17
 use Exception;
18 18
 
19
-class Mirza{
19
+class Mirza {
20 20
     /**
21 21
      * The variable that will contain the MirzaClient instance
22 22
      *
@@ -29,7 +29,7 @@  discard block
 block discarded – undo
29 29
      * 
30 30
      * @param MirzaClient $MirzaClient
31 31
      */
32
-    public function __construct(MirzaClient $MirzaClient){
32
+    public function __construct(MirzaClient $MirzaClient) {
33 33
         $this->client = $MirzaClient;
34 34
     }
35 35
     /**
@@ -39,8 +39,8 @@  discard block
 block discarded – undo
39 39
      * @param string $lang
40 40
      * @return string
41 41
      */
42
-    public function translate(string $text,string $lang){
43
-        return $this->client->translate($text,$lang);
42
+    public function translate(string $text, string $lang) {
43
+        return $this->client->translate($text, $lang);
44 44
     }
45 45
     /**
46 46
      * Translates an array of text to the given language
@@ -54,19 +54,19 @@  discard block
 block discarded – undo
54 54
      * @return string
55 55
      * @throws Exception if the target language is not supported
56 56
      */
57
-    public function translateArray(array $textArray, string $lang, $assoc = false){
58
-        if(!$this->isSupportedLang($lang)){
57
+    public function translateArray(array $textArray, string $lang, $assoc = false) {
58
+        if (!$this->isSupportedLang($lang)) {
59 59
             throw new Exception('The target language is not supported. Run getSupportedLanguages() to get the list of supported languages.');
60 60
         }
61 61
         $keys = array_keys($textArray);
62 62
         $translated = array();
63 63
 
64 64
         $index = 0;
65
-        if($assoc) $this->isAssoc($textArray);
66
-        foreach($textArray as $key => $text){
65
+        if ($assoc) $this->isAssoc($textArray);
66
+        foreach ($textArray as $key => $text) {
67 67
             $translated[$key] = [
68 68
                 'originalText' => $text,
69
-                'translatedText' => $this->client->translate($text,$lang) 
69
+                'translatedText' => $this->client->translate($text, $lang) 
70 70
             ];
71 71
             $index++;
72 72
         }
@@ -82,23 +82,23 @@  discard block
 block discarded – undo
82 82
      * @return string
83 83
      * @throws Exception if one or more target languages are not supported
84 84
      */
85
-    public function translateTo(string $text, array $langs){
85
+    public function translateTo(string $text, array $langs) {
86 86
         $notSupported = array();
87
-        foreach($langs as $lang){
88
-            if(!$this->isSupportedLang($lang)){
87
+        foreach ($langs as $lang) {
88
+            if (!$this->isSupportedLang($lang)) {
89 89
                 array_push($notSupported, $lang);
90 90
             }
91 91
         }
92
-        if(count($notSupported) > 0){
92
+        if (count($notSupported) > 0) {
93 93
             throw new Exception('The following languages are not supported: '.implode("\n", $notSupported));
94 94
         }
95 95
         $textLang = $this->detectLanguage($text);
96
-        $translatedText = ['originalText' => $text, 'originalLanguage' => $textLang,'text' => array()];
97
-        foreach($langs as $lang){
98
-            try{
99
-                $translatedText['text'][$lang] = $this->client->translate($text,$lang);
96
+        $translatedText = ['originalText' => $text, 'originalLanguage' => $textLang, 'text' => array()];
97
+        foreach ($langs as $lang) {
98
+            try {
99
+                $translatedText['text'][$lang] = $this->client->translate($text, $lang);
100 100
             }
101
-            catch(Exception $e){
101
+            catch (Exception $e) {
102 102
                 $translatedText['text'][$lang] = '';
103 103
             }
104 104
         }
@@ -115,12 +115,12 @@  discard block
 block discarded – undo
115 115
      * @return string
116 116
      * @throws Exception if the language name is not found
117 117
      */
118
-    public function detectLanguage(string $text,$langName = false){
118
+    public function detectLanguage(string $text, $langName = false) {
119 119
         $langCode = $this->client->detectLanguage($text);
120
-        if($langName){
120
+        if ($langName) {
121 121
             $allLanguages = $this->client->getLanguages();
122 122
 
123
-            if(array_key_exists($langCode,$allLanguages))
123
+            if (array_key_exists($langCode, $allLanguages))
124 124
                 return $allLanguages->$langCode;
125 125
             else
126 126
                 throw new Exception('Language name could not be found.');
@@ -133,7 +133,7 @@  discard block
 block discarded – undo
133 133
      *
134 134
      * @return string
135 135
      */
136
-    public function getSupportedLanguages(){
136
+    public function getSupportedLanguages() {
137 137
         return json_encode($this->client->getLanguages());
138 138
     }
139 139
     /**
@@ -143,17 +143,17 @@  discard block
 block discarded – undo
143 143
      * @param string $text
144 144
      * @return string
145 145
      */
146
-    public function translateToAll(string $text){
146
+    public function translateToAll(string $text) {
147 147
         $langs = $this->client->getLanguages(true);
148 148
         $textLang = $this->detectLanguage($text);
149
-        $translatedText = ['originalText' => $text, 'originalLanguage' => $textLang,'text' => array()];
149
+        $translatedText = ['originalText' => $text, 'originalLanguage' => $textLang, 'text' => array()];
150 150
         unset($langs[array_search($textLang, $langs)]);
151 151
         
152
-        foreach($langs as $lang){
153
-            try{
154
-                $translatedText['text'][$lang] = $this->client->translate($text,$lang);
152
+        foreach ($langs as $lang) {
153
+            try {
154
+                $translatedText['text'][$lang] = $this->client->translate($text, $lang);
155 155
             }
156
-            catch(Exception $e){
156
+            catch (Exception $e) {
157 157
                 $translatedText['text'][$lang] = '';
158 158
             }
159 159
         }
@@ -167,7 +167,7 @@  discard block
 block discarded – undo
167 167
      * @param string $fontsize
168 168
      * @return string
169 169
      */
170
-    public function yandex_rights(string $color = "#fff", string $fontsize = "14px"){
170
+    public function yandex_rights(string $color = "#fff", string $fontsize = "14px") {
171 171
         $copyrights = "Powered by Yandex.Translate";
172 172
 
173 173
         return "<a href='https://translate.yandex.com/' target='_blank' style='font-size:$fontsize;color:$color;'>".$copyrights."</a>";
@@ -177,12 +177,12 @@  discard block
 block discarded – undo
177 177
      *
178 178
      * @return string
179 179
      */
180
-    public function languages_select(){
180
+    public function languages_select() {
181 181
         $select = '<select>';
182 182
         $option = '<option value="_lang_code_">_lang_name_</option>';
183
-        foreach($this->client->supportedLanguages as $langCode => $langName){
184
-            $optionTemp = str_replace('_lang_code_',$langCode);
185
-            $optionTemp = str_replace('_lang_name_',$langName);
183
+        foreach ($this->client->supportedLanguages as $langCode => $langName) {
184
+            $optionTemp = str_replace('_lang_code_', $langCode);
185
+            $optionTemp = str_replace('_lang_name_', $langName);
186 186
             $select .= $optionTemp;
187 187
         }
188 188
         $select .= '</select>';
@@ -195,8 +195,8 @@  discard block
 block discarded – undo
195 195
      * @return boolean
196 196
      * @throws Exception if the array given is not associative
197 197
      */
198
-    private function isAssoc(array $array){
199
-        if(array() == $array || array_keys($array) === range(0, count($array) - 1)){
198
+    private function isAssoc(array $array) {
199
+        if (array() == $array || array_keys($array) === range(0, count($array) - 1)) {
200 200
             throw new Exception('Argument 1 given to translateArray is a sequential array, an associative array is expected.');
201 201
         }
202 202
         return true;
@@ -207,8 +207,8 @@  discard block
 block discarded – undo
207 207
      * @param string $lang
208 208
      * @return boolean
209 209
      */
210
-    private function isSupportedLang(string $lang){
211
-        if(!in_array($lang,json_decode(json_encode($this->client->supportedLanguages),true)))
210
+    private function isSupportedLang(string $lang) {
211
+        if (!in_array($lang, json_decode(json_encode($this->client->supportedLanguages), true)))
212 212
             return false;
213 213
         else
214 214
             return true;
Please login to merge, or discard this patch.
Braces   +17 added lines, -15 removed lines patch added patch discarded remove patch
@@ -62,7 +62,9 @@  discard block
 block discarded – undo
62 62
         $translated = array();
63 63
 
64 64
         $index = 0;
65
-        if($assoc) $this->isAssoc($textArray);
65
+        if($assoc) {
66
+            $this->isAssoc($textArray);
67
+        }
66 68
         foreach($textArray as $key => $text){
67 69
             $translated[$key] = [
68 70
                 'originalText' => $text,
@@ -97,8 +99,7 @@  discard block
 block discarded – undo
97 99
         foreach($langs as $lang){
98 100
             try{
99 101
                 $translatedText['text'][$lang] = $this->client->translate($text,$lang);
100
-            }
101
-            catch(Exception $e){
102
+            } catch(Exception $e){
102 103
                 $translatedText['text'][$lang] = '';
103 104
             }
104 105
         }
@@ -120,13 +121,14 @@  discard block
 block discarded – undo
120 121
         if($langName){
121 122
             $allLanguages = $this->client->getLanguages();
122 123
 
123
-            if(array_key_exists($langCode,$allLanguages))
124
-                return $allLanguages->$langCode;
125
-            else
126
-                throw new Exception('Language name could not be found.');
124
+            if(array_key_exists($langCode,$allLanguages)) {
125
+                            return $allLanguages->$langCode;
126
+            } else {
127
+                            throw new Exception('Language name could not be found.');
128
+            }
129
+        } else {
130
+                    return $langCode;
127 131
         }
128
-        else
129
-            return $langCode;
130 132
     }
131 133
     /**
132 134
      * Returns the list of all supported languages
@@ -152,8 +154,7 @@  discard block
 block discarded – undo
152 154
         foreach($langs as $lang){
153 155
             try{
154 156
                 $translatedText['text'][$lang] = $this->client->translate($text,$lang);
155
-            }
156
-            catch(Exception $e){
157
+            } catch(Exception $e){
157 158
                 $translatedText['text'][$lang] = '';
158 159
             }
159 160
         }
@@ -208,10 +209,11 @@  discard block
 block discarded – undo
208 209
      * @return boolean
209 210
      */
210 211
     private function isSupportedLang(string $lang){
211
-        if(!in_array($lang,json_decode(json_encode($this->client->supportedLanguages),true)))
212
-            return false;
213
-        else
214
-            return true;
212
+        if(!in_array($lang,json_decode(json_encode($this->client->supportedLanguages),true))) {
213
+                    return false;
214
+        } else {
215
+                    return true;
216
+        }
215 217
     }
216 218
 
217 219
     
Please login to merge, or discard this patch.
src/MirzaServiceProvider.php 2 patches
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -16,7 +16,7 @@
 block discarded – undo
16 16
     {
17 17
 
18 18
         $this->publishes([
19
-			__DIR__.'/../config/mirza.php' => config_path('mirza.php'),
19
+            __DIR__.'/../config/mirza.php' => config_path('mirza.php'),
20 20
         ]);
21 21
         Blade::directive('translate', function ($expression){
22 22
             $expression = explode(',', $expression);
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -18,18 +18,18 @@  discard block
 block discarded – undo
18 18
         $this->publishes([
19 19
 			__DIR__.'/../config/mirza.php' => config_path('mirza.php'),
20 20
         ]);
21
-        Blade::directive('translate', function ($expression){
21
+        Blade::directive('translate', function($expression) {
22 22
             $expression = explode(',', $expression);
23 23
             $text = $expression[0];
24 24
             $lang = $expression[1];
25 25
 
26 26
             return "<?php echo Mirza::translate($text, $lang); ?>";
27 27
         });
28
-        Blade::directive('langselect', function (){
28
+        Blade::directive('langselect', function() {
29 29
             return "<?php echo Mirza::languages_select(); ?>";
30 30
         });
31
-        Blade::directive('yandex_rights',function($expression){
32
-            $expression = explode(',',$expression);
31
+        Blade::directive('yandex_rights', function($expression) {
32
+            $expression = explode(',', $expression);
33 33
             $color = $expression[0];
34 34
             $fontsize = $expression[1];
35 35
 
@@ -48,15 +48,15 @@  discard block
 block discarded – undo
48 48
             __DIR__.'/../config/mirza.php', 'mirza'
49 49
         );
50 50
 
51
-        \App::singleton('MirzaClient',function(){
51
+        \App::singleton('MirzaClient', function() {
52 52
             return new MirzaClient(config('mirza.secret'));
53 53
         });
54
-        \App::bind('Mirza',function(){
54
+        \App::bind('Mirza', function() {
55 55
             $client = resolve('MirzaClient');
56 56
             return new Mirza($client);
57 57
         });
58 58
 
59
-        \App::alias('Mirza','yak0d3\Mirza_Yandex_Translator\MirzaFacade');
59
+        \App::alias('Mirza', 'yak0d3\Mirza_Yandex_Translator\MirzaFacade');
60 60
 
61 61
     }
62 62
 }
Please login to merge, or discard this patch.
src/MirzaClient.php 2 patches
Spacing   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -14,7 +14,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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." Key: ".$key);
78 78
         }
79 79
         
@@ -87,18 +87,18 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
     }
Please login to merge, or discard this patch.
Braces   +15 added lines, -12 removed lines patch added patch discarded remove patch
@@ -98,10 +98,11 @@  discard block
 block discarded – undo
98 98
 
99 99
         $response = json_decode(curl_exec($ch));
100 100
         curl_close($ch);
101
-        if(array_key_exists('text',$response))
102
-            return $response->text[0];
103
-        else
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.');
101
+        if(array_key_exists('text',$response)) {
102
+                    return $response->text[0];
103
+        } else {
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.');
105
+        }
105 106
     }
106 107
     /**
107 108
      * Detects the language of a given text and returns the language code.
@@ -121,10 +122,11 @@  discard block
 block discarded – undo
121 122
         $response = json_decode(curl_exec($ch));
122 123
         curl_close($ch);
123 124
         
124
-        if(array_key_exists("lang",$response) && $response->lang != null)
125
-            return $response->lang;
126
-        else
127
-            throw new Exception('Could not get the language code: the entered string may not be valid.');
125
+        if(array_key_exists("lang",$response) && $response->lang != null) {
126
+                    return $response->lang;
127
+        } else {
128
+                    throw new Exception('Could not get the language code: the entered string may not be valid.');
129
+        }
128 130
     }
129 131
     /**
130 132
      * Returns the list of supported languages
@@ -144,10 +146,11 @@  discard block
 block discarded – undo
144 146
 
145 147
         $response = json_decode(curl_exec($ch));
146 148
         curl_close($ch);
147
-        if(array_key_exists("langs",$response))
148
-            return $codes ? array_keys(json_decode(json_encode($response->langs),true)) : $response->langs;
149
-        else
150
-            throw new Exception('An unknown error has occured while trying to fetch the list of supported languages.');
149
+        if(array_key_exists("langs",$response)) {
150
+                    return $codes ? array_keys(json_decode(json_encode($response->langs),true)) : $response->langs;
151
+        } else {
152
+                    throw new Exception('An unknown error has occured while trying to fetch the list of supported languages.');
153
+        }
151 154
     }
152 155
 
153 156
 }
Please login to merge, or discard this patch.