Completed
Push — master ( 156dfc...2ef3f7 )
by Abdelrahman
02:24 queued 01:20
created

helpers.php ➔ array_diff_assoc_recursive()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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