@@ -24,9 +24,9 @@ discard block |
||
| 24 | 24 | private $entities = false; |
| 25 | 25 | private $style; |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @param string $style Either 'HTML', 'Markdown' or 'MarkdownV2'. |
|
| 29 | - */ |
|
| 27 | + /** |
|
| 28 | + * @param string $style Either 'HTML', 'Markdown' or 'MarkdownV2'. |
|
| 29 | + */ |
|
| 30 | 30 | public function __construct(string $style = 'HTML') |
| 31 | 31 | { |
| 32 | 32 | $this->style = $style; |
@@ -384,7 +384,7 @@ discard block |
||
| 384 | 384 | { |
| 385 | 385 | if(in_array($entity->type, array('bold', 'italic', 'code', 'pre', 'text_mention', 'text_link', 'strikethrough', 'underline', 'spoiler'))) |
| 386 | 386 | { |
| 387 | - $entities[] = $entity; |
|
| 387 | + $entities[] = $entity; |
|
| 388 | 388 | } |
| 389 | 389 | } |
| 390 | 390 | } |
@@ -45,10 +45,12 @@ discard block |
||
| 45 | 45 | throw new Exception('message must be an object'); |
| 46 | 46 | } |
| 47 | 47 | //Get available entities (for text or for attachment like photo, document, etc.) |
| 48 | - if(!empty($message->entities)) |
|
| 49 | - $this->entities = $message->entities; |
|
| 50 | - if(!empty($message->caption_entities)) |
|
| 51 | - $this->entities = $message->caption_entities; |
|
| 48 | + if(!empty($message->entities)) { |
|
| 49 | + $this->entities = $message->entities; |
|
| 50 | + } |
|
| 51 | + if(!empty($message->caption_entities)) { |
|
| 52 | + $this->entities = $message->caption_entities; |
|
| 53 | + } |
|
| 52 | 54 | //Get internal encoding |
| 53 | 55 | $prevencoding = mb_internal_encoding(); |
| 54 | 56 | //Set encoding to UTF-8 |
@@ -57,8 +59,9 @@ discard block |
||
| 57 | 59 | $textToDecode = (!empty($message->text) ? $message->text : (!empty($message->caption) ? $message->caption : "")); |
| 58 | 60 | //if the message has no entities or no text return the original text |
| 59 | 61 | if (empty($this->entities) || $textToDecode == "") { |
| 60 | - if($prevencoding) |
|
| 61 | - mb_internal_encoding($prevencoding); |
|
| 62 | + if($prevencoding) { |
|
| 63 | + mb_internal_encoding($prevencoding); |
|
| 64 | + } |
|
| 62 | 65 | return $textToDecode; |
| 63 | 66 | } |
| 64 | 67 | //split text in char array with UTF-16 code units length |
@@ -84,8 +87,9 @@ discard block |
||
| 84 | 87 | } |
| 85 | 88 | if($entityCheckStop !== false) |
| 86 | 89 | { |
| 87 | - if($entityCheckStart === false) |
|
| 88 | - $finalText .= $this->escapeSpecialChars($arrayText[$i]['char'], true, $openedEntities); |
|
| 90 | + if($entityCheckStart === false) { |
|
| 91 | + $finalText .= $this->escapeSpecialChars($arrayText[$i]['char'], true, $openedEntities); |
|
| 92 | + } |
|
| 89 | 93 | if($this->style == 'MarkdownV2' && $this->checkMarkdownV2AmbiguousEntities($entityCheckStop)) |
| 90 | 94 | { |
| 91 | 95 | $stopChar = "_\r__"; |
@@ -115,8 +119,9 @@ discard block |
||
| 115 | 119 | $finalText .= $this->getEntityStopString($oe); |
| 116 | 120 | } |
| 117 | 121 | } |
| 118 | - if($prevencoding) |
|
| 119 | - mb_internal_encoding($prevencoding); |
|
| 122 | + if($prevencoding) { |
|
| 123 | + mb_internal_encoding($prevencoding); |
|
| 124 | + } |
|
| 120 | 125 | |
| 121 | 126 | return $finalText; |
| 122 | 127 | } |
@@ -130,28 +135,37 @@ discard block |
||
| 130 | 135 | $str_split_unicode = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY); |
| 131 | 136 | $new_string_split = []; |
| 132 | 137 | $joiner = false; |
| 133 | - for($i = 0; $i<count($str_split_unicode); $i++) //loop the array |
|
| 138 | + for($i = 0; $i<count($str_split_unicode); $i++) { |
|
| 139 | + //loop the array |
|
| 134 | 140 | { |
| 135 | - $codepoint = bin2hex(mb_convert_encoding($str_split_unicode[$i], 'UTF-16')); //Get the string rappresentation of the unicode char |
|
| 136 | - if($codepoint == "fe0f" || $codepoint == "1f3fb" || $codepoint == "1f3fc" || $codepoint == "1f3fd" || $codepoint == "1f3fe" || $codepoint == "1f3ff") //Manage the modifiers |
|
| 141 | + $codepoint = bin2hex(mb_convert_encoding($str_split_unicode[$i], 'UTF-16')); |
|
| 142 | + } |
|
| 143 | + //Get the string rappresentation of the unicode char |
|
| 144 | + if($codepoint == "fe0f" || $codepoint == "1f3fb" || $codepoint == "1f3fc" || $codepoint == "1f3fd" || $codepoint == "1f3fe" || $codepoint == "1f3ff") { |
|
| 145 | + //Manage the modifiers |
|
| 137 | 146 | { |
| 138 | - $new_string_split[count($new_string_split) - 1] .= $str_split_unicode[$i]; //Apppend the modifier to the previous char |
|
| 147 | + $new_string_split[count($new_string_split) - 1] .= $str_split_unicode[$i]; |
|
| 139 | 148 | } |
| 140 | - else |
|
| 149 | + //Apppend the modifier to the previous char |
|
| 150 | + } else |
|
| 141 | 151 | { |
| 142 | - if($codepoint == "200d") //Manage the Zero Width Joiner |
|
| 152 | + if($codepoint == "200d") { |
|
| 153 | + //Manage the Zero Width Joiner |
|
| 143 | 154 | { |
| 144 | - $new_string_split[count($new_string_split) - 1] .= $str_split_unicode[$i]; //Apppend the ZWJ to the previous char |
|
| 145 | - $joiner = true; |
|
| 155 | + $new_string_split[count($new_string_split) - 1] .= $str_split_unicode[$i]; |
|
| 146 | 156 | } |
| 147 | - else |
|
| 157 | + //Apppend the ZWJ to the previous char |
|
| 158 | + $joiner = true; |
|
| 159 | + } else |
|
| 148 | 160 | { |
| 149 | - if($joiner) //If previous one was a ZWJ |
|
| 161 | + if($joiner) { |
|
| 162 | + //If previous one was a ZWJ |
|
| 150 | 163 | { |
| 151 | - $new_string_split[count($new_string_split) - 1] .= $str_split_unicode[$i]; //Apppend to the previous char |
|
| 152 | - $joiner = false; |
|
| 164 | + $new_string_split[count($new_string_split) - 1] .= $str_split_unicode[$i]; |
|
| 153 | 165 | } |
| 154 | - else |
|
| 166 | + //Apppend to the previous char |
|
| 167 | + $joiner = false; |
|
| 168 | + } else |
|
| 155 | 169 | { |
| 156 | 170 | $new_string_split[] = $str_split_unicode[$i]; //New char |
| 157 | 171 | } |
@@ -180,38 +194,31 @@ discard block |
||
| 180 | 194 | if($char == $this->getEntityStartString($entity)) |
| 181 | 195 | { |
| 182 | 196 | return $char."\\".$char.$char; |
| 183 | - } |
|
| 184 | - else |
|
| 197 | + } else |
|
| 185 | 198 | { |
| 186 | 199 | return $char; |
| 187 | 200 | } |
| 188 | - } |
|
| 189 | - else |
|
| 201 | + } else |
|
| 190 | 202 | { |
| 191 | 203 | return $char; |
| 192 | 204 | } |
| 193 | - } |
|
| 194 | - else |
|
| 205 | + } else |
|
| 195 | 206 | { |
| 196 | 207 | if($char == '*' || $char == '_' || $char == '[' || $char == '`') |
| 197 | 208 | { |
| 198 | 209 | return "\\".$char; |
| 199 | - } |
|
| 200 | - else |
|
| 210 | + } else |
|
| 201 | 211 | { |
| 202 | 212 | return $char; |
| 203 | 213 | } |
| 204 | 214 | } |
| 205 | - } |
|
| 206 | - else if($this->style == 'HTML') |
|
| 215 | + } else if($this->style == 'HTML') |
|
| 207 | 216 | { |
| 208 | 217 | return ($char == '<' ? '<' : ($char == '>' ? '>' : ($char == '&' ? '&' : $char))); |
| 209 | - } |
|
| 210 | - else if($this->style == 'MarkdownV2') |
|
| 218 | + } else if($this->style == 'MarkdownV2') |
|
| 211 | 219 | { |
| 212 | 220 | return (in_array($char, array('_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!', '\\')) ? '\\'.$char : $char); |
| 213 | - } |
|
| 214 | - else |
|
| 221 | + } else |
|
| 215 | 222 | { |
| 216 | 223 | return $char; |
| 217 | 224 | } |
@@ -258,8 +265,7 @@ discard block |
||
| 258 | 265 | break; |
| 259 | 266 | } |
| 260 | 267 | } |
| 261 | - } |
|
| 262 | - else if($this->style == 'HTML') |
|
| 268 | + } else if($this->style == 'HTML') |
|
| 263 | 269 | { |
| 264 | 270 | switch($entity->type) |
| 265 | 271 | { |
@@ -313,8 +319,7 @@ discard block |
||
| 313 | 319 | break; |
| 314 | 320 | } |
| 315 | 321 | } |
| 316 | - } |
|
| 317 | - else if($this->style == 'MarkdownV2') |
|
| 322 | + } else if($this->style == 'MarkdownV2') |
|
| 318 | 323 | { |
| 319 | 324 | switch($entity->type) |
| 320 | 325 | { |
@@ -364,8 +369,7 @@ discard block |
||
| 364 | 369 | break; |
| 365 | 370 | } |
| 366 | 371 | } |
| 367 | - } |
|
| 368 | - else |
|
| 372 | + } else |
|
| 369 | 373 | { |
| 370 | 374 | //Exception |
| 371 | 375 | } |
@@ -388,10 +392,11 @@ discard block |
||
| 388 | 392 | } |
| 389 | 393 | } |
| 390 | 394 | } |
| 391 | - if(count($entities) > 0) |
|
| 392 | - return $entities; |
|
| 393 | - else |
|
| 394 | - return false; |
|
| 395 | + if(count($entities) > 0) { |
|
| 396 | + return $entities; |
|
| 397 | + } else { |
|
| 398 | + return false; |
|
| 399 | + } |
|
| 395 | 400 | } |
| 396 | 401 | |
| 397 | 402 | /** |
@@ -435,8 +440,7 @@ discard block |
||
| 435 | 440 | break; |
| 436 | 441 | } |
| 437 | 442 | } |
| 438 | - } |
|
| 439 | - else if($this->style == 'HTML') |
|
| 443 | + } else if($this->style == 'HTML') |
|
| 440 | 444 | { |
| 441 | 445 | switch($entity->type) |
| 442 | 446 | { |
@@ -486,8 +490,7 @@ discard block |
||
| 486 | 490 | break; |
| 487 | 491 | } |
| 488 | 492 | } |
| 489 | - } |
|
| 490 | - else if($this->style == 'MarkdownV2') |
|
| 493 | + } else if($this->style == 'MarkdownV2') |
|
| 491 | 494 | { |
| 492 | 495 | switch($entity->type) |
| 493 | 496 | { |
@@ -537,8 +540,7 @@ discard block |
||
| 537 | 540 | break; |
| 538 | 541 | } |
| 539 | 542 | } |
| 540 | - } |
|
| 541 | - else |
|
| 543 | + } else |
|
| 542 | 544 | { |
| 543 | 545 | //Exception |
| 544 | 546 | } |
@@ -561,10 +563,11 @@ discard block |
||
| 561 | 563 | } |
| 562 | 564 | } |
| 563 | 565 | } |
| 564 | - if(count($entities) > 0) |
|
| 565 | - return array_reverse($entities); |
|
| 566 | - else |
|
| 567 | - return false; |
|
| 566 | + if(count($entities) > 0) { |
|
| 567 | + return array_reverse($entities); |
|
| 568 | + } else { |
|
| 569 | + return false; |
|
| 570 | + } |
|
| 568 | 571 | } |
| 569 | 572 | |
| 570 | 573 | /** |