Passed
Push — master ( 3a54e2...3f0976 )
by Domenico
02:14
created

PercentDoubleCurlyBrackets   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 24
ccs 10
cts 10
cp 1
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A transform() 0 17 2
1
<?php
2
3
namespace Matecat\SubFiltering\Filters;
4
5
use Matecat\SubFiltering\Commons\AbstractHandler;
6
use Matecat\SubFiltering\Enum\CTypeEnum;
7
8
class PercentDoubleCurlyBrackets extends AbstractHandler {
9
10
    /**
11
     * @param $segment
12
     *
13
     * @return string
14
     */
15 86
    public function transform( string $segment ): string {
16
        /*
17
         * Examples:
18
         * - %{{(text-align=center)}}
19
         */
20 86
        preg_match_all( '/%{{(?!<ph )[^{}]*?}}/', $segment, $html, PREG_SET_ORDER );
21 86
        foreach ( $html as $pos => $variable ) {
22
            //replace subsequent elements excluding already encoded
23 3
            $segment = preg_replace(
24 3
                    '/' . preg_quote( $variable[ 0 ], '/' ) . '/',
25 3
                    '<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::PERCENT_VARIABLE . '" equiv-text="base64:' . base64_encode( $variable[ 0 ] ) . "\"/>",
26 3
                    $segment,
27 3
                    1
28 3
            );
29
        }
30
31 86
        return $segment;
32
    }
33
}