Completed
Push — master ( 216072...caf6f7 )
by Team eFabrica
02:21
created

RegexTextFinder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addPattern() 0 4 1
A find() 0 16 4
1
<?php
2
3
namespace Efabrica\TranslationsAutomatization\TextFinder;
4
5
class RegexTextFinder implements TextFinderInterface
6
{
7
    private $patterns = [];
8
9 6
    public function addPattern(string $pattern, int $textPosition = 1)
10
    {
11 6
        $this->patterns[$pattern] = $textPosition;
12 6
        return $this;
13
    }
14
15 6
    public function find(string $content): array
16
    {
17 6
        $texts = [];
18 6
        foreach ($this->patterns as $pattern => $textPosition) {
19 6
            preg_match_all($pattern, $content, $matches);
20 6
            $matchesCount = count($matches[0]);
21 6
            for ($i = 0; $i < $matchesCount; ++$i) {
22 6
                $text = trim($matches[$textPosition][$i]);
23 6
                if ($text === '') {
24 4
                    continue;
25
                }
26 6
                $texts[trim($matches[0][$i])] = $text;
27
            }
28
        }
29
30 6
        return $texts;
31
    }
32
}
33