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 strip_tags(Str::replaceLast($separator, '', str_replace('</li>', $separator, $breadcrumbs))); |
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
|
|
|
$redirect = redirect(Arr::pull($arguments, 'url'), $status); |
44
|
|
|
|
45
|
|
|
if (request()->expectsJson()) { |
46
|
|
|
$response = collect($arguments['withErrors'] ?? $arguments['with']); |
47
|
|
|
|
48
|
|
|
return response()->json([$response->flatten()->first() ?? 'OK']); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
foreach ($arguments as $key => $value) { |
52
|
|
|
$redirect = in_array($key, ['home', 'back']) ? $redirect->{$key}() : $redirect->{$key}($value); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $redirect; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (! function_exists('mimetypes')) { |
60
|
|
|
/** |
61
|
|
|
* Get valid mime types. |
62
|
|
|
* |
63
|
|
|
* @see https://github.com/symfony/http-foundation/blob/3.0/File/MimeType/MimeTypeExtensionGuesser.php |
64
|
|
|
* @see http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
function mimetypes() |
69
|
|
|
{ |
70
|
|
|
return json_decode(file_get_contents(__DIR__.'/../../resources/data/mimetypes.json'), true); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (! function_exists('timezones')) { |
75
|
|
|
/** |
76
|
|
|
* Get valid timezones. |
77
|
|
|
* This list is based upon the timezone database version 2017.2. |
78
|
|
|
* |
79
|
|
|
* @see http://php.net/manual/en/timezones.php |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
function timezones() |
84
|
|
|
{ |
85
|
|
|
return json_decode(file_get_contents(__DIR__.'/../../resources/data/timezones.json'), true); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (! function_exists('array_search_recursive')) { |
90
|
|
|
/** |
91
|
|
|
* Recursively searches the array for a given value and returns the corresponding key if successful. |
92
|
|
|
* |
93
|
|
|
* @param mixed $needle |
94
|
|
|
* @param array $haystack |
95
|
|
|
* |
96
|
|
|
* @return mixed |
|
|
|
|
97
|
|
|
*/ |
98
|
|
|
function array_search_recursive($needle, $haystack) |
99
|
|
|
{ |
100
|
|
|
foreach ($haystack as $key => $value) { |
101
|
|
|
$current_key = $key; |
102
|
|
|
if ($needle === $value || (is_array($value) && array_search_recursive($needle, $value) !== false)) { |
103
|
|
|
return $current_key; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (! function_exists('array_trim_recursive')) { |
112
|
|
|
/** |
113
|
|
|
* Recursively trim elements of the given array. |
114
|
|
|
* |
115
|
|
|
* @param mixed $values |
116
|
|
|
* @param string $charlist |
117
|
|
|
* |
118
|
|
|
* @return mixed |
|
|
|
|
119
|
|
|
*/ |
120
|
|
|
function array_trim_recursive($values, $charlist = " \t\n\r\0\x0B") |
121
|
|
|
{ |
122
|
|
|
if (is_array($values)) { |
123
|
|
|
return array_map('array_trim_recursive', $values); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return is_string($values) ? trim($values, $charlist) : $values; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (! function_exists('array_filter_recursive')) { |
131
|
|
|
/** |
132
|
|
|
* Recursively filter empty strings and null elements of the given array. |
133
|
|
|
* |
134
|
|
|
* @param array $values |
135
|
|
|
* @param bool $strOnly |
136
|
|
|
* |
137
|
|
|
* @return mixed |
138
|
|
|
*/ |
139
|
|
|
function array_filter_recursive($values, $strOnly = true) |
140
|
|
|
{ |
141
|
|
|
foreach ($values as &$value) { |
142
|
|
|
if (is_array($value)) { |
143
|
|
|
$value = array_filter_recursive($value); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return ! $strOnly ? array_filter($values) : array_filter($values, function ($item) { |
148
|
|
|
return ! is_null($item) && ! ((is_string($item) || is_array($item)) && empty($item)); |
149
|
|
|
}); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
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.