Total Complexity | 7 |
Total Lines | 42 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
5 | class RegexTextFinder implements TextFinderInterface |
||
6 | { |
||
7 | private $patterns = []; |
||
8 | |||
9 | /** |
||
10 | * @param string $pattern |
||
11 | * @param int|null $textPosition position of text in pattern, use null to remove possible false positives only |
||
12 | * @return RegexTextFinder |
||
13 | */ |
||
14 | 16 | public function addPattern(string $pattern, ?int $textPosition = 1): RegexTextFinder |
|
15 | { |
||
16 | 16 | $this->patterns[$pattern] = $textPosition; |
|
17 | 16 | return $this; |
|
18 | } |
||
19 | |||
20 | 16 | public function find(string $content): array |
|
21 | { |
||
22 | 16 | $texts = []; |
|
23 | 16 | foreach ($this->patterns as $pattern => $textPosition) { |
|
24 | 14 | if ($textPosition === null) { |
|
25 | 2 | $content = preg_replace($pattern, '', $content); |
|
26 | 2 | continue; |
|
27 | } |
||
28 | 14 | $texts = array_merge($texts, $this->findTexts($pattern, $content, $textPosition)); |
|
29 | 14 | $content = preg_replace($pattern, '', $content); |
|
30 | } |
||
31 | 16 | return $texts; |
|
32 | } |
||
33 | |||
34 | 14 | private function findTexts(string $pattern, string $content, int $textPosition): array |
|
47 | } |
||
48 | } |
||
49 |