Completed
Pull Request — master (#43)
by Rick
04:01 queued 20s
created

Filter   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 98.39%

Importance

Changes 20
Bugs 1 Features 6
Metric Value
wmc 23
c 20
b 1
f 6
lcom 2
cbo 4
dl 0
loc 208
ccs 61
cts 62
cp 0.9839
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A value() 0 4 1
A values() 0 4 1
A all() 0 4 1
A getFilterResource() 0 4 1
A setEncodingFormat() 0 4 1
A addFilterRule() 0 4 1
A filter() 0 10 1
B filterGlobals() 0 21 5
A getFilterResult() 0 8 2
A filterChains() 0 14 3
A getChain() 0 13 3
A getGlobalChain() 0 7 2
A buildChain() 0 4 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 Particle\Filter\Value\Container;
12
13
/**
14
 * Class Filter
15
 *
16
 * @package Particle\Filter
17
 */
18
class Filter
19
{
20
    /**
21
     * @var array<string, Chain>
22
     */
23
    protected $chains = [];
24
25
    /**
26
     * @var Container
27
     */
28
    protected $data;
29
30
    /**
31
     * @var string|null
32
     */
33
    protected $encodingFormat = null;
34
35
    /**
36
     * @var Chain
37
     */
38
    protected $globalChain = null;
39
40
    /**
41
     * Set a filter for a value on a specific key
42
     *
43
     * @param string $key
44
     * @return FilterResource
45
     */
46 151
    public function value($key)
47
    {
48 151
        return $this->getFilterResource($key);
49
    }
50
51
    /**
52
     * Set a filter for the values on the given keys
53
     *
54
     * @param string[] $keys
55
     * @return FilterResource
56
     */
57 6
    public function values(array $keys)
58
    {
59 6
        return $this->getFilterResource($keys);
60
    }
61
62
    /**
63
     * Set a filter for all values off an array
64
     *
65
     * @return FilterResource
66
     */
67 2
    public function all()
68
    {
69 2
        return $this->getFilterResource();
70
    }
71
72
    /**
73
     * @param null|string|string[] $keys
74
     * @return FilterResource
75
     */
76 158
    public function getFilterResource($keys = null)
77
    {
78 158
        return new FilterResource($this, $keys);
79
    }
80
81
    /**
82
     * Set the encoding format for all string manipulating filter-rules.
83
     * Note: You should set the encoding format before you add filter-rules to your filter, otherwise the
84
     * encoding format would not be set on the values added before the encoding format was set.
85
     *
86
     * @param string $encodingFormat
87
     */
88 6
    public function setEncodingFormat($encodingFormat)
89
    {
90 6
        $this->encodingFormat = $encodingFormat;
91 6
    }
92
93
    /**
94
     * Set a filter rule on a chain
95
     *
96
     * @param FilterRule $rule
97
     * @param null|string $key
98
     */
99 157
    public function addFilterRule(FilterRule $rule, $key = null)
100
    {
101 157
        $this->getChain($key)->addRule($rule, $this->encodingFormat);
102 157
    }
103
104
    /**
105
     * Filter the provided data
106
     *
107
     * @param array $data
108
     * @return array
109
     */
110 159
    public function filter(array $data)
111
    {
112 159
        $data = $this->filterGlobals($data);
113
114 159
        $this->data = new Container($data);
115
116 159
        $this->filterChains();
117
118 159
        return $this->data->getArrayCopy();
119
    }
120
121
    /**
122
     * Filter all set fields with a global chain, recursively
123
     *
124
     * @param array $data
125
     * @return array
126
     */
127 159
    protected function filterGlobals(array $data)
128
    {
129 159
        if ($this->globalChain === null) {
130 157
            return $data;
131
        }
132
133 2
        foreach ($data as $key => $value) {
134 2
            if (is_array($value)) {
135 1
                $data[$key] = $this->filterGlobals($value);
136 1
            } else {
137 2
                $filterResult = $this->globalChain->filter(true, $value, $data);
138 2
                if ($filterResult->isNotEmpty()) {
139 2
                    $data[$key] = $filterResult->getFilteredValue();
140 2
                } else {
141
                    unset($data[$key]);
142
                }
143
            }
144 2
        }
145
146 2
        return $data;
147
    }
148
149
    /**
150
     * Get the filter result from a chain
151
     *
152
     * @param string $key
153
     * @param Chain $chain
154
     * @return FilterResult
155
     */
156 155
    protected function getFilterResult($key, Chain $chain)
157
    {
158 155
        if ($this->data->has($key)) {
159 144
            return $chain->filter(true, $this->data->get($key), $this->data->getArrayCopy());
160
        }
161
162 12
        return $chain->filter(false, null, $this->data->getArrayCopy());
163
    }
164
165
    /**
166
     * Filter all chains set
167
     */
168 159
    protected function filterChains()
169
    {
170 159
        foreach ($this->chains as $key => $chain) {
171 155
            $filterResult = $this->getFilterResult($key, $chain);
172 155
            if ($filterResult->isNotEmpty()) {
173 150
                $this->data->set(
174 150
                    $key,
175 150
                    $filterResult->getFilteredValue()
176 150
                );
177 150
            } else {
178 6
                $this->data->remove($key);
179
            }
180 159
        }
181 159
    }
182
183
    /**
184
     * Get a filter rule chain for a key
185
     *
186
     * @param null|string $key
187
     * @return Chain
188
     */
189 157
    protected function getChain($key)
190
    {
191
        // If no key, set global chain
192 157
        if ($key === null) {
193 2
            return $this->getGlobalChain();
194
        }
195
196
        // Return chain for key
197 155
        if (isset($this->chains[$key])) {
198 8
            return $this->chains[$key];
199
        }
200 155
        return $this->chains[$key] = $this->buildChain();
201
    }
202
203
    /**
204
     * Get the global chain for all values
205
     *
206
     * @return Chain
207
     */
208 2
    protected function getGlobalChain()
209
    {
210 2
        if ($this->globalChain === null) {
211 2
            $this->globalChain = $this->buildChain();
212 2
        }
213 2
        return $this->globalChain;
214
    }
215
216
    /**
217
     * Build a new chain of filters
218
     *
219
     * @return Chain
220
     */
221 157
    protected function buildChain()
222
    {
223 157
        return new Chain();
224
    }
225
}
226