Completed
Push — master ( b133bf...737a42 )
by Rick
04:48
created

Filter::getFilterResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 2
crap 2
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 141
    public function value($key)
47
    {
48 141
        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 148
    public function getFilterResource($keys = null)
77
    {
78 148
        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 147
    public function addFilterRule(FilterRule $rule, $key = null)
100
    {
101 147
        $this->getChain($key)->addRule($rule, $this->encodingFormat);
102 147
    }
103
104
    /**
105
     * Filter the provided data
106
     *
107
     * @param array $data
108
     * @return array
109
     */
110 149
    public function filter(array $data)
111
    {
112 149
        $data = $this->filterGlobals($data);
113
114 149
        $this->data = new Container($data);
115
116 149
        $this->filterChains();
117
118 149
        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 149
    protected function filterGlobals(array $data)
128
    {
129 149
        if ($this->globalChain === null) {
130 147
            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);
138 2
                $data[$key] = $filterResult->getFilteredValue();
139
            }
140 2
        }
141
142 2
        return $data;
143
    }
144
145
    /**
146
     * Get the filter result from a chain
147
     *
148
     * @param string $key
149
     * @param Chain $chain
150
     * @return FilterResult
151
     */
152 145
    protected function getFilterResult($key, Chain $chain)
153
    {
154 145
        if ($this->data->has($key)) {
155 136
            return $chain->filter(true, $this->data->get($key));
156
        }
157
158 10
        return $chain->filter(false);
159
    }
160
161
    /**
162
     * Filter all chains set
163
     */
164 149
    protected function filterChains()
165
    {
166 149
        foreach ($this->chains as $key => $chain) {
167 145
            $filterResult = $this->getFilterResult($key, $chain);
168 145
            if ($filterResult->isNotEmpty()) {
169 144
                $this->data->set(
170 144
                    $key,
171 144
                    $filterResult->getFilteredValue()
172 144
                );
173 144
            }
174 149
        }
175 149
    }
176
177
    /**
178
     * Get a filter rule chain for a key
179
     *
180
     * @param null|string $key
181
     * @return Chain
182
     */
183 147
    protected function getChain($key)
184
    {
185
        // If no key, set global chain
186 147
        if ($key === null) {
187 2
            return $this->getGlobalChain();
188
        }
189
190
        // Return chain for key
191 145
        if (isset($this->chains[$key])) {
192 8
            return $this->chains[$key];
193
        }
194 145
        return $this->chains[$key] = $this->buildChain();
195
    }
196
197
    /**
198
     * Get the global chain for all values
199
     *
200
     * @return Chain
201
     */
202 2
    protected function getGlobalChain()
203
    {
204 2
        if ($this->globalChain === null) {
205 2
            $this->globalChain = $this->buildChain();
206 2
        }
207 2
        return $this->globalChain;
208
    }
209
210
    /**
211
     * Build a new chain of filters
212
     *
213
     * @return Chain
214
     */
215 147
    protected function buildChain()
216
    {
217 147
        return new Chain();
218
    }
219
}
220