Completed
Pull Request — master (#37)
by Rick
04:08
created

Chain   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 22
Bugs 0 Features 16
Metric Value
wmc 6
c 22
b 0
f 16
lcom 1
cbo 1
dl 0
loc 42
ccs 11
cts 11
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B filter() 0 11 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 $isEmpty
27
     * @param mixed $value
28
     * @return FilterResult
29
     */
30 143
    public function filter($isEmpty, $value = null)
31
    {
32
        /** @var FilterRule $rule */
33 143
        foreach ($this->rules as $rule) {
34 143
            if (!$isEmpty || $isEmpty && $rule->allowEmpty()) {
35 143
                $value = $rule->filter($value);
36 143
            }
37 143
        }
38
39 143
        return new FilterResult($value === null, $value);
40
    }
41
42
    /**
43
     * Add a new rule to the chain
44
     *
45
     * @param FilterRule $rule
46
     * @param string|null $encodingFormat
47
     * @return $this
48
     */
49 143
    public function addRule(FilterRule $rule, $encodingFormat)
50
    {
51 143
        $rule->setEncodingFormat($encodingFormat);
52
53 143
        $this->rules[] = $rule;
54
55 143
        return $this;
56
    }
57
}
58