@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | */ |
30 | 30 | public function __construct(string $style = 'HTML') |
31 | 31 | { |
32 | - $this->style = $style; |
|
32 | + $this->style = $style; |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | /** |
@@ -40,14 +40,14 @@ discard block |
||
40 | 40 | */ |
41 | 41 | public function decode($message): string |
42 | 42 | { |
43 | - if(!is_object($message)) |
|
43 | + if (!is_object($message)) |
|
44 | 44 | { |
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)) |
|
48 | + if (!empty($message->entities)) |
|
49 | 49 | $this->entities = $message->entities; |
50 | - if(!empty($message->caption_entities)) |
|
50 | + if (!empty($message->caption_entities)) |
|
51 | 51 | $this->entities = $message->caption_entities; |
52 | 52 | //Get internal encoding |
53 | 53 | $prevencoding = mb_internal_encoding(); |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | $textToDecode = (!empty($message->text) ? $message->text : (!empty($message->caption) ? $message->caption : "")); |
58 | 58 | //if the message has no entities or no text return the original text |
59 | 59 | if (empty($this->entities) || $textToDecode == "") { |
60 | - if($prevencoding) |
|
60 | + if ($prevencoding) |
|
61 | 61 | mb_internal_encoding($prevencoding); |
62 | 62 | return $textToDecode; |
63 | 63 | } |
@@ -68,13 +68,13 @@ discard block |
||
68 | 68 | $openedEntities = []; |
69 | 69 | $currenPosition = 0; |
70 | 70 | //Cycle characters one by one to calculate begins and ends of entities and escape special chars |
71 | - for($i = 0; $i < count($arrayText); $i++) { |
|
71 | + for ($i = 0; $i < count($arrayText); $i++) { |
|
72 | 72 | $offsetAndLength = $currenPosition + $arrayText[$i]['length']; |
73 | 73 | $entityCheckStart = $this->checkForEntityStart($currenPosition); |
74 | 74 | $entityCheckStop = $this->checkForEntityStop($offsetAndLength); |
75 | - if($entityCheckStart !== false) |
|
75 | + if ($entityCheckStart !== false) |
|
76 | 76 | { |
77 | - foreach($entityCheckStart as $stEntity) |
|
77 | + foreach ($entityCheckStart as $stEntity) |
|
78 | 78 | { |
79 | 79 | $startChar = $this->getEntityStartString($stEntity); |
80 | 80 | $openedEntities[] = $stEntity; |
@@ -82,40 +82,40 @@ discard block |
||
82 | 82 | } |
83 | 83 | $finalText .= $this->escapeSpecialChars($arrayText[$i]['char'], true, $openedEntities); |
84 | 84 | } |
85 | - if($entityCheckStop !== false) |
|
85 | + if ($entityCheckStop !== false) |
|
86 | 86 | { |
87 | - if($entityCheckStart === false) |
|
87 | + if ($entityCheckStart === false) |
|
88 | 88 | $finalText .= $this->escapeSpecialChars($arrayText[$i]['char'], true, $openedEntities); |
89 | - if($this->style == 'MarkdownV2' && $this->checkMarkdownV2AmbiguousEntities($entityCheckStop)) |
|
89 | + if ($this->style == 'MarkdownV2' && $this->checkMarkdownV2AmbiguousEntities($entityCheckStop)) |
|
90 | 90 | { |
91 | 91 | $stopChar = "_\r__"; |
92 | 92 | $finalText .= $stopChar; |
93 | 93 | array_pop($openedEntities); |
94 | 94 | array_pop($openedEntities); |
95 | 95 | } |
96 | - foreach($entityCheckStop as $stEntity) |
|
96 | + foreach ($entityCheckStop as $stEntity) |
|
97 | 97 | { |
98 | 98 | $stopChar = $this->getEntityStopString($stEntity); |
99 | 99 | $finalText .= $stopChar; |
100 | 100 | array_pop($openedEntities); |
101 | 101 | } |
102 | 102 | } |
103 | - if($entityCheckStart === false && $entityCheckStop === false) |
|
103 | + if ($entityCheckStart === false && $entityCheckStop === false) |
|
104 | 104 | { |
105 | 105 | $isEntityOpen = count($openedEntities) > 0; |
106 | 106 | $finalText .= $this->escapeSpecialChars($arrayText[$i]['char'], $isEntityOpen, $openedEntities); |
107 | 107 | } |
108 | 108 | $currenPosition = $offsetAndLength; |
109 | 109 | } |
110 | - if(count($openedEntities) > 0) |
|
110 | + if (count($openedEntities) > 0) |
|
111 | 111 | { |
112 | 112 | $openedEntities = array_reverse($openedEntities); |
113 | - foreach($openedEntities as $oe) |
|
113 | + foreach ($openedEntities as $oe) |
|
114 | 114 | { |
115 | 115 | $finalText .= $this->getEntityStopString($oe); |
116 | 116 | } |
117 | 117 | } |
118 | - if($prevencoding) |
|
118 | + if ($prevencoding) |
|
119 | 119 | mb_internal_encoding($prevencoding); |
120 | 120 | |
121 | 121 | return $finalText; |
@@ -130,36 +130,36 @@ discard block |
||
130 | 130 | $str_split_unicode = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY); |
131 | 131 | $new_string_split = []; |
132 | 132 | $joiner = false; |
133 | - for($i = 0; $i<count($str_split_unicode); $i++) //loop the array |
|
133 | + for ($i = 0; $i < count($str_split_unicode); $i++) //loop the array |
|
134 | 134 | { |
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 |
|
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 |
|
137 | 137 | { |
138 | - $new_string_split[count($new_string_split) - 1] .= $str_split_unicode[$i]; //Apppend the modifier to the previous char |
|
138 | + $new_string_split[count($new_string_split) - 1] .= $str_split_unicode[$i]; //Apppend the modifier to the previous char |
|
139 | 139 | } |
140 | 140 | else |
141 | 141 | { |
142 | - if($codepoint == "200d") //Manage the Zero Width Joiner |
|
142 | + if ($codepoint == "200d") //Manage the Zero Width Joiner |
|
143 | 143 | { |
144 | 144 | $new_string_split[count($new_string_split) - 1] .= $str_split_unicode[$i]; //Apppend the ZWJ to the previous char |
145 | 145 | $joiner = true; |
146 | 146 | } |
147 | 147 | else |
148 | 148 | { |
149 | - if($joiner) //If previous one was a ZWJ |
|
149 | + if ($joiner) //If previous one was a ZWJ |
|
150 | 150 | { |
151 | - $new_string_split[count($new_string_split) - 1] .= $str_split_unicode[$i]; //Apppend to the previous char |
|
151 | + $new_string_split[count($new_string_split) - 1] .= $str_split_unicode[$i]; //Apppend to the previous char |
|
152 | 152 | $joiner = false; |
153 | 153 | } |
154 | 154 | else |
155 | 155 | { |
156 | - $new_string_split[] = $str_split_unicode[$i]; //New char |
|
156 | + $new_string_split[] = $str_split_unicode[$i]; //New char |
|
157 | 157 | } |
158 | 158 | } |
159 | 159 | } |
160 | 160 | } |
161 | 161 | $data = []; |
162 | - foreach($new_string_split as $s) |
|
162 | + foreach ($new_string_split as $s) |
|
163 | 163 | { |
164 | 164 | $data[] = ["char" => $s, "length" => $this->getUTF16CodePointsLength($s)]; |
165 | 165 | } |
@@ -170,14 +170,14 @@ discard block |
||
170 | 170 | * Apply Telegram escape rules for the choosen style |
171 | 171 | */ |
172 | 172 | protected function escapeSpecialChars($char, $isEntityOpen, $entities) { |
173 | - if($this->style == 'Markdown') |
|
173 | + if ($this->style == 'Markdown') |
|
174 | 174 | { |
175 | - if($isEntityOpen) |
|
175 | + if ($isEntityOpen) |
|
176 | 176 | { |
177 | 177 | $entity = $entities[0]; |
178 | - if($char == '*' || $char == '_') |
|
178 | + if ($char == '*' || $char == '_') |
|
179 | 179 | { |
180 | - if($char == $this->getEntityStartString($entity)) |
|
180 | + if ($char == $this->getEntityStartString($entity)) |
|
181 | 181 | { |
182 | 182 | return $char."\\".$char.$char; |
183 | 183 | } |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | } |
194 | 194 | else |
195 | 195 | { |
196 | - if($char == '*' || $char == '_' || $char == '[' || $char == '`') |
|
196 | + if ($char == '*' || $char == '_' || $char == '[' || $char == '`') |
|
197 | 197 | { |
198 | 198 | return "\\".$char; |
199 | 199 | } |
@@ -203,11 +203,11 @@ discard block |
||
203 | 203 | } |
204 | 204 | } |
205 | 205 | } |
206 | - else if($this->style == 'HTML') |
|
206 | + else if ($this->style == 'HTML') |
|
207 | 207 | { |
208 | 208 | return ($char == '<' ? '<' : ($char == '>' ? '>' : ($char == '&' ? '&' : $char))); |
209 | 209 | } |
210 | - else if($this->style == 'MarkdownV2') |
|
210 | + else if ($this->style == 'MarkdownV2') |
|
211 | 211 | { |
212 | 212 | return (in_array($char, array('_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!', '\\')) ? '\\'.$char : $char); |
213 | 213 | } |
@@ -223,9 +223,9 @@ discard block |
||
223 | 223 | protected function getEntityStartString($entity) |
224 | 224 | { |
225 | 225 | $startString = ''; |
226 | - if($this->style == 'Markdown') |
|
226 | + if ($this->style == 'Markdown') |
|
227 | 227 | { |
228 | - switch($entity->type) |
|
228 | + switch ($entity->type) |
|
229 | 229 | { |
230 | 230 | case 'bold': |
231 | 231 | { |
@@ -245,7 +245,7 @@ discard block |
||
245 | 245 | case 'pre': |
246 | 246 | { |
247 | 247 | $startString = '```'; |
248 | - if(isset($entity->language)) |
|
248 | + if (isset($entity->language)) |
|
249 | 249 | { |
250 | 250 | $startString .= $entity->language; |
251 | 251 | } |
@@ -259,9 +259,9 @@ discard block |
||
259 | 259 | } |
260 | 260 | } |
261 | 261 | } |
262 | - else if($this->style == 'HTML') |
|
262 | + else if ($this->style == 'HTML') |
|
263 | 263 | { |
264 | - switch($entity->type) |
|
264 | + switch ($entity->type) |
|
265 | 265 | { |
266 | 266 | case 'bold': |
267 | 267 | { |
@@ -296,7 +296,7 @@ discard block |
||
296 | 296 | case 'pre': |
297 | 297 | { |
298 | 298 | $startString = '<pre>'; |
299 | - if(isset($entity->language)) |
|
299 | + if (isset($entity->language)) |
|
300 | 300 | { |
301 | 301 | $startString .= '<code class="language-'.$entity->language.'">'; |
302 | 302 | } |
@@ -314,9 +314,9 @@ discard block |
||
314 | 314 | } |
315 | 315 | } |
316 | 316 | } |
317 | - else if($this->style == 'MarkdownV2') |
|
317 | + else if ($this->style == 'MarkdownV2') |
|
318 | 318 | { |
319 | - switch($entity->type) |
|
319 | + switch ($entity->type) |
|
320 | 320 | { |
321 | 321 | case 'bold': |
322 | 322 | { |
@@ -341,7 +341,7 @@ discard block |
||
341 | 341 | case 'pre': |
342 | 342 | { |
343 | 343 | $startString = '```'; |
344 | - if(isset($entity->language)) |
|
344 | + if (isset($entity->language)) |
|
345 | 345 | { |
346 | 346 | $startString .= $entity->language; |
347 | 347 | } |
@@ -378,17 +378,17 @@ discard block |
||
378 | 378 | protected function checkForEntityStart($pos) |
379 | 379 | { |
380 | 380 | $entities = []; |
381 | - foreach($this->entities as $entity) |
|
381 | + foreach ($this->entities as $entity) |
|
382 | 382 | { |
383 | - if($entity->offset == $pos) |
|
383 | + if ($entity->offset == $pos) |
|
384 | 384 | { |
385 | - if(in_array($entity->type, array('bold', 'italic', 'code', 'pre', 'text_mention', 'text_link', 'strikethrough', 'underline', 'spoiler'))) |
|
385 | + if (in_array($entity->type, array('bold', 'italic', 'code', 'pre', 'text_mention', 'text_link', 'strikethrough', 'underline', 'spoiler'))) |
|
386 | 386 | { |
387 | 387 | $entities[] = $entity; |
388 | 388 | } |
389 | 389 | } |
390 | 390 | } |
391 | - if(count($entities) > 0) |
|
391 | + if (count($entities) > 0) |
|
392 | 392 | return $entities; |
393 | 393 | else |
394 | 394 | return false; |
@@ -400,9 +400,9 @@ discard block |
||
400 | 400 | protected function getEntityStopString($entity) |
401 | 401 | { |
402 | 402 | $stopString = ''; |
403 | - if($this->style == 'Markdown') |
|
403 | + if ($this->style == 'Markdown') |
|
404 | 404 | { |
405 | - switch($entity->type) |
|
405 | + switch ($entity->type) |
|
406 | 406 | { |
407 | 407 | case 'bold': |
408 | 408 | { |
@@ -436,9 +436,9 @@ discard block |
||
436 | 436 | } |
437 | 437 | } |
438 | 438 | } |
439 | - else if($this->style == 'HTML') |
|
439 | + else if ($this->style == 'HTML') |
|
440 | 440 | { |
441 | - switch($entity->type) |
|
441 | + switch ($entity->type) |
|
442 | 442 | { |
443 | 443 | case 'bold': |
444 | 444 | { |
@@ -472,7 +472,7 @@ discard block |
||
472 | 472 | } |
473 | 473 | case 'pre': |
474 | 474 | { |
475 | - if(isset($entity->language)) |
|
475 | + if (isset($entity->language)) |
|
476 | 476 | { |
477 | 477 | $stopString = '</code>'; |
478 | 478 | } |
@@ -487,9 +487,9 @@ discard block |
||
487 | 487 | } |
488 | 488 | } |
489 | 489 | } |
490 | - else if($this->style == 'MarkdownV2') |
|
490 | + else if ($this->style == 'MarkdownV2') |
|
491 | 491 | { |
492 | - switch($entity->type) |
|
492 | + switch ($entity->type) |
|
493 | 493 | { |
494 | 494 | case 'bold': |
495 | 495 | { |
@@ -551,17 +551,17 @@ discard block |
||
551 | 551 | protected function checkForEntityStop($pos) |
552 | 552 | { |
553 | 553 | $entities = []; |
554 | - foreach($this->entities as $entity) |
|
554 | + foreach ($this->entities as $entity) |
|
555 | 555 | { |
556 | - if($entity->offset + $entity->length == $pos) |
|
556 | + if ($entity->offset + $entity->length == $pos) |
|
557 | 557 | { |
558 | - if(in_array($entity->type, array('bold', 'italic', 'code', 'pre', 'text_mention', 'text_link', 'strikethrough', 'underline', 'spoiler'))) |
|
558 | + if (in_array($entity->type, array('bold', 'italic', 'code', 'pre', 'text_mention', 'text_link', 'strikethrough', 'underline', 'spoiler'))) |
|
559 | 559 | { |
560 | 560 | $entities[] = $entity; |
561 | 561 | } |
562 | 562 | } |
563 | 563 | } |
564 | - if(count($entities) > 0) |
|
564 | + if (count($entities) > 0) |
|
565 | 565 | return array_reverse($entities); |
566 | 566 | else |
567 | 567 | return false; |
@@ -575,19 +575,19 @@ discard block |
||
575 | 575 | $result = false; |
576 | 576 | $newEntities = []; |
577 | 577 | $foundIndex = 0; |
578 | - foreach($entitiesToCheck as $ec) |
|
578 | + foreach ($entitiesToCheck as $ec) |
|
579 | 579 | { |
580 | - if($ec->type == 'italic' || $ec->type == 'underline') |
|
580 | + if ($ec->type == 'italic' || $ec->type == 'underline') |
|
581 | 581 | { |
582 | 582 | $foundIndex++; |
583 | 583 | } |
584 | 584 | } |
585 | - if($foundIndex == 2) |
|
585 | + if ($foundIndex == 2) |
|
586 | 586 | { |
587 | 587 | $result = true; |
588 | - foreach($entitiesToCheck as $ec) |
|
588 | + foreach ($entitiesToCheck as $ec) |
|
589 | 589 | { |
590 | - if($ec->type != 'italic' && $ec->type != 'underline') |
|
590 | + if ($ec->type != 'italic' && $ec->type != 'underline') |
|
591 | 591 | { |
592 | 592 | $newEntities[] = $ec; |
593 | 593 | } |