Completed
Pull Request — master (#43)
by Rick
27:38 queued 27:38
created

FilterResource::upper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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
/**
12
 * Class FilterResource
13
 * @package Particle\Filter
14
 */
15
class FilterResource
16
{
17
    /**
18
     * @var Filter
19
     */
20
    protected $filter;
21
22
    /**
23
     * @var null|string|string[]
24
     */
25
    protected $keys;
26
27
    /**
28
     * @param Filter $filter
29
     * @param null|string|string[] $keys
30
     */
31 162
    public function __construct(Filter $filter, $keys = null)
32
    {
33 162
        $this->filter = $filter;
34 162
        $this->keys = $keys;
35 162
    }
36
37
    /**
38
     * Results rule that returns alphabetic numeric characters from the value
39
     *
40
     * @return $this
41
     */
42 4
    public function alnum()
43
    {
44 4
        return $this->addRule(new FilterRule\AlNum);
45
    }
46
47
    /**
48
     * Results rule that returns the value appended with a given value
49
     *
50
     * @param string $append
51
     * @return Chain
52
     */
53 4
    public function append($append)
54
    {
55 4
        return $this->addRule(new FilterRule\Append($append));
56
    }
57
58
    /**
59
     * Results rule that returns a casted boolean
60
     *
61
     * @return $this
62
     */
63 16
    public function bool()
64
    {
65 16
        return $this->addRule(new FilterRule\CastBool);
66
    }
67
68
    /**
69
     * Returns rule that returns a value modified by a callable closure
70
     *
71
     * @param callable $callable
72
     * @param bool $allowNotSet
73
     * @return $this
74
     */
75 6
    public function callback(callable $callable, $allowNotSet = false)
76
    {
77 6
        return $this->addRule(new FilterRule\Callback($callable, $allowNotSet));
78
    }
79
80
    /**
81
     * Returns rule that defaults a given value if the data key was not provided
82
     *
83
     * @param mixed $defaultValue
84
     * @return $this
85
     */
86 13
    public function defaults($defaultValue)
87
    {
88 13
        return $this->addRule(new FilterRule\Defaults($defaultValue));
89
    }
90
91
    /**
92
     * Returns rule that returns an value in a specific encoding format
93
     *
94
     * @param string|null $toEncodingFormat
95
     * @param string|null $fromEncodingFormat
96
     * @return FilterResource
97
     */
98 5
    public function encode($toEncodingFormat = null, $fromEncodingFormat = null)
99
    {
100 5
        return $this->addRule(new FilterRule\Encode($toEncodingFormat, $fromEncodingFormat));
101
    }
102
103
    /**
104
     * Returns rule that results a casted float
105
     *
106
     * @return $this
107
     */
108 12
    public function float()
109
    {
110 12
        return $this->addRule(new FilterRule\CastFloat);
111
    }
112
113
    /**
114
     * Returns rule that results a casted int
115
     *
116
     * @return $this
117
     */
118 11
    public function int()
119
    {
120 11
        return $this->addRule(new FilterRule\CastInt);
121
    }
122
123
    /**
124
     * Returns rule that results all letters of a value
125
     *
126
     * @return $this
127
     */
128 3
    public function letters()
129
    {
130 3
        return $this->addRule(new FilterRule\Letters);
131
    }
132
133
    /**
134
     * Returns rule that results a lower-cased value
135
     *
136
     * @return $this
137
     */
138 16
    public function lower()
139
    {
140 16
        return $this->addRule(new FilterRule\Lower);
141
    }
142
143
    /**
144
     * Returns rule that formats numbers
145
     *
146
     * @param int $decimals
147
     * @param string $decimalPoint
148
     * @param string $thousandSeparator
149
     * @return $this
150
     */
151 12
    public function numberFormat($decimals, $decimalPoint, $thousandSeparator)
152
    {
153 12
        return $this->addRule(new FilterRule\NumberFormat($decimals, $decimalPoint, $thousandSeparator));
154
    }
155
156
    /**
157
     * Returns rule that results all numbers of a value
158
     *
159
     * @return $this
160
     */
161 3
    public function numbers()
162
    {
163 3
        return $this->addRule(new FilterRule\Numbers);
164
    }
165
166
    /**
167
     * Results rule that returns the value prepended with a given value
168
     *
169
     * @param string $prepend
170
     * @return Chain
171
     */
172 4
    public function prepend($prepend)
173
    {
174 4
        return $this->addRule(new FilterRule\Prepend($prepend));
175
    }
176
177
    /**
178
     * Results rule that returns a value with replacements by a regex
179
     *
180
     * @param string $searchRegex
181
     * @param string $replace
182
     * @return $this
183
     */
184 2
    public function regexReplace($searchRegex, $replace)
185
    {
186 2
        return $this->addRule(new FilterRule\RegexReplace($searchRegex, $replace));
187
    }
188
189
    /**
190
     * Results rule that returns an empty result so it can be removed
191
     *
192
     * @return $this
193
     */
194 5
    public function remove()
195
    {
196 5
        return $this->addRule(new FilterRule\Remove);
197
    }
198
199
    /**
200
     * Results rule that returns an empty result when the value is null so it can be removed
201
     *
202
     * @return $this
203
     */
204 7
    public function removeNull()
205
    {
206 7
        return $this->addRule(new FilterRule\RemoveNull);
207
    }
208
209
    /**
210
     * Results rule that returns a value with replacements
211
     *
212
     * @param mixed $search
213
     * @param mixed $replace
214
     * @return $this
215
     */
216 5
    public function replace($search, $replace)
217
    {
218 5
        return $this->addRule(new FilterRule\Replace($search, $replace));
219
    }
220
221
    /**
222
     * Returns rule that results a casted string
223
     *
224
     * @return $this
225
     */
226 7
    public function string()
227
    {
228 7
        return $this->addRule(new FilterRule\CastString);
229
    }
230
231
    /**
232
     * Results rule that results a html-stripped value
233
     *
234
     * @param null|string $excludeTags
235
     * @return $this
236
     */
237 4
    public function stripHtml($excludeTags = null)
238
    {
239 4
        return $this->addRule(new FilterRule\StripHtml($excludeTags));
240
    }
241
242
    /**
243
     * Returns rule that results a trimmed value
244
     *
245
     * @param string|null $characters
246
     * @return $this
247
     */
248 21
    public function trim($characters = null)
249
    {
250 21
        return $this->addRule(new FilterRule\Trim($characters));
251
    }
252
253
    /**
254
     * Results rule that returns an upper-cased value
255
     *
256
     * @return $this
257
     */
258 6
    public function upper()
259
    {
260 6
        return $this->addRule(new FilterRule\Upper);
261
    }
262
263
    /**
264
     * Returns rule that results a value starting with a upper-cased character
265
     *
266
     * @return $this
267
     */
268 14
    public function upperFirst()
269
    {
270 14
        return $this->addRule(new FilterRule\UpperFirst);
271
    }
272
273
    /**
274
     * Add a new rule to the chain
275
     *
276
     * @param FilterRule $rule
277
     * @return $this
278
     */
279 161
    protected function addRule(FilterRule $rule)
280
    {
281 161
        if ($this->keys === null) {
282 4
            $this->filter->addFilterRule($rule);
283 4
        }
284
285 161
        if (is_array($this->keys)) {
286 6
            foreach ($this->keys as $key) {
287 6
                $this->filter->addFilterRule($rule, $key);
288 6
            }
289 6
        } else {
290 156
            $this->filter->addFilterRule($rule, $this->keys);
291
        }
292
293 161
        return $this;
294
    }
295
}
296