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

RegexTextFinder::find()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
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