Filter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Ionut\Sylar\Filters\PHPIDS;
4
5
use Ionut\Sylar\Filters\FilterInterface;
6
use Ionut\Sylar\Filters\FilterReportInterface;
7
use Ionut\Sylar\NormalizedValueVariant;
8
9
class Filter implements FilterInterface
10
{
11
12
    /**
13
     * @var array
14
     */
15
    protected $filters;
16
17
    public function __construct()
18
    {
19
        $contents = json_decode(file_get_contents(__DIR__.'/_filters.json'));
20
        $this->filters = $contents->filters->filter;
21
    }
22
23
    /**
24
     * Check a given value against this set of filters.
25
     *
26
     * @param NormalizedValueVariant|string $value
27
     * @return FilterReportInterface|null
28
     */
29
    public function matches(NormalizedValueVariant $value)
30
    {
31
        foreach ($this->filters as $filter) {
32
            if ($this->checkAgainstRule($filter->rule, $value->getValue())) {
33
                return new Report($filter, $value);
34
            }
35
        }
36
    }
37
38
    /**
39
     * @param  string $rule
40
     * @param  string $value
41
     * @return boolean
42
     */
43
    protected function checkAgainstRule($rule, $value)
44
    {
45
        return preg_match('/'.$rule.'/', $value);
46
    }
47
}