Passed
Push — master ( bbc68c...66b206 )
by Jeroen
03:18
created

RegexTextFilter::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Jerodev\Diglett\CssFilters;
4
5
use Symfony\Component\DomCrawler\Crawler;
6
7
class RegexTextFilter implements ICssFilter
8
{
9
    private $regex;
10
11
    public function __construct(array $parameters)
12
    {
13
        $this->regex = null;
14
        if (count($parameters) > 0) {
15
            $this->regex = $parameters[0];
16
        }
17
    }
18
19
    public static function getFunctionName(): string
20
    {
21
        return 'regextext';
22
    }
23
24
    public function filterNodes(Crawler $crawler): ?Crawler
25
    {
26
        if ($crawler->count() === 0) {
27
            return null;
28
        } elseif (empty($this->regex)) {
29
            return $crawler;
30
        } else {
31
            $regex = $this->regex;
32
            return $crawler->reduce(function ($node) use ($regex) {
33
                return preg_match("/$regex/", $node->text()) === 1;
34
            });
35
        }
36
    }
37
}