DataRefRestore   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 101
ccs 30
cts 30
cp 1
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 17 3
A restoreXliffPcTagsFromMatecatPhTags() 0 18 3
A __construct() 0 2 1
A restoreXliffPhTagsFromMatecatPhTags() 0 12 3
1
<?php
2
3
namespace Matecat\SubFiltering\Filters;
4
5
use Exception;
6
use Matecat\SubFiltering\Commons\AbstractHandler;
7
use Matecat\SubFiltering\Enum\CTypeEnum;
8
use Matecat\SubFiltering\Utils\DataRefReplacer;
9
10
class DataRefRestore extends AbstractHandler {
11
12
    /**
13
     * @var array
14
     */
15
    private array $dataRefMap = [];
16
17
    /**
18
     * DataRefReplace constructor.
19
     */
20 38
    public function __construct() {
21 38
        parent::__construct();
22
    }
23
24
    /**
25
     * @inheritDoc
26
     * @throws Exception
27
     */
28 38
    public function transform( string $segment ): string {
29
30 38
        if ( empty( $this->dataRefMap ) ) {
31 38
            $this->dataRefMap = $this->pipeline->getDataRefMap();
32
        }
33
34 38
        if ( empty( $this->dataRefMap ) ) {
35 33
            $segment = $this->restoreXliffPhTagsFromMatecatPhTags( $segment );
36
37 33
            return $this->restoreXliffPcTagsFromMatecatPhTags( $segment );
38
        }
39
40 5
        $dataRefReplacer = new DataRefReplacer( $this->dataRefMap );
41 5
        $segment         = $dataRefReplacer->restore( $segment );
42 5
        $segment         = $this->restoreXliffPhTagsFromMatecatPhTags( $segment );
43
44 5
        return $this->restoreXliffPcTagsFromMatecatPhTags( $segment );
45
    }
46
47
    /**
48
     * This function restores <ph> tags (encoded as Matecat ph tags) without any dataRef correspondence
49
     * for the persistence layer (layer 0).
50
     *
51
     * Example:
52
     *
53
     * Saame nähtavuse piirangutega kontrollida, kes sisu näeb .<ph id="mtc_ph_u_1" equiv-text="base64:Jmx0O3BoIGlkPSJzb3VyY2UxIiBkYXRhUmVmPSJzb3VyY2UxIi8mZ3Q7"/>
54
     *
55
     * is transformed to:
56
     *
57
     * Saame nähtavuse piirangutega kontrollida, kes sisu näeb .<ph id="source1" dataRef="source1"/>
58
     *
59
     * @param string $segment
60
     *
61
     * @return string
62
     */
63 38
    private function restoreXliffPhTagsFromMatecatPhTags( string $segment ): string {
64 38
        preg_match_all( '|<ph[^>]+? ctype="' . CTypeEnum::ORIGINAL_PH_OR_NOT_DATA_REF . '" equiv-text="base64:(.*?)"/>|iu', $segment, $matches );
65
66 38
        if ( empty( $matches[ 0 ] ) ) {
67 34
            return $segment;
68
        }
69
70 4
        foreach ( $matches[ 0 ] as $index => $match ) {
71 4
            $segment = str_replace( $match, base64_decode( $matches[ 1 ][ $index ] ), $segment );
72
        }
73
74 4
        return $segment;
75
    }
76
77
    /**
78
     * This function restores <pc> tags (encoded as Matecat ph tags) without any dataRef correspondence
79
     * for the persistence layer (layer 0).
80
     *
81
     * Example:
82
     *
83
     * Testo <ph id="source1_1" dataType="pcStart" originalData="Jmx0O3BjIGlkPSJzb3VyY2UxIiBkYXRhUmVmU3RhcnQ9InNvdXJjZTEiIGRhdGFSZWZFbmQ9InNvdXJjZTEiJmd0Ow==" dataRef="source1" equiv-text="base64:Xw=="/><ph id="mtc_u_1" equiv-text="base64:Jmx0O3BjIGlkPSIxdSIgdHlwZT0iZm10IiBzdWJUeXBlPSJtOnUiJmd0Ow=="/><ph id="mtc_u_2" equiv-text="base64:Jmx0Oy9wYyZndDs="/><ph id="source1_2" dataType="pcEnd" originalData="Jmx0Oy9wYyZndDs=" dataRef="source1" equiv-text="base64:Xw=="/>
84
     *
85
     * is transformed to:
86
     *
87
     * Testo <pc id="source1" dataRefStart="source1" dataRefEnd="source1"><pc id="1u" type="fmt" subType="m:u"></pc></pc>
88
     *
89
     * @param $segment
90
     *
91
     * @return string
92
     */
93 38
    private function restoreXliffPcTagsFromMatecatPhTags( string $segment ): string {
94
95 38
        $matches = [];
96 38
        preg_match_all( '|<ph[^>]+? ctype="' . CTypeEnum::ORIGINAL_PC_OPEN_NO_DATA_REF . '" equiv-text="base64:(.*?)"/>|iu', $segment, $open_matches );
97 38
        preg_match_all( '|<ph[^>]+? ctype="' . CTypeEnum::ORIGINAL_PC_CLOSE_NO_DATA_REF . '" equiv-text="base64:(.*?)"/>|iu', $segment, $close_matches );
98
99 38
        $matches[ 0 ] = array_merge( $open_matches[ 0 ], $close_matches[ 0 ] );
100 38
        $matches[ 1 ] = array_merge( $open_matches[ 1 ], $close_matches[ 1 ] );
101
102 38
        if ( empty( $matches[ 0 ] ) ) {
103 36
            return $segment;
104
        }
105
106 2
        foreach ( $matches[ 0 ] as $index => $match ) {
107 2
            $segment = str_replace( $match, base64_decode( $matches[ 1 ][ $index ] ), $segment );
108
        }
109
110 2
        return $segment;
111
    }
112
}