Passed
Push — master ( 4e66a6...80b17a )
by Mauro
04:56
created

XliffSdl::createTargetTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * @author hashashiyyin [email protected] / [email protected]
5
 * Date: 02/08/24
6
 * Time: 19:04
7
 *
8
 */
9
10
namespace Matecat\XliffParser\XliffReplacer;
11
12
use Matecat\XliffParser\Utils\Strings;
13
14
class XliffSdl extends Xliff12 {
15
16
    /**
17
     * @var array
18
     */
19
    protected array $nodesToBuffer = [
20
        'source',
21
        'seg-source',
22
        'note',
23
        'context-group',
24
        'tag-defs',
25
        'cxt-defs'
26
    ];
27
28
    /**
29
     * @inheritDoc
30
     */
31
    protected function tagOpen( $parser, string $name, array $attr ) {
32
33
        $this->handleOpenUnit( $name, $attr );
34
35
        // check if we are entering into a <target>
36
        $this->checkSetInTarget( $name );
37
38
        // reset Marker positions
39
        if ( 'sdl:seg-defs' == $name ) {
40
            $this->segmentInUnitPosition = 0;
41
        }
42
43
        // open buffer
44
        $this->setInBuffer( $name );
45
46
        // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit
47
        // <target> must be stripped to be replaced, so this check avoids <target> reconstruction
48
        if ( !$this->inTarget ) {
49
50
            // costruct tag
51
            $tag = "<$name ";
52
53
            foreach ( $attr as $k => $v ) {
54
55
                // if tag name is file, we must replace the target-language attribute
56
                if ( $name == 'file' && $k == 'target-language' && !empty( $this->targetLang ) ) {
57
                    //replace Target language with job language provided from constructor
58
                    $tag .= "$k=\"$this->targetLang\" ";
59
                } else {
60
                    //normal tag flux, put attributes in it
61
                    // Warning, this is NOT an elseif
62
                    if ( $k != 'conf' ) {
63
                        //put also the current attribute in it if it is not a "conf" attribute
64
                        $tag .= "$k=\"$v\" ";
65
                    }
66
                }
67
            }
68
69
            $seg = $this->getCurrentSegment();
70
71
            if ( 'sdl:seg' == $name && !empty( $seg ) && isset( $seg[ 'sid' ] ) ) {
72
                $tag .= $this->prepareTargetStatuses( $seg );
73
            }
74
75
            $this->checkForSelfClosedTagAndFlush( $parser, $tag );
76
77
        }
78
79
    }
80
81
    /**
82
     * @param $segment
83
     *
84
     * @return string
85
     */
86
    protected function prepareTargetStatuses( $segment ): string {
87
        $statusMap = [
88
                'NEW'        => '',
89
                'DRAFT'      => 'Draft',
90
                'TRANSLATED' => 'Translated',
91
                'APPROVED'   => 'ApprovedTranslation',
92
                'APPROVED2'  => 'ApprovedSignOff',
93
                'REJECTED'   => 'RejectedTranslation',
94
        ];
95
96
        return "conf=\"{$statusMap[ $segment[ 'status' ] ]}\" ";
97
    }
98
99
    protected function rebuildMarks( array $seg, string $translation ): string {
100
101
        $trailingSpaces = str_repeat( ' ', Strings::getTheNumberOfTrailingSpaces( $translation ) );
102
103
        if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) {
104
            if ( $this->targetLang === 'ja-JP' ) {
105
                $seg[ 'mrk_succ_tags' ] = ltrim( $seg[ 'mrk_succ_tags' ] );
106
            }
107
108
            $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . rtrim( $translation ) . $seg[ 'mrk_succ_tags' ] . "</mrk>" . $trailingSpaces;
109
        }
110
111
        return $translation;
112
113
    }
114
115
    /**
116
     * This function creates a <target>
117
     *
118
     * @param string $translation
119
     * @param string $stateProp
120
     *
121
     * @return string
122
     */
123
    protected function createTargetTag( string $translation, string $stateProp ): string {
124
        $targetLang = ' xml:lang="' . $this->targetLang . '"';
125
126
        return "<target $targetLang $stateProp>$translation</target>";
127
    }
128
}