Completed
Pull Request — master (#43)
by Rick
117:24 queued 90:45
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
11
use Exception;
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 $isNotEmpty
29
     * @param mixed $value
30
     * @param array|null $filterData
31
     * @return FilterResult
32
     * @throws Exception
33
     */
34 163
    public function filter($isNotEmpty, $value = null, $filterData = null)
35
    {
36
        /** @var FilterRule $rule */
37 163
        foreach ($this->rules as $rule) {
38 163
            $rule->setFilterData($filterData);
39 163
            if ($isNotEmpty || $rule->allowedNotSet()) {
40 160
                $filterResult = $rule->filter($value);
41
42 160
                if (!$filterResult instanceof FilterResult) {
43
                    throw new Exception(
44
                        'A FilterResult object must be returned by the FilterRule->filter function.'
45
                        . ' got another value from ' . get_class($rule)
46
                    );
47
                }
48
49 160
                $isNotEmpty = $filterResult->isNotEmpty();
50 160
                $value = $filterResult->getFilteredValue();
51 160
            }
52 163
        }
53
54 163
        return new FilterResult($isNotEmpty, $value);
55
    }
56
57
    /**
58
     * Add a new rule to the chain
59
     *
60
     * @param FilterRule $rule
61
     * @param string|null $encodingFormat
62
     * @return $this
63
     */
64 163
    public function addRule(FilterRule $rule, $encodingFormat)
65
    {
66 163
        $rule->setEncodingFormat($encodingFormat);
67
68 163
        $this->rules[] = $rule;
69
70 163
        return $this;
71
    }
72
}
73