Completed
Pull Request — master (#37)
by Rick
04:08
created

FilterResource::defaults()   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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
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 144
    public function __construct(Filter $filter, $keys = null)
32
    {
33 144
        $this->filter = $filter;
34 144
        $this->keys = $keys;
35 144
    }
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
     * @return $this
73
     */
74 2
    public function callback(callable $callable)
75
    {
76 2
        return $this->addRule(new FilterRule\Callback($callable));
77
    }
78
79
    /**
80
     * Returns rule that defaults a given value if the data key was not provided
81
     *
82
     * @param mixed $defaultValue
83
     * @return $this
84
     */
85 11
    public function defaults($defaultValue)
86
    {
87 11
        return $this->addRule(new FilterRule\Defaults($defaultValue));
88
    }
89
90
    /**
91
     * Returns rule that returns an value in a specific encoding format
92
     *
93
     * @param string|null $toEncodingFormat
94
     * @param string|null $fromEncodingFormat
95
     * @return FilterResource
96
     */
97 5
    public function encode($toEncodingFormat = null, $fromEncodingFormat = null)
98
    {
99 5
        return $this->addRule(new FilterRule\Encode($toEncodingFormat, $fromEncodingFormat));
100
    }
101
102
    /**
103
     * Returns rule that results a casted float
104
     *
105
     * @return $this
106
     */
107 12
    public function float()
108
    {
109 12
        return $this->addRule(new FilterRule\CastFloat);
110
    }
111
112
    /**
113
     * Returns rule that results a casted int
114
     *
115
     * @return $this
116
     */
117 11
    public function int()
118
    {
119 11
        return $this->addRule(new FilterRule\CastInt);
120
    }
121
122
    /**
123
     * Returns rule that results all letters of a value
124
     *
125
     * @return $this
126
     */
127 3
    public function letters()
128
    {
129 3
        return $this->addRule(new FilterRule\Letters);
130
    }
131
132
    /**
133
     * Returns rule that results a lower-cased value
134
     *
135
     * @return $this
136
     */
137 16
    public function lower()
138
    {
139 16
        return $this->addRule(new FilterRule\Lower);
140
    }
141
142
    /**
143
     * Returns rule that formats numbers
144
     *
145
     * @param int $decimals
146
     * @param string $decimalPoint
147
     * @param string $thousandSeperator
148
     * @return $this
149
     */
150 12
    public function numberFormat($decimals, $decimalPoint, $thousandSeperator)
151
    {
152 12
        return $this->addRule(new FilterRule\NumberFormat($decimals, $decimalPoint, $thousandSeperator));
153
    }
154
155
    /**
156
     * Returns rule that results all numbers of a value
157
     *
158
     * @return $this
159
     */
160 3
    public function numbers()
161
    {
162 3
        return $this->addRule(new FilterRule\Numbers);
163
    }
164
165
    /**
166
     * Results rule that returns the value prepended with a given value
167
     *
168
     * @param string $prepend
169
     * @return Chain
170
     */
171 4
    public function prepend($prepend)
172
    {
173 4
        return $this->addRule(new FilterRule\Prepend($prepend));
174
    }
175
176
    /**
177
     * Results rule that returns a value with replacements by a regex
178
     *
179
     * @param string $searchRegex
180
     * @param string $replace
181
     * @return $this
182
     */
183 2
    public function regexReplace($searchRegex, $replace)
184
    {
185 2
        return $this->addRule(new FilterRule\RegexReplace($searchRegex, $replace));
186
    }
187
188
    /**
189
     * Results rule that returns a value with replacements
190
     *
191
     * @param mixed $search
192
     * @param mixed $replace
193
     * @return $this
194
     */
195 5
    public function replace($search, $replace)
196
    {
197 5
        return $this->addRule(new FilterRule\Replace($search, $replace));
198
    }
199
200
    /**
201
     * Returns rule that results a casted string
202
     *
203
     * @return $this
204
     */
205 7
    public function string()
206
    {
207 7
        return $this->addRule(new FilterRule\CastString);
208
    }
209
210
    /**
211
     * Results rule that results a html-stripped value
212
     *
213
     * @param null|string $excludeTags
214
     * @return $this
215
     */
216 4
    public function stripHtml($excludeTags = null)
217
    {
218 4
        return $this->addRule(new FilterRule\StripHtml($excludeTags));
219
    }
220
221
    /**
222
     * Returns rule that results a trimmed value
223
     *
224
     * @param string|null $characters
225
     * @return $this
226
     */
227 21
    public function trim($characters = null)
228
    {
229 21
        return $this->addRule(new FilterRule\Trim($characters));
230
    }
231
232
    /**
233
     * Results rule that returns an upper-cased value
234
     *
235
     * @return $this
236
     */
237 6
    public function upper()
238
    {
239 6
        return $this->addRule(new FilterRule\Upper);
240
    }
241
242
    /**
243
     * Returns rule that results a value starting with a upper-cased character
244
     *
245
     * @return $this
246
     */
247 14
    public function upperFirst()
248
    {
249 14
        return $this->addRule(new FilterRule\UpperFirst);
250
    }
251
252
    /**
253
     * Add a new rule to the chain
254
     *
255
     * @param FilterRule $rule
256
     * @return $this
257
     */
258 143
    protected function addRule(FilterRule $rule)
259
    {
260 143
        if ($this->keys === null) {
261 2
            $this->filter->addFilterRule($rule);
262 2
        }
263
264 143
        if (is_array($this->keys)) {
265 5
            foreach ($this->keys as $key) {
266 5
                $this->filter->addFilterRule($rule, $key);
267 5
            }
268 5
        } else {
269 139
            $this->filter->addFilterRule($rule, $this->keys);
270
        }
271
272 143
        return $this;
273
    }
274
}
275