Completed
Push — master ( 182185...cd5526 )
by Abdelrahman
01:17 queued 11s
created

helpers.php ➔ get_access_area()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 0
dl 0
loc 15
rs 9.7666
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
use Illuminate\Support\Facades\Route;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Route.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

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