Completed
Pull Request — master (#43)
by Rick
91:22 queued 61:44
created

Chain   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 83.33%

Importance

Changes 26
Bugs 0 Features 18
Metric Value
wmc 6
c 26
b 0
f 18
lcom 1
cbo 1
dl 0
loc 55
ccs 15
cts 18
cp 0.8333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B filter() 0 22 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
use Exception;
11
12
/**
13
 * Class Chain
14
 *
15
 * @package Particle\Filter
16
 */
17
class Chain
18
{
19
    /**
20
     * @var FilterRule[]
21
     */
22
    protected $rules;
23
24
    /**
25
     * Execute all filters in the chain
26
     *
27
     * @param bool $isNotEmpty
28
     * @param mixed $value
29
     * @param array|null $filterData
30
     * @return FilterResult
31
     * @throws Exception
32
     */
33 163
    public function filter($isNotEmpty, $value = null, $filterData = null)
34
    {
35
        /** @var FilterRule $rule */
36 163
        foreach ($this->rules as $rule) {
37 163
            $rule->setFilterData($filterData);
38 163
            if ($isNotEmpty || $rule->allowedNotSet()) {
39 160
                $filterResult = $rule->filter($value);
40
41 160
                if (!$filterResult instanceof FilterResult) {
42
                    throw new Exception(
43
                        'A FilterResult object must be returned by the FilterRule->filter function.'
44
                        . ' got another value from ' . get_class($rule)
45
                    );
46
                }
47
48 160
                $isNotEmpty = $filterResult->isNotEmpty();
49 160
                $value = $filterResult->getFilteredValue();
50 160
            }
51 163
        }
52
53 163
        return new FilterResult($isNotEmpty, $value);
54
    }
55
56
    /**
57
     * Add a new rule to the chain
58
     *
59
     * @param FilterRule $rule
60
     * @param string|null $encodingFormat
61
     * @return $this
62
     */
63 163
    public function addRule(FilterRule $rule, $encodingFormat)
64
    {
65 163
        $rule->setEncodingFormat($encodingFormat);
66
67 163
        $this->rules[] = $rule;
68
69 163
        return $this;
70
    }
71
}
72