BasicStringFunction   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 21
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A applyFunction() 0 14 4
1
<?php
2
3
namespace Startwind\Forrest\Enrichment\EnrichFunction\String;
4
5
use Startwind\Forrest\Enrichment\EnrichFunction\StringEnrichFunction;
6
7
abstract class BasicStringFunction implements StringEnrichFunction
8
{
9
    protected string $functionName = '';
10
11
    public function applyFunction(string $string): string
12
    {
13
        if ($this->functionName == '') {
14
            throw new \RuntimeException('The function name must be set');
15
        }
16
17
        $pattern = '#' . preg_quote(StringEnrichFunction::FUNCTION_LIMITER_START) . $this->functionName . '\((.*?)\)' . preg_quote(StringEnrichFunction::FUNCTION_LIMITER_END) . '#';
18
        preg_match_all($pattern, $string, $matches);
19
        if (count($matches) > 0) {
20
            foreach ($matches[1] as $functionValue) {
21
                $string = str_replace(StringEnrichFunction::FUNCTION_LIMITER_START . $this->functionName . '(' . $functionValue . ')' . StringEnrichFunction::FUNCTION_LIMITER_END, $this->getValue($functionValue), $string);
22
            }
23
        }
24
        return $string;
25
    }
26
27
    abstract protected function getValue(string $value): string;
28
}
29