Passed
Pull Request — master (#51)
by Domenico
01:51
created

ICUVariables   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 10
c 1
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A transform() 0 16 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * @author Domenico Lupinetti (hashashiyyin) [email protected] / [email protected]
5
 * Date: 02/09/25
6
 * Time: 18:07
7
 *
8
 */
9
10
namespace Matecat\SubFiltering\Filters;
11
12
use Matecat\SubFiltering\Commons\AbstractHandler;
13
use Matecat\SubFiltering\Enum\ConstantEnum;
14
use Matecat\SubFiltering\Enum\CTypeEnum;
15
16
class ICUVariables extends AbstractHandler { // TODO
17
18
    /**
19
     * @inheritDoc
20
     */
21 1
    public function transform( string $segment ): string {
22 1
        preg_match_all( '/{.+}/', $segment, $text_content, PREG_SET_ORDER );
23 1
        foreach ( $text_content as $icu_variable ) {
24
            //check if inside the variable there is a tag because in this case shouldn't replace the content with PH tag
25 1
            if ( !strstr( $icu_variable[ 0 ], ConstantEnum::GTPLACEHOLDER ) ) {
26
                //replace subsequent elements excluding already encoded
27 1
                $segment = preg_replace(
28 1
                        '/' . preg_quote( $icu_variable[ 0 ], '/' ) . '/',
29 1
                        '<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::ICU . '" equiv-text="base64:' . base64_encode( $icu_variable[ 0 ] ) . '"/>',
30 1
                        $segment,
31 1
                        1
32 1
                );
33
            }
34
        }
35
36 1
        return $segment;
37
    }
38
}