Completed
Push — master ( 29a107...006b0a )
by Abdelrahman
01:23 queued 11s
created

helpers.php ➔ intend()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 8
nop 2
dl 0
loc 18
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\HtmlString;
7
8
if (! function_exists('extract_title')) {
9
    /**
10
     * Extract page title from breadcrumbs.
11
     *
12
     * @return string
13
     */
14
    function extract_title(HtmlString $breadcrumbs, string $separator = ' » ')
15
    {
16
        return Str::afterLast(preg_replace('/[\n\r\s]+/', ' ', strip_tags(Str::replaceLast($separator, '', str_replace('</li>', $separator, $breadcrumbs)))), $separator)." {$separator} ".config('app.name');
17
    }
18
}
19
20
if (! function_exists('domain')) {
21
    /**
22
     * Return domain host.
23
     *
24
     * @return string
25
     */
26
    function domain()
27
    {
28
        return parse_url(config('app.url'))['host'];
29
    }
30
}
31
32
if (! function_exists('intend')) {
33
    /**
34
     * Return redirect response.
35
     *
36
     * @param array $arguments
37
     * @param int   $status
38
     *
39
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
40
     */
41
    function intend(array $arguments, int $status = 302)
42
    {
43
        if (request()->expectsJson() || request()->isApi()) {
44
            $status !== 0 || $status = 401; // If status code = 0, it's authorization error
45
            $response = collect($arguments['withErrors'] ?? $arguments['with']);
46
47
            return response()->json([$response->flatten()->first() ?? 'OK'], $status);
48
        }
49
50
        $status !== 0 || $status = 302; // If status code = 0, it's authorization error
51
        $redirect = redirect(Arr::pull($arguments, 'url'), $status);
52
53
        foreach ($arguments as $key => $value) {
54
            $redirect = in_array($key, ['home', 'back']) ? $redirect->{$key}() : $redirect->{$key}($value);
55
        }
56
57
        return $redirect;
58
    }
59
}
60
61
if (! function_exists('mimetypes')) {
62
    /**
63
     * Get valid mime types.
64
     *
65
     * @see https://github.com/symfony/http-foundation/blob/3.0/File/MimeType/MimeTypeExtensionGuesser.php
66
     * @see http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
67
     *
68
     * @return array
69
     */
70
    function mimetypes()
71
    {
72
        return json_decode(file_get_contents(__DIR__.'/../../resources/data/mimetypes.json'), true);
73
    }
74
}
75
76
if (! function_exists('timezones')) {
77
    /**
78
     * Get valid timezones.
79
     *
80
     * @return array
81
     */
82
    function timezones()
83
    {
84
        return array_combine(timezone_identifiers_list(), timezone_identifiers_list());
85
    }
86
}
87
88
if (! function_exists('timeoffsets')) {
89
    /**
90
     * Get valid time offsets.
91
     *
92
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,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...
93
     */
94
    function timeoffsets()
95
    {
96
        return [
97
            '-1200' => 'UTC -12:00',
98
            '-1100' => 'UTC -11:00',
99
            '-1000' => 'UTC -10:00',
100
            '-0930' => 'UTC -09:30',
101
            '-0900' => 'UTC -09:00',
102
            '-0800' => 'UTC -08:00',
103
            '-0700' => 'UTC -07:00',
104
            '-0600' => 'UTC -06:00',
105
            '-0500' => 'UTC -05:00',
106
            '-0400' => 'UTC -04:00',
107
            '-0330' => 'UTC -03:30',
108
            '-0300' => 'UTC -03:00',
109
            '-0200' => 'UTC -02:00',
110
            '-0100' => 'UTC -01:00',
111
            '+0000' => 'UTC ±00:00',
112
            '+0100' => 'UTC +01:00',
113
            '+0200' => 'UTC +02:00',
114
            '+0300' => 'UTC +03:00',
115
            '+0330' => 'UTC +03:30',
116
            '+0400' => 'UTC +04:00',
117
            '+0430' => 'UTC +04:30',
118
            '+0500' => 'UTC +05:00',
119
            '+0530' => 'UTC +05:30',
120
            '+0545' => 'UTC +05:45',
121
            '+0600' => 'UTC +06:00',
122
            '+0630' => 'UTC +06:30',
123
            '+0700' => 'UTC +07:00',
124
            '+0800' => 'UTC +08:00',
125
            '+0845' => 'UTC +08:45',
126
            '+0900' => 'UTC +09:00',
127
            '+0930' => 'UTC +09:30',
128
            '+1000' => 'UTC +10:00',
129
            '+1030' => 'UTC +10:30',
130
            '+1100' => 'UTC +11:00',
131
            '+1200' => 'UTC +12:00',
132
            '+1245' => 'UTC +12:45',
133
            '+1300' => 'UTC +13:00',
134
            '+1400' => 'UTC +14:00',
135
        ];
136
    }
137
}
138
139
if (! function_exists('array_search_recursive')) {
140
    /**
141
     * Recursively searches the array for a given value and returns the corresponding key if successful.
142
     *
143
     * @param mixed $needle
144
     * @param array $haystack
145
     *
146
     * @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...
147
     */
148
    function array_search_recursive($needle, $haystack)
149
    {
150
        foreach ($haystack as $key => $value) {
151
            $current_key = $key;
152
            if ($needle === $value || (is_array($value) && array_search_recursive($needle, $value) !== false)) {
153
                return $current_key;
154
            }
155
        }
156
157
        return false;
158
    }
159
}
160
161
if (! function_exists('array_trim_recursive')) {
162
    /**
163
     * Recursively trim elements of the given array.
164
     *
165
     * @param mixed  $values
166
     * @param string $charlist
167
     *
168
     * @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...
169
     */
170
    function array_trim_recursive($values, $charlist = " \t\n\r\0\x0B")
171
    {
172
        if (is_array($values)) {
173
            return array_map('array_trim_recursive', $values);
174
        }
175
176
        return is_string($values) ? trim($values, $charlist) : $values;
177
    }
178
}
179
180
if (! function_exists('array_filter_recursive')) {
181
    /**
182
     * Recursively filter empty strings and null elements of the given array.
183
     *
184
     * @param array $values
185
     * @param bool  $strOnly
186
     *
187
     * @return mixed
188
     */
189
    function array_filter_recursive($values, $strOnly = true)
190
    {
191
        foreach ($values as &$value) {
192
            if (is_array($value)) {
193
                $value = array_filter_recursive($value);
194
            }
195
        }
196
197
        return ! $strOnly ? array_filter($values) : array_filter($values, function ($item) {
198
            return ! is_null($item) && ! ((is_string($item) || is_array($item)) && empty($item));
199
        });
200
    }
201
}
202