Completed
Pull Request — master (#37)
by Rick
07:58 queued 02:10
created

Chain::filter()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 8.8571
cc 5
eloc 6
nc 3
nop 2
crap 5
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2015 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/Filter/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Filter;
10
11
/**
12
 * Class Chain
13
 *
14
 * @package Particle\Filter
15
 */
16
class Chain
17
{
18
    /**
19
     * @var FilterRule[]
20
     */
21
    protected $rules;
22
23
    /**
24
     * Execute all filters in the chain
25
     *
26
     * @param bool $isSet
27
     * @param mixed $value
28
     * @return FilterResult
29
     */
30 147
    public function filter($isSet, $value = null)
31
    {
32
        /** @var FilterRule $rule */
33 147
        foreach ($this->rules as $rule) {
34 147
            if ($isSet || !$isSet && $rule->allowedNotSet()) {
35 146
                $value = $rule->filter($value);
36 146
                $isSet = true;
37 146
            }
38 147
        }
39
40 147
        return new FilterResult($isSet, $value);
41
    }
42
43
    /**
44
     * Add a new rule to the chain
45
     *
46
     * @param FilterRule $rule
47
     * @param string|null $encodingFormat
48
     * @return $this
49
     */
50 147
    public function addRule(FilterRule $rule, $encodingFormat)
51
    {
52 147
        $rule->setEncodingFormat($encodingFormat);
53
54 147
        $this->rules[] = $rule;
55
56 147
        return $this;
57
    }
58
}
59