1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* Part of the Rinvex Support Package. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to The MIT License (MIT) |
9
|
|
|
* that is bundled with this package in the LICENSE file. |
10
|
|
|
* |
11
|
|
|
* Package: Rinvex Support Package |
12
|
|
|
* License: The MIT License (MIT) |
13
|
|
|
* Link: https://rinvex.com |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
declare(strict_types=1); |
17
|
|
|
|
18
|
|
|
use Illuminate\Support\Str; |
19
|
|
|
use Illuminate\Http\JsonResponse; |
20
|
|
|
|
21
|
|
|
if (! function_exists('intend')) { |
22
|
|
|
/** |
23
|
|
|
* Return redirect response. |
24
|
|
|
* |
25
|
|
|
* @param array $arguments |
26
|
|
|
* @param string|null $statusCode |
27
|
|
|
* |
28
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse |
29
|
|
|
*/ |
30
|
|
|
function intend(array $arguments, $statusCode = null) |
31
|
|
|
{ |
32
|
|
|
$redirect = redirect(array_pull($arguments, 'url')); |
33
|
|
|
$statusCode = $statusCode ?: isset($arguments['withErrors']) ? 422 : 200; |
34
|
|
|
|
35
|
|
|
if (request()->expectsJson()) { |
36
|
|
|
return new JsonResponse($arguments['withErrors'] ?: $arguments['with'] ?: 'OK', $statusCode); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
foreach ($arguments as $key => $value) { |
40
|
|
|
$redirect = in_array($key, ['home', 'back']) ? $redirect->{$key}() : $redirect->{$key}($value); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $redirect; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (! function_exists('lower_case')) { |
48
|
|
|
/** |
49
|
|
|
* Convert the given string to lower-case. |
50
|
|
|
* |
51
|
|
|
* @param string $value |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
function lower_case($value) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
return Str::lower($value); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (! function_exists('upper_case')) { |
62
|
|
|
/** |
63
|
|
|
* Convert the given string to upper-case. |
64
|
|
|
* |
65
|
|
|
* @param string $value |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
function upper_case($value) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
return Str::upper($value); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (! function_exists('mimetypes')) { |
76
|
|
|
/** |
77
|
|
|
* Get valid mime types. |
78
|
|
|
* |
79
|
|
|
* @see https://github.com/symfony/http-foundation/blob/3.0/File/MimeType/MimeTypeExtensionGuesser.php |
80
|
|
|
* @see http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
function mimetypes() |
85
|
|
|
{ |
86
|
|
|
return json_decode(file_get_contents(__DIR__.'/../../resources/data/mimetypes.json'), true); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (! function_exists('timezones')) { |
91
|
|
|
/** |
92
|
|
|
* Get valid timezones. |
93
|
|
|
* This list is based upon the timezone database version 2016.1. |
94
|
|
|
* |
95
|
|
|
* @see http://php.net/manual/en/timezones.php |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
function timezones() |
100
|
|
|
{ |
101
|
|
|
return json_decode(file_get_contents(__DIR__.'/../../resources/data/timezones.json'), true); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (! function_exists('array_search_recursive')) { |
106
|
|
|
/** |
107
|
|
|
* Recursively searches the array for a given value and returns the corresponding key if successful. |
108
|
|
|
* |
109
|
|
|
* @param mixed $needle |
110
|
|
|
* @param array $haystack |
111
|
|
|
* |
112
|
|
|
* @return mixed |
|
|
|
|
113
|
|
|
*/ |
114
|
|
|
function array_search_recursive($needle, $haystack) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
foreach ($haystack as $key => $value) { |
117
|
|
|
$current_key = $key; |
118
|
|
|
if ($needle === $value || (is_array($value) && array_search_recursive($needle, $value) !== false)) { |
119
|
|
|
return $current_key; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (! function_exists('array_trim_recursive')) { |
128
|
|
|
/** |
129
|
|
|
* Recursively trim elements of the given array. |
130
|
|
|
* |
131
|
|
|
* @param mixed $values |
132
|
|
|
* @param string $charlist |
133
|
|
|
* |
134
|
|
|
* @return mixed |
|
|
|
|
135
|
|
|
*/ |
136
|
|
|
function array_trim_recursive($values, $charlist = " \t\n\r\0\x0B") |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
if (is_array($values)) { |
139
|
|
|
return array_map('array_trim_recursive', $values); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return is_string($values) ? trim($values, $charlist) : $values; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (! function_exists('array_filter_recursive')) { |
147
|
|
|
/** |
148
|
|
|
* Recursively filter empty strings and null elements of the given array. |
149
|
|
|
* |
150
|
|
|
* @param array $values |
151
|
|
|
* @param bool $strOnly |
152
|
|
|
* |
153
|
|
|
* @return mixed |
154
|
|
|
*/ |
155
|
|
|
function array_filter_recursive($values, $strOnly = true) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
foreach ($values as &$value) { |
158
|
|
|
if (is_array($value)) { |
159
|
|
|
$value = array_filter_recursive($value); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return ! $strOnly ? array_filter($values) : array_filter($values, function ($item) { |
164
|
|
|
return ! is_null($item) && ! ((is_string($item) || is_array($item)) && empty($item)); |
165
|
|
|
}); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
Learn more about camelCase.