Chain   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 47
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 0 14 4
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-2016 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 191
    public function filter($isSet, $value = null, $filterData = null)
35
    {
36
        /** @var FilterRule $rule */
37 191
        foreach ($this->rules as $rule) {
38 191
            $rule->setFilterData($filterData);
39
40 191
            if ($isSet || $rule->allowedNotSet()) {
41 188
                $value = $rule->filter($value);
42 188
                $isSet = $rule->isNotEmpty();
43 188
            }
44 191
        }
45
46 191
        return new FilterResult($isSet, $value);
47
    }
48
49
    /**
50
     * Add a new rule to the chain
51
     *
52
     * @param FilterRule $rule
53
     * @param string|null $encodingFormat
54
     * @return $this
55
     */
56 191
    public function addRule(FilterRule $rule, $encodingFormat)
57
    {
58 191
        $rule->setEncodingFormat($encodingFormat);
59
60 191
        $this->rules[] = $rule;
61
62 191
        return $this;
63
    }
64
}
65