SanitizesRequests::sanitize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
nc 1
cc 1
eloc 3
nop 2
crap 1
1
<?php
2
3
namespace Alfheim\Sanitizer\Laravel;
4
5
use Alfheim\Sanitizer\Sanitizer;
6
use Illuminate\Http\Request;
7
8
/*
9
 * This trait can be used on any class. However, you'll probably mostly use it
10
 * on your controllers. Similarly to the `ValidatesRequests` trait, this will
11
 * allow for:
12
 *
13
 *     $input = $this->sanitize($request, [ rules ... ]);
14
 *
15
 * Of course, the variable can be named whatever you see fit.
16
 *
17
 * Keep in mind that doing this will NOT commit changes to the request object,
18
 * which means the sanitized input is only accessible through the variable it
19
 * is stored onto.
20
 *
21
 * So, to fetch request input, do the following:
22
 *
23
 *     $userEmail = $input['email'];
24
 *
25
 * As opposed to what you may be used to:
26
 *
27
 *     $userEmail = $request->input('email');
28
 *     //            Request::input('email')
29
 */
30
trait SanitizesRequests
31
{
32
    /**
33
     * Run a sanitizer on a request object and return the sanitized data.
34
     *
35
     * @param  \Illuminate\Http\Request  $request
36
     * @param  array  $ruleset
37
     *
38
     * @return array
39
     */
40 4
    public function sanitize(Request $request, array $ruleset)
41
    {
42 4
        $factory = app(Sanitizer::class)->rules($ruleset);
43
44 4
        return $factory->sanitize($request->all());
45
    }
46
}
47