SingleCurlyBracketsToPh   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 35
ccs 11
cts 11
cp 1
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 [email protected] / [email protected]
5
 * Date: 05/11/18
6
 * Time: 17.34
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 SingleCurlyBracketsToPh extends AbstractHandler {
17
18
    /**
19
     * TestSet:
20
     * <code>
21
     *  Dear {{%%customer.first_name%%}}, This is {{ "now"|date(null, "Europe/Rome") }} with {%%agent.alias%%} Airbnb. {% for user in users %} e {%%ciao%%}
22
     *  {# note: disabled template because we no longer use this
23
     *          {% for user in users %}
24
     *          ...
25
     *          {% endfor %}
26
     *
27
     * ... {variable}
28
     *  #}
29
     * </code>
30
     *
31
     * @param string $segment
32
     *
33
     * @return string
34
     */
35 2
    public function transform( string $segment ): string {
36 2
        preg_match_all( '/{[^<>{} ]+?}/', $segment, $html, PREG_SET_ORDER );
37 2
        foreach ( $html as $pos => $twig_variable ) {
38
            //check if inside twig variable there is a tag because in this case shouldn't replace the content with PH tag
39 2
            if ( !strstr( $twig_variable[ 0 ], ConstantEnum::GTPLACEHOLDER ) ) {
40
                //replace subsequent elements excluding already encoded
41 2
                $segment = preg_replace(
42 2
                        '/' . preg_quote( $twig_variable[ 0 ], '/' ) . '/',
43 2
                        '<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::CURLY_BRACKETS . '" equiv-text="base64:' . base64_encode( $twig_variable[ 0 ] ) . '"/>',
44 2
                        $segment,
45 2
                        1
46 2
                );
47
            }
48
        }
49
50 2
        return $segment;
51
    }
52
53
}