Completed
Pull Request — master (#43)
by Rick
44:56 queued 44:56
created

Chain::filter()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 11
Bugs 0 Features 4
Metric Value
c 11
b 0
f 4
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 8.8571
cc 6
eloc 9
nc 4
nop 3
crap 6
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
use Particle\Filter\Exception\ExpectFilterResultException;
12
13
/**
14
 * Class Chain
15
 *
16
 * @package Particle\Filter
17
 */
18
class Chain
19
{
20
    /**
21
     * @var FilterRule[]
22
     */
23
    protected $rules;
24
25
    /**
26
     * Execute all filters in the chain
27
     *
28
     * @param bool $isSet
29
     * @param mixed $value
30
     * @param array|null $filterData
31
     * @return FilterResult
32
     * @throws ExpectFilterResultException
33
     */
34 163
    public function filter($isSet, $value = null, $filterData = null)
35
    {
36
        /** @var FilterRule $rule */
37 163
        foreach ($this->rules as $rule) {
38 163
            $rule->setFilterData($filterData);
39 163
            if ($isSet || $rule->allowedNotSet()) {
40 160
                $value = $rule->filter($value);
41 160
                $isSet = true;
42
43 160
                if ($value === null && $rule->isEmpty()) {
44 6
                    $isSet = false;
45 6
                }
46 160
            }
47 163
        }
48
49 163
        return new FilterResult($isSet, $value);
50
    }
51
52
    /**
53
     * Add a new rule to the chain
54
     *
55
     * @param FilterRule $rule
56
     * @param string|null $encodingFormat
57
     * @return $this
58
     */
59 163
    public function addRule(FilterRule $rule, $encodingFormat)
60
    {
61 163
        $rule->setEncodingFormat($encodingFormat);
62
63 163
        $this->rules[] = $rule;
64
65 163
        return $this;
66
    }
67
}
68