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

Chain   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 23
Bugs 0 Features 16
Metric Value
wmc 6
c 23
b 0
f 16
lcom 1
cbo 1
dl 0
loc 43
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B filter() 0 12 5
A addRule() 0 8 1
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