Completed
Push — master ( ce5311...f14f8d )
by James
03:06 queued 57s
created

TransformsRequest::clean()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Fatindeed\EmojiHelper\Http\Middleware;
4
5
use Closure;
6
use Symfony\Component\HttpFoundation\ParameterBag;
7
8
/**
9
 * @see https://github.com/laravel/framework/blob/6.x/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php Illuminate\Foundation\Http\Middleware\TransformsRequest
10
 */
11
class TransformsRequest
12
{
13
    /**
14
     * Handle an incoming request.
15
     *
16
     * @param  \Illuminate\Http\Request  $request
17
     * @param  \Closure  $next
18
     * @return mixed
19
     */
20
    public function handle($request, Closure $next)
21
    {
22
        $this->clean($request);
23
24
        return $next($request);
25
    }
26
27
    /**
28
     * Clean the request's data.
29
     *
30
     * @param  \Illuminate\Http\Request  $request
31
     * @return void
32
     */
33
    protected function clean($request)
34
    {
35
        $this->cleanParameterBag($request->query);
36
37
        if ($request->isJson()) {
38
            $this->cleanParameterBag($request->json());
39
        } elseif ($request->request !== $request->query) {
40
            $this->cleanParameterBag($request->request);
41
        }
42
    }
43
44
    /**
45
     * Clean the data in the parameter bag.
46
     *
47
     * @param  \Symfony\Component\HttpFoundation\ParameterBag  $bag
48
     * @return void
49
     */
50
    protected function cleanParameterBag(ParameterBag $bag)
51
    {
52
        $bag->replace($this->cleanArray($bag->all()));
53
    }
54
55
    /**
56
     * Clean the data in the given array.
57
     *
58
     * @param  array  $data
59
     * @param  string  $keyPrefix
60
     * @return array
61
     */
62
    protected function cleanArray(array $data, $keyPrefix = '')
63
    {
64
        return collect($data)->map(function ($value, $key) use ($keyPrefix) {
65
            return $this->cleanValue($keyPrefix.$key, $value);
66
        })->all();
67
    }
68
69
    /**
70
     * Clean the given value.
71
     *
72
     * @param  string  $key
73
     * @param  mixed  $value
74
     * @return mixed
75
     */
76
    protected function cleanValue($key, $value)
77
    {
78
        if (is_array($value)) {
79
            return $this->cleanArray($value, $key.'.');
80
        }
81
82
        return $this->transform($key, $value);
83
    }
84
85
    /**
86
     * Transform the given value.
87
     *
88
     * @param  string  $key
89
     * @param  mixed  $value
90
     * @return mixed
91
     */
92
    protected function transform($key, $value)
93
    {
94
        return $value;
95
    }
96
}