TwigToPh   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 TwigToPh 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 $segment
32
     *
33
     * @return string
34
     */
35 88
    public function transform( string $segment ): string {
36 88
        preg_match_all( '/{{[^<>!{}]+?}}|{%[^<>!%]+?%}|{#[^<>!#]+?#}/', $segment, $html, PREG_SET_ORDER );
37 88
        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 9
            if ( !strstr( $twig_variable[ 0 ], ConstantEnum::GTPLACEHOLDER ) ) {
40
                //replace subsequent elements excluding already encoded
41 9
                $segment = preg_replace(
42 9
                        '/' . preg_quote( $twig_variable[ 0 ], '/' ) . '/',
43 9
                        '<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::TWIG . '" equiv-text="base64:' . base64_encode( $twig_variable[ 0 ] ) . '"/>',
44 9
                        $segment,
45 9
                        1
46 9
                );
47
            }
48
        }
49
50 88
        return $segment;
51
    }
52
53
}