|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* @author domenico [email protected] / [email protected] |
|
5
|
|
|
* Date: 11/01/19 |
|
6
|
|
|
* Time: 15.11 |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Matecat\SubFiltering\Filters; |
|
11
|
|
|
|
|
12
|
|
|
use Matecat\SubFiltering\Commons\AbstractHandler; |
|
13
|
|
|
use Matecat\SubFiltering\Enum\CTypeEnum; |
|
14
|
|
|
|
|
15
|
|
|
class StandardPHToMateCatCustomPH extends AbstractHandler { |
|
16
|
|
|
|
|
17
|
82 |
|
public function transform( string $segment ): string { |
|
18
|
|
|
|
|
19
|
82 |
|
$segment = $this->filterPhTagContent( $segment ); |
|
20
|
|
|
|
|
21
|
82 |
|
return $this->filterOriginalSelfClosePhTagsWithEquivText( $segment ); |
|
22
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $segment |
|
27
|
|
|
* |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
82 |
|
private function filterPhTagContent( string $segment ): string { |
|
31
|
|
|
|
|
32
|
82 |
|
if ( preg_match( '|</ph>|s', $segment ) ) { |
|
33
|
1 |
|
preg_match_all( '|<ph id=["\']([^\'"]+?)["\'].*?>(.*?)</ph>|', $segment, $phTags, PREG_SET_ORDER ); |
|
34
|
1 |
|
foreach ( $phTags as $group ) { |
|
35
|
1 |
|
$segment = preg_replace( |
|
36
|
1 |
|
'/' . preg_quote( $group[ 0 ], '/' ) . '/', |
|
37
|
1 |
|
'<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::ORIGINAL_PH_CONTENT . '" x-orig="' . base64_encode( $group[ 0 ] ) . '" equiv-text="base64:' . |
|
38
|
1 |
|
base64_encode( htmlentities( $group[ 2 ], ENT_NOQUOTES | 16 /* ENT_XML1 */, 'UTF-8' ) ) . |
|
39
|
1 |
|
'"/>', |
|
40
|
1 |
|
$segment, |
|
41
|
1 |
|
1 |
|
42
|
1 |
|
); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
82 |
|
return $segment; |
|
47
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Show the equivalent text of the <ph> tag instead of the tag itself. |
|
52
|
|
|
* |
|
53
|
|
|
* @param $segment |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
82 |
|
private function filterOriginalSelfClosePhTagsWithEquivText( string $segment ): string { |
|
58
|
|
|
|
|
59
|
82 |
|
preg_match_all( '|<ph[^>]+?equiv-text\s*?=\s*?(["\'])(?!base64:)(.*?)?\1[^>]*?/>|', $segment, $html, PREG_SET_ORDER ); |
|
60
|
82 |
|
foreach ( $html as $tag_attribute ) { |
|
61
|
|
|
|
|
62
|
|
|
//replace subsequent elements excluding already encoded |
|
63
|
5 |
|
$segment = preg_replace( |
|
64
|
5 |
|
'/' . preg_quote( $tag_attribute[ 0 ], '/' ) . '/', |
|
65
|
5 |
|
'<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::ORIGINAL_SELF_CLOSE_PH_WITH_EQUIV_TEXT . '" x-orig="' . base64_encode( $tag_attribute[ 0 ] ) . '" equiv-text="base64:' . |
|
66
|
5 |
|
base64_encode( $tag_attribute[ 2 ] != '' ? $tag_attribute[ 2 ] : 'NULL' ) . |
|
67
|
5 |
|
'"/>', |
|
68
|
5 |
|
$segment, |
|
69
|
5 |
|
1 |
|
70
|
5 |
|
); |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
82 |
|
return $segment; |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
|