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

Chain::addRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 3
Metric Value
c 4
b 0
f 3
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 4
nc 1
nop 2
crap 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