Passed
Push — master ( 299e29...923e03 )
by Jeff
02:45 queued 10s
created

helper_test()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: shehbaz
5
 * Date: 1/21/19
6
 * Time: 12:19 PM
7
 */
8
9
10
use Illuminate\Support\Facades\Validator;
11
12
if (!function_exists("helper_test")) {
13
    function helper_test()
14
    {
15
        echo "it is working";
16
    }
17
}
18
19
if (!function_exists("populate_breadcumb")) {
20
    /**
21
     * popular data to layouts.admin.app when send from controller
22
     *
23
     *<h1> controller example </h1>
24
     * <pre>
25
     *  $data = [
26
     * ["name" => "Dashboard1", "url" => route("admin.dashboard")],
27
     * ["name" => "Products1", "url" => request()->fullUrl()]
28
     * ];
29
     *
30
     * populate_breadcumb($data)
31
     * </pre>
32
     *
33
     * @param $data
34
     * @return void
35
     */
36
    function populate_breadcumb($data)
37
    {
38
        $validated = validate_breadcumb($data);
39
        if ($validated["valid"] === true) {
40
            view()->composer([
41
                "layouts.admin.app"
42
            ], function ($view) use ($data) {
43
                $view->with(
44
                    [
45
                        "breadcumbs" => $data
46
                    ]
47
                );
48
            });
49
        }
50
51
    }
52
53
}
54
55
if (!function_exists('validate_breadcumb')) {
56
57
    /**
58
     * validate breadcumb data
59
     * @param $data
60
     * @return array
61
     */
62
    function validate_breadcumb($data)
63
    {
64
        $validated = false;
65
        $errors = [];
66
        foreach ($data as $key => $item) {
67
            $messages = [
68
                'required' => "The :attribute field is required at index: $key.",
69
                "url" => "The :attribute format is invalid at index: $key"
70
71
            ];
72
            $validator = Validator::make($item, [
73
                'name' => 'required',
74
                'url' => "required|url",
75
//                "icon" => ""
76
            ], $messages);
77
            if ($validator->fails()) {
78
                $validated = false;
79
                $errors[] = $validator->errors();
80
81
            } else {
82
                $validated = true;
83
            }
84
        }
85
        return ["errors" => $errors, "valid" => $validated];
86
    }
87
}
88