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

ContainsTextFilter::__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 ContainsTextFilter implements ICssFilter
8
{
9
    private $text;
10
11
    public function __construct(array $parameters)
12
    {
13
        $this->text = null;
14
        if (count($parameters) > 0) {
15
            $this->text = $parameters[0];
16
        }
17
    }
18
19
    public static function getFunctionName(): string
20
    {
21
        return 'containstext';
22
    }
23
24
    public function filterNodes(Crawler $crawler): ?Crawler
25
    {
26
        if ($crawler->count() === 0) {
27
            return null;
28
        }
29
30
        if (empty($this->text)) {
31
            return $crawler;
32
        }
33
34
        $text = $this->text;
35
        return $crawler->reduce(function ($node) use ($text) {
36
            return strpos($node->text(), $text) !== false;
37
        });
38
    }
39
}