DataRefRestore   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
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 100
ccs 31
cts 31
cp 1
rs 10
wmc 10

4 Methods

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