SanitizesRequests   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%
Metric Value
wmc 1
lcom 0
cbo 1
dl 0
loc 17
ccs 3
cts 3
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A sanitize() 0 6 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