Passed
Push — master ( dc9630...ec0b89 )
by Jeroen
01:17
created

RegexTextFilter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getFunctionName() 0 3 1
A filterNodes() 0 15 3
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
        {
16
            $this->regex = $parameters[0];
17
        }
18
    }
19
20
    public static function getFunctionName(): string
21
    {
22
        return 'regextext';
23
    }
24
25
    public function filterNodes(Crawler $crawler): ?Crawler {
26
27
        if ($crawler->count() === 0)
28
        {
29
            return null;
30
        }
31
        elseif (empty($this->regex))
32
        {
33
            return $crawler;
34
        }
35
        else
36
        {
37
            $regex = $this->regex;
38
            return $crawler->reduce(function ($node) use ($regex) {
39
                return preg_match("/$regex/", $node->text()) === 1;
40
            });
41
        }
42
43
    }
44
}