Passed
Push — master ( 62919d...1f65fa )
by Iman
04:23
created

CbValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A valid() 0 18 3
1
<?php
2
3
namespace Crocodicstudio\Crudbooster\helpers;
4
5
use Illuminate\Support\Facades\Validator;
6
7
class CbValidator
8
{
9
    public static function valid($rules = [], string $type = 'json')
10
    {
11
        $validator = Validator::make(request()->all(), $rules);
12
13
        if (!$validator->fails()) {
14
            return true;
15
        }
16
17
        $message = $validator->errors()->all();
18
19
        if ($type != 'json') {
20
            respondWith(redirect()->back()->with(['message' => implode('<br/>', $message), 'message_type' => 'warning'])->withInput());
21
        }
22
        $result = [
23
            'api_status' => 0,
24
            'api_message' => implode(', ', $message),
25
        ];
26
        respondWith()->json($result, 200);
27
    }
28
}