RubyOnRailsI18n   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 38
ccs 11
cts 11
cp 1
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A transform() 0 24 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 RubyOnRailsI18n extends AbstractHandler {
17
18
    /**
19
     * Support for ruby on rails i18n variables
20
     *
21
     * TestSet:
22
     * <code>
23
     *  Dear %{person}, This is %{agent.alias} from Customer. %{ this will not locked } e %{ciao}
24
     * </code>
25
     *
26
     * @param string $segment
27
     *
28
     * @return string
29
     */
30 86
    public function transform( string $segment ): string {
31
32
        /*
33
         * Examples:
34
         * - %{# }
35
         * - %{\n}$spaces=2%{\n}
36
         * - %{vars}
37
         *
38
         */
39 86
        preg_match_all( '/%{(?!<ph )[^{}]*?}/', $segment, $html, PREG_SET_ORDER );
40 86
        foreach ( $html as $pos => $ruby_variable ) {
41
            //check if inside twig variable there is a tag because in this case shouldn't replace the content with PH tag
42 3
            if ( !strstr( $ruby_variable[ 0 ], ConstantEnum::GTPLACEHOLDER ) ) {
43
                //replace subsequent elements excluding already encoded
44 3
                $segment = preg_replace(
45 3
                        '/' . preg_quote( $ruby_variable[ 0 ], '/' ) . '/',
46 3
                        '<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::RUBY_ON_RAILS . '" equiv-text="base64:' . base64_encode( $ruby_variable[ 0 ] ) . '"/>',
47 3
                        $segment,
48 3
                        1
49 3
                );
50
            }
51
        }
52
53 86
        return $segment;
54
    }
55
56
}