AttributePreg::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 4
crap 6
1
<?php
2
3
namespace Thruster\Component\XMLIterator;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Class AttributePreg
9
 *
10
 * @package Thruster\Component\XMLIterator
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
class AttributePreg extends AttributeFilterBase
14
{
15
    /**
16
     * @var string
17
     */
18
    private $pattern;
19
20
    /**
21
     * @var bool
22
     */
23
    private $invert;
24
25
    /**
26
     * @param ElementIterator $elements
27
     * @param string          $attr    name of the attribute, '*' for every attribute
28
     * @param string          $pattern pcre based regex pattern for the attribute value
29
     * @param bool            $invert
30
     *
31
     * @throws InvalidArgumentException
32
     */
33
    public function __construct(ElementIterator $elements, string $attr, string $pattern, bool $invert = false)
34
    {
35
        parent::__construct($elements, $attr);
36
37
        if (false === preg_match("$pattern", '')) {
38
            throw new InvalidArgumentException("Invalid pcre pattern '$pattern'.");
39
        }
40
41
        $this->pattern = $pattern;
42
        $this->invert  = $invert;
43
    }
44
45
    public function accept()
46
    {
47
        return (bool) preg_grep($this->pattern, $this->getAttributeValues(), $this->invert ? PREG_GREP_INVERT : 0);
48
    }
49
}
50