Passed
Pull Request — master (#51)
by Domenico
16:08 queued 13:41
created

ICUVariables   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 21
ccs 0
cts 11
cp 0
rs 10
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 {
17
18
    /**
19
     * @inheritDoc
20
     */
21
    public function transform( string $segment ): string {
22
        preg_match_all( '/\{(?!<ph )[^{}]+?}/', $segment, $text_content, PREG_SET_ORDER );
23
        foreach ( $text_content as $icul_variable ) {
24
            //check if inside the variable there is a tag because in this case shouldn't replace the content with PH tag
25
            if ( !strstr( $icul_variable[ 0 ], ConstantEnum::GTPLACEHOLDER ) ) {
26
                //replace subsequent elements excluding already encoded
27
                $segment = preg_replace(
28
                        '/' . preg_quote( $icul_variable[ 0 ], '/' ) . '/',
29
                        '<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::ICU . '" equiv-text="base64:' . base64_encode( $icul_variable[ 0 ] ) . '"/>',
30
                        $segment,
31
                        1
32
                );
33
            }
34
        }
35
36
        return $segment;
37
    }
38
}