Completed
Push — master ( e3c46e...858b2e )
by Raed
02:21
created
src/Mirza.php 1 patch
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, $option);
185
-            $optionTemp = str_replace('_lang_name_',$langName, $optionTemp);
183
+        foreach ($this->client->supportedLanguages as $langCode => $langName) {
184
+            $optionTemp = str_replace('_lang_code_', $langCode, $option);
185
+            $optionTemp = str_replace('_lang_name_', $langName, $optionTemp);
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.