PercentDoubleCurlyBrackets::transform()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 2
rs 10
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
}