ContainsTextFilter::filterNodes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
eloc 7
c 2
b 1
f 0
nc 3
nop 1
dl 0
loc 14
rs 10
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
36
        return $crawler->reduce(function ($node) use ($text) {
37
            return strpos($node->text(), $text) !== false;
38
        });
39
    }
40
}
41