Completed
Push — master ( aa69d3...1330d0 )
by Elf
05:09
created

helpers.php ➔ random_uuid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
use Illuminate\Support\Str;
4
use Ramsey\Uuid\Uuid;
5
6
if (! function_exists('mb_trim')) {
7
    /**
8
     * Strip whitespace (or other characters) from the beginning and end of a string.
9
     *
10
     * @see https://github.com/vanderlee/PHP-multibyte-functions/blob/master/functions/mb_trim.php
11
     *
12
     * @param  string  $string
13
     * @return string
14
     */
15
    function mb_trim($string)
16
    {
17
        return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string);
18
    }
19
}
20
21
if (! function_exists('urlsafe_base64_encode')) {
22
    /**
23
     * Encodes the given data with base64, and returns an URL-safe string.
24
     *
25
     * @param  string  $data
26
     * @return string
27
     */
28
    function urlsafe_base64_encode($data)
29
    {
30
        return strtr(base64_encode($data), ['+' => '-', '/' => '_', '=' => '']);
31
    }
32
}
33
34
if (! function_exists('urlsafe_base64_decode')) {
35
    /**
36
     * Decodes a base64 encoded data.
37
     *
38
     * @param  string  $data
39
     * @param  bool  $strict
40
     * @return string
41
     */
42
    function urlsafe_base64_decode($data, $strict = false)
43
    {
44
        return base64_decode(strtr($data.str_repeat('=', (4 - strlen($data) % 4)), '-_', '+/'), $strict);
45
    }
46
}
47
48
if (! function_exists('string_value')) {
49
    /**
50
     * Converts any type to a string.
51
     *
52
     * @param  mixed  $value
53
     * @return string
54
     */
55
    function string_value($value, $jsonOptions = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
56
    {
57
        if (is_object($value)) {
58
            if (method_exists($value, '__toString')) {
59
                return (string) $value;
60
            }
61
62
            if (method_exists($value, 'toArray')) {
63
                $value = $value->toArray();
64
            }
65
        }
66
67
        return is_string($value) ? $value : json_encode($value, $jsonOptions);
68
    }
69
}
70
71
if (! function_exists('active_if')) {
72
    /**
73
     * Returns string 'active' if the current request URI matches the given patterns.
74
     *
75
     * @return string
76
     */
77
    function active_if()
78
    {
79
        return call_user_func_array([app('request'), 'is'], func_get_args()) ? 'active' : '';
80
    }
81
}
82
83
if (! function_exists('str_limit2')) {
84
    /**
85
     * Limit the number of characters in a string.
86
     *
87
     * @param  string  $value
88
     * @param  int     $limit
89
     * @param  string  $end
90
     * @return string
91
     */
92
    function str_limit2($value, $limit = 100, $end = '')
93
    {
94
        return Str::limit($value, $limit, $end);
95
    }
96
}
97
98
if (! function_exists('is_domain')) {
99
    /**
100
     * Determines the current domain equals to the given domain identifier.
101
     *
102
     * @param  string  $identifier
103
     * @return bool
104
     */
105
    function is_domain($identifier)
106
    {
107 1
        return app('request')->getHost() === config('app.domains.'.$identifier);
108
    }
109
}
110
111
if (! function_exists('app_url')) {
112
    /**
113
     * Generate an URL for the application.
114
     *
115
     * @param  string  $path
116
     * @param  mixed  $parameters
117
     * @param  string  $identifier
118
     * @return string
119
     */
120
    function app_url($path = '', $parameters = null, $identifier = 'site')
121
    {
122
        $path = trim($path, '/');
123
        if (! empty($path) && ! starts_with($path, ['?', '&', '#'])) {
124
            $path = '/'.$path;
125
        }
126
127
        if (! is_null($parameters)) {
128
            $query = http_build_query($parameters);
129
            if (! empty($query)) {
130
                $path .= (str_contains($path, ['?', '&', '#']) ? '&' : '?').$query;
131
            }
132
        }
133
134
        if ($identifier && ($root = config('support.url.'.$identifier))) {
135
            return $root.$path;
136
        }
137
138
        return url($path);
139
    }
140
}
141
142
if (! function_exists('revision')) {
143
    /**
144
     * Get the revisioned asset path.
145
     *
146
     * @param  string  $path
147
     * @return string
148
     */
149
    function revision($path)
150
    {
151
        if ($rev = array_get(config('assets'), trim($path, '/'))) {
152
            return $path.'?'.$rev;
153
        }
154
155
        return $path;
156
    }
157
}
158
159
if (! function_exists('asset_url')) {
160
    /**
161
     * Generate an asset URL.
162
     *
163
     * @param  string $path
164
     * @return string
165
     */
166
    function asset_url($path, $identifier = 'asset')
167
    {
168
        if (filter_var($path, FILTER_VALIDATE_URL) !== false) {
169
            return $path;
170
        }
171
172
        return config('support.url.'.$identifier).'/'.revision(trim($path, '/'));
173
    }
174
}
175
176
if (! function_exists('cdn_url')) {
177
    /**
178
     * Generate an asset CDN URL.
179
     *
180
     * @param  string  $path
181
     * @return string
182
     */
183
    function cdn_url($path)
184
    {
185
        return asset_url($path, 'cdn');
186
    }
187
}
188
189
if (! function_exists('api')) {
190
    /**
191
     * Create a new API response.
192
     *
193
     * @return \App\Http\ApiResponse
194
     */
195
    function api(...$args)
196
    {
197
        return response()->api(...$args);
0 ignored issues
show
Bug introduced by
The method api() does not seem to exist on object<Illuminate\Contra...outing\ResponseFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
198
    }
199
}
200
201
if (! function_exists('optimus_encode')) {
202
    /**
203
     * Encode a number with Optimus.
204
     *
205
     * @param  int  $number
206
     * @return int
207
     */
208
    function optimus_encode($number)
209
    {
210
        return app('optimus')->encode($number);
211
    }
212
}
213
214
if (! function_exists('optimus_decode')) {
215
    /**
216
     * Decode a number with Optimus.
217
     *
218
     * @param  int  $number
219
     * @return int
220
     */
221
    function optimus_decode($number)
222
    {
223
        return app('optimus')->decode($number);
224
    }
225
}
226
227
if (! function_exists('random_uuid')) {
228
    /**
229
     * Generate a version 4 (random) UUID.
230
     *
231
     * @param  bool  $hex
232
     * @return string
233
     */
234
    function random_uuid($hex = false)
235
    {
236
        $uuid = Uuid::uuid4();
237
238
        return $hex ? $uuid->getHex() : $uuid->toString();
239
    }
240
}
241