NthFilter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 29
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
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 NthFilter implements ICssFilter
8
{
9
    private $parameters;
10
11
    public function __construct(array $parameters)
12
    {
13
        $this->parameters = $parameters;
14
    }
15
16
    public static function getFunctionName(): string
17
    {
18
        return 'nth';
19
    }
20
21
    public function filterNodes(Crawler $crawler): ?Crawler
22
    {
23
        // Nth needs one parameter
24
        if (count($this->parameters) === 0) {
25
            throw new \ErrorException(':nth(x) css selector should have at least one parameter');
26
        }
27
28
        // If not enough nodes in the list, return null
29
        $count = intval($this->parameters[0]);
30
        if ($crawler->count() < $count) {
31
            return null;
32
        }
33
34
        // Get the nth element
35
        return $crawler->eq($count - 1);
36
    }
37
}
38