Passed
Push — ft/appmove ( db87fd...97613e )
by Philippe
45:05 queued 26:47
created

MenuRequest::validationData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Http\Requests;
4
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Foundation\Http\FormRequest;
7
use Thinktomorrow\Url\Url;
8
use Thinktomorrow\Url\Root;
9
use Thinktomorrow\Chief\Concerns\Translatable\TranslatableCommand;
10
11
class MenuRequest extends FormRequest
12
{
13
    use TranslatableCommand;
14
15
    /**
16
     * Determine if the user is authorized to make this request.
17
     *
18
     * @return bool
19
     */
20
    public function authorize()
21
    {
22
        return Auth::guard('chief')->user();
23
    }
24
25
    public function validationData()
26
    {
27
        $data = parent::validationData();
28
29
        $data = $this->sanitizeUrl($data);
30
31
        return $data;
32
    }
33
34
    /**
35
     * Get the validation rules that apply to the request.
36
     *
37
     * @return array
38
     */
39
    public function rules()
40
    {
41
        $translations = $this->request->get('trans', []);
42
43
        $rules['type']            = 'required|in:custom,internal,nolink';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$rules was never initialized. Although not strictly required by PHP, it is generally a good practice to add $rules = array(); before regardless.
Loading history...
44
        $rules['page_id']         = 'required_if:type,internal';
45
46
        foreach ($translations as $locale => $trans) {
47
            if ($this->isCompletelyEmpty(['label'], $trans) && $locale !== config('app.locale')) {
48
                continue;
49
            }
50
51
            $rules['trans.' . $locale . '.label']   = 'required';
52
            if ($this->request->get('trans.' . $locale . '.url') != null) {
53
                $rules['trans.' . $locale . '.url']     = 'url';
54
            }
55
        }
56
57
        return $rules;
58
    }
59
60
    public function attributes()
61
    {
62
        $attributes = [];
63
64
        foreach ($this->request->get('trans', []) as $locale => $trans) {
65
            $attributes['trans.' . $locale . '.label']   = $locale . ' label';
66
            $attributes['trans.' . $locale . '.url']     = $locale . ' link';
67
        }
68
69
        $attributes['page_id'] = 'Interne pagina';
70
71
72
        return $attributes;
73
    }
74
75
    public function messages()
76
    {
77
        return [
78
            'required_if' => 'Gelieve nog een :attribute te kiezen, aub.',
79
            'required'    => 'Gelieve een :attribute in te geven, aub.',
80
            'url'         => 'Dit is geen geldige url. Kan je dit even nakijken, aub?',
81
        ];
82
    }
83
84
    /**
85
     * @param $data
86
     * @return mixed
87
     */
88
    protected function sanitizeUrl($data)
89
    {
90
        foreach ($data['trans'] as $locale => $trans) {
91
            if (empty($trans['url'])) {
92
                continue;
93
            }
94
95
            // Check if it is a relative
96
            if ($this->isRelativeUrl($trans['url'])) {
97
                $data['trans'][$locale]['url'] = '/'. trim($trans['url'], '/');
98
            } else {
99
                $data['trans'][$locale]['url'] = Url::fromString($trans['url'])->secure()->get();
100
            }
101
102
            $this->request->set('trans', $data['trans']);
103
        }
104
105
        return $data;
106
    }
107
108
    private function isRelativeUrl($url): bool
109
    {
110
        $nakedUrl = ltrim($url, '/');
111
112
        // Check if passed url is not intended as a host instead of a relative path
113
        $notIntentedAsRoot = (null == Root::fromString($url)->scheme() && false === strpos($url, '.'));
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing Thinktomorrow\Url\Root::...mString($url)->scheme() of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
114
115
        return ($notIntentedAsRoot && in_array($url, [$nakedUrl, '/'.$nakedUrl]));
116
    }
117
}
118