SmartCounts::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 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
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 SmartCounts extends AbstractHandler {
9
    /**
10
     * @param $segment
11
     *
12
     * @return string
13
     */
14 3
    public function transform( string $segment ): string {
15
        /*
16
         * Examples:
17
         * - [AIRBNB] Reminder: Reply to %{guest}’s inquiry. |||| [AIRBNB] Reminder: Reply to %{guest}’s inquiry.
18
         */
19 3
        preg_match_all( '/(\|\|\|\|)/', $segment, $html, PREG_SET_ORDER );
20 3
        foreach ( $html as $pos => $variable ) {
21
            //replace subsequent elements excluding already encoded
22 2
            $segment = preg_replace(
23 2
                    '/' . preg_quote( $variable[ 0 ], '/' ) . '/',
24 2
                    '<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::SMART_COUNT . '" equiv-text="base64:' . base64_encode( $variable[ 0 ] ) . "\"/>",
25 2
                    $segment,
26 2
                    1
27 2
            );
28
        }
29
30 3
        return $segment;
31
    }
32
}