Completed
Push — develop ( 484d8b...a730d2 )
by Abdelrahman
01:15
created

helpers.php ➔ intend()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 2
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
use Illuminate\Support\Str;
6
7
if (! function_exists('domain')) {
8
    /**
9
     * Return domain host.
10
     *
11
     * @return string
12
     */
13
    function domain()
14
    {
15
        return parse_url(config('app.url'))['host'];
16
    }
17
}
18
19
if (! function_exists('intend')) {
20
    /**
21
     * Return redirect response.
22
     *
23
     * @param array $arguments
24
     * @param int   $status
25
     *
26
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
27
     */
28
    function intend(array $arguments, int $status = 302)
29
    {
30
        $redirect = redirect(array_pull($arguments, 'url'), $status);
31
32
        if (request()->expectsJson()) {
33
            $response = collect($arguments['withErrors'] ?? $arguments['with']);
34
35
            return response()->json([$response->flatten()->first() ?? 'OK']);
36
        }
37
38
        foreach ($arguments as $key => $value) {
39
            $redirect = in_array($key, ['home', 'back']) ? $redirect->{$key}() : $redirect->{$key}($value);
40
        }
41
42
        return $redirect;
43
    }
44
}
45
46
if (! function_exists('lower_case')) {
47
    /**
48
     * Convert the given string to lower-case.
49
     *
50
     * @param string $value
51
     *
52
     * @return string
53
     */
54
    function lower_case($value)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
55
    {
56
        return Str::lower($value);
57
    }
58
}
59
60
if (! function_exists('upper_case')) {
61
    /**
62
     * Convert the given string to upper-case.
63
     *
64
     * @param string $value
65
     *
66
     * @return string
67
     */
68
    function upper_case($value)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
69
    {
70
        return Str::upper($value);
71
    }
72
}
73
74
if (! function_exists('mimetypes')) {
75
    /**
76
     * Get valid mime types.
77
     *
78
     * @see https://github.com/symfony/http-foundation/blob/3.0/File/MimeType/MimeTypeExtensionGuesser.php
79
     * @see http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
80
     *
81
     * @return array
82
     */
83
    function mimetypes()
84
    {
85
        return json_decode(file_get_contents(__DIR__.'/../../resources/data/mimetypes.json'), true);
86
    }
87
}
88
89
if (! function_exists('timezones')) {
90
    /**
91
     * Get valid timezones.
92
     * This list is based upon the timezone database version 2017.2.
93
     *
94
     * @see http://php.net/manual/en/timezones.php
95
     *
96
     * @return array
97
     */
98
    function timezones()
99
    {
100
        return json_decode(file_get_contents(__DIR__.'/../../resources/data/timezones.json'), true);
101
    }
102
}
103
104
if (! function_exists('array_search_recursive')) {
105
    /**
106
     * Recursively searches the array for a given value and returns the corresponding key if successful.
107
     *
108
     * @param mixed $needle
109
     * @param array $haystack
110
     *
111
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use integer|string|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
112
     */
113
    function array_search_recursive($needle, $haystack)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
114
    {
115
        foreach ($haystack as $key => $value) {
116
            $current_key = $key;
117
            if ($needle === $value || (is_array($value) && array_search_recursive($needle, $value) !== false)) {
118
                return $current_key;
119
            }
120
        }
121
122
        return false;
123
    }
124
}
125
126
if (! function_exists('array_trim_recursive')) {
127
    /**
128
     * Recursively trim elements of the given array.
129
     *
130
     * @param mixed  $values
131
     * @param string $charlist
132
     *
133
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array|object|integer|double|null|boolean|string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
134
     */
135
    function array_trim_recursive($values, $charlist = " \t\n\r\0\x0B")
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
136
    {
137
        if (is_array($values)) {
138
            return array_map('array_trim_recursive', $values);
139
        }
140
141
        return is_string($values) ? trim($values, $charlist) : $values;
142
    }
143
}
144
145
if (! function_exists('array_filter_recursive')) {
146
    /**
147
     * Recursively filter empty strings and null elements of the given array.
148
     *
149
     * @param array $values
150
     * @param bool  $strOnly
151
     *
152
     * @return mixed
153
     */
154
    function array_filter_recursive($values, $strOnly = true)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
155
    {
156
        foreach ($values as &$value) {
157
            if (is_array($value)) {
158
                $value = array_filter_recursive($value);
159
            }
160
        }
161
162
        return ! $strOnly ? array_filter($values) : array_filter($values, function ($item) {
163
            return ! is_null($item) && ! ((is_string($item) || is_array($item)) && empty($item));
164
        });
165
    }
166
}
167